Configuration Design Idea #70
Replies: 3 comments
-
|
I worked out a similar solution for functions defined between the registration commands. First, load the functions into your PowerShell session normally. Then export them to a JSON file. Get-Command LuckyNumber,Get-MyStatus,Get-PSRunVersion |
Select-Object Name,Definition |
ConvertTo-Json |
Out-File c:\scripts\psrun-functions.json -Encoding utf8In the configuration script, you can import this data. Start-PSRunFunctionRegistration
Get-Content $PSScriptRoot\psrun-functions.json |
ConvertFrom-Json |
Foreach-Object {
[void](New-Item -path Function: -Name "global:$($_.Name)" -Value ([ScriptBlock]::Create($_.definition)) -Force )
}
Stop-PSRunFunctionRegistrationI keep the function source code in a separate file that I can update or edit and re-export to JSON. |
Beta Was this translation helpful? Give feedback.
-
|
I've extended my techniques to also import theme settings. I exported the current theme to a JSON file. ((Get-PSRunDefaultSelectorOption).Theme | convertTo-JSON).split("`n") | where {$_ -notmatch "null"} | out-file d:\temp\psruntheme.json -Encoding utf8This strips out off of the Null entries. I then edit and clean up the file to define the elements I want to include in my theme. {
"ConsoleCursorShape": 0,
"KeyRemapModeConsoleCursorShape": 6,
"CanvasHeightPercentage": 75,
"PreviewSizePercentage": 60,
"NameWidthPercentage": 30,
"PreviewPosition": 1,
"CanvasTopMargin": 1,
"CanvasBorderFlags": 0,
"CanvasBorderSymbol": {
"Vertical": "│",
"Horizontal": "─",
"TopLeft": "╭",
"TopRight": "╮",
"BottomLeft": "╰",
"BottomRight": "╯"
},
"CanvasBorderForegroundColor": "Yellow",
"CursorEnable": true,
"Marker": "✓ ",
"PromptSymbol": null,
"PromptForegroundColor": "BrightYellow",
"QueryStyle": 0,
"SearchBarBorderFlags": 8,
"SearchBarBorderSymbol": {
"Vertical": "│",
"Horizontal": "─",
"TopLeft": "┌",
"TopRight": "┐",
"BottomLeft": "└",
"BottomRight": "┘"
},
"SearchBarBorderForegroundColor": "BrightGreen",
"CursorForegroundColor": "Magenta",
"MarkerForegroundColor": "Yellow",
"IconEnable": true,
"IconForegroundColor": null,
"NameStyle": 0,
"NameHighlightForegroundColor": "Cyan",
"NameHighlightStyle": 0,
"NameFocusStyle": 4,
"NameFocusHighlightBackgroundColor": "Magenta",
"NameFocusHighlightStyle": 4,
"DescriptionEnable": true,
"DescriptionForegroundColor": "BrightBlue",
"DescriptionStyle": 0,
"DescriptionHighlightForegroundColor": "Cyan",
"DescriptionHighlightStyle": 0,
"DescriptionFocusStyle": 4,
"DescriptionFocusHighlightForegroundColor": "Magenta",
"DescriptionFocusHighlightStyle": 4,
"EntryBorderFlags": 0,
"EntryBorderSymbol": {
"Vertical": "│",
"Horizontal": "─",
"TopLeft": "┌",
"TopRight": "┐",
"BottomLeft": "└",
"BottomRight": "┘"
},
"PreviewEnable": true,
"PreviewStyle": 0,
"PreviewBorderFlags": 15,
"PreviewBorderSymbol": {
"Vertical": "│",
"Horizontal": "─",
"TopLeft": "╭",
"TopRight": "╮",
"BottomLeft": "╰",
"BottomRight": "╯"
},
"PreviewBorderForegroundColor": "BrightYellow",
"ActionWindowKeyStyle": 0,
"ActionWindowKeyFocusStyle": 4,
"ActionWindowDescriptionForegroundColor": "BrightBlue",
"ActionWindowDescriptionStyle": 0,
"ActionWindowDescriptionFocusStyle": 4,
"ActionWindowBorderEnable": true,
"ActionWindowBorderSymbol": {
"Vertical": "│",
"Horizontal": "─",
"TopLeft": "╭",
"TopRight": "╮",
"BottomLeft": "╰",
"BottomRight": "╯"
},
"ActionWindowBorderForegroundColor": "BrightYellow",
"ActionWindowScrollBarForegroundColor": "Magenta",
"TabSize": 6
}In my configuration script, I import the file. $data = Get-Content $PSScriptRoot\psruntheme.json | ConvertFrom-JsonI create a new option object and set my prompt. $option = [PowerShellRun.SelectorOption]::new()
$option.Prompt = 'Hey Jeff! 🤓 --> '
```
Finally, I loop through the properties of the imported data and set the corresponding value of the option object.
```powershell
($option.theme.PSObject.Properties).ForEach({
if ($_.Name -match "color" -AND $data.$($_.Name)) {
#Write-Host "Color: $($_.Name) = $($data.$($_.Name))" -ForegroundColor green
$_.Value = $($data.$($_.Name))
}
else {
$_.Value = $data.$($_.Name)
}
})
```
The last step is to make this option object my new default.
```powershell
Set-PSRunDefaultSelectorOption -Option $option
```
|
Beta Was this translation helpful? Give feedback.
-
|
I use a JSON file to store my PowerShell run settings. {
"InvokePsRunChord": "Ctrl+Alt+j",
"PSReadLineHistoryChord": "Ctrl+r",
"DefaultEditorScript": "param ($path)\r\ncode (Convert-Path $path)"
}In my configuration script, I import the file and apply the settings. $set = Get-Content $PSScriptRoot\PSRun-Settings.json | ConvertFrom-Json
$set | Set-PSRunPSReadLineKeyHandler
Set-PSRunDefaultEditorScript -ScriptBlock ([ScriptBlock]::Create($set.DefaultEditorScript)) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I thought I'd share how I am handling the configuration script for this module. I originally has a script file,
PSRun.ps1with all of the necessaryAdd-PSRunFavoriteFolder,Add-PSRunScriptBlock, andAdd-PSRunFavoriteFilecommands to define my configuration. Then I thought about separating the data I needed from the Add-PSRun* command.I've been testing storing different segments of my configuration data in JSON files. For example, here the JSON for my favorite folders.
[ { "Name": "Scripts", "Path": "C:\\Scripts", "Description": "My scripts folder" }, { "Name": "Temp D", "Path": "D:\\Temp", "Description": "Temp folder on D:" }, { "Name": "Temp C", "Path": "C:\\Temp", "Description": "Temp folder on C:" }, { "Name": "Work Files", "Path": "C:\\Work", "Description": "My work files" } ]In my configuration script, I can import the JSON and pass it directly to
Add-PSRunFavoriteFolder.Most of my configuration is arranged by entry group. Those I define normally in the configuration script.
But I can store the data in a JSON file.
[ { "PSRunType": "ScriptBlock", "Name": "Open Vantage", "Description": "Open Lenovo Vantage", "Icon": "🔋", "ScriptBlock": "Start-Process shell:AppsFolder\\E046963F.LenovoSettingsforEnterprise_k1h2ywk1493x8!App ; psrun" }, { "PSRunType": "ScriptBlock", "Name": "Spotify", "Description": "Open Spotify", "Icon": "🎵", "ScriptBlock": "Start-Process C:\\Users\\Jeff\\AppData\\Roaming\\Spotify\\Spotify.exe ; psrun" }, { "PSRunType": "ScriptBlock", "Name": "Obsidian", "Description": "Launch Obsidian", "Icon": "📝", "ScriptBlock": "Start-Process shell:AppsFolder\\md.obsidian ; psrun" }, { "PSRunType": "ScriptBlock", "Name": " VSCodium", "Description": " Launch VSCodium", "Icon": "🖱️", "ScriptBlock": "Start-Process shell:AppsFolder\\VSCodium.VSCodium" }, { "PSRunType": "ScriptBlock", "Name": "Visual Studio 2022 Community", "Description": "Launch Visual Studio", "Icon": "🦉", "ScriptBlock": "Start-Process shell:AppsFolder\\VisualStudio.2ff91b9b" }, { "PSRunType": "ScriptBlock", "Name": " HyperV Manager", "Description": " Launch the Hyper-V management console", "Icon": "🛡️", "ScriptBlock": "Start-Process 'C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Administrative Tools\\Hyper-V Manager.lnk' ; psrun" } ]Converting the script block string to an actual script block that
Add-PSRunScriptBlockcan process takes a little processing.You'll notice that each item has a
PSRunTypeproperty. This is because my group data might have scriptblocks, files, or folders. In my configuration script, I have a helper function to process the JSON data.The only thing I can't store in the JSON file is the entry group. But as long as it is defined in the configuration script I can use it later.
My configuration script has 6 lines of code that generates 27 PowerShellRun configuration entries. If I want to modify or add an item, I edit the JSON file, not the script.
I still need to do something similar with functions defined between the
PSRunFunctionRegistrationcommands.But I thought I'd share my work in hopes it sparks something for you.
Beta Was this translation helpful? Give feedback.
All reactions