PowerShell Tip: Add Parameters to Registry for Easy Access
I tend to work on large PowerShell scripts that accept parameters. In the past I’ve always defined these parameters in the script while I test, and then I have to go back and remove them when I blog about the script here. I decided I’d figure out an easier way to do this!
I’m already using my ISE addon to run scripts in a new PowerShell session, so I decided to edit that to add parameter support!
First off, I only care to do this for saved scripts, so I only have to edit the “else” statement in my ISE addon:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Run In New Window", {
If ($psISE.CurrentFile.IsUntitled) {
$ScriptBlock = $psISE.CurrentFile.Editor.Text
$newGuid = [guid]::NewGuid()
$TempFilePath = "$env:TEMP\$newGuid.ps1"
$ScriptBlock > $TempFilePath
$Command = "$TempFilePath"
Start-Process Powershell.exe -Wait -ArgumentList "-noexit","-ExecutionPolicy Bypass","-file $Command"
Remove-Item -Path $TempFilePath -Force
}
else {
$psISE.CurrentFile.save()
$CurrentFile = $psIse.CurrentFile
$CurrentFilePath = $CurrentFile.FullPath
Start-Process Powershell.exe -Wait -ArgumentList "-noexit","-ExecutionPolicy Bypass","-file `"$CurrentFilePath`""
}
},"ALT+F5") | out-Null
I ended up deciding to store these variables in the registry. I post a lot of my projects to GitHub and didn’t want a file full of variables in my Git repo. So I went to my registry and created a new Key under HKCU:\Software called EphingParams. I then need a way to tell it what script I’m using. The current project I’m working on has a lot of scripts in the same directory all using a similar set of parameters. So I created a key with the name of the folder “ConfigMgr_Console_Extensions”
The idea is, my launch script will check to see what the file path is. If the file path includes “\ConfigMgr_Console_Extensions\” (meaning it is in that folder) or the file path includes “\ConfigMgr_Console_Extensions.” (meaning it is the file name) then it will grab the parameters. Now I just need to use the registry editor to make a few test parameters:
I’ve finished with the setup! Now I need to add the logic to my ISE addon:
$arguments = '' try { Get-ChildItem HKCU:\Software\EphingParams | ForEach-Object { $Name = $_.PSChildName $NameToLower = $Name.ToLower() if(($CurrentFilePath.ToLower().Contains("\$nametolower\")) -or ($CurrentFilePath.ToLower().Contains("\$nametolower."))) { $Script:Properties = Get-ItemProperty -Path $_.PSPath $PropertyMembers = Get-Member -InputObject $Properties foreach ($property in $PropertyMembers) { if($Property.MemberType -eq 'NoteProperty') { if(($Property.Name -ne 'PSChildName') -and ($Property.Name -ne 'PSParentPath') -and ($Property.Name -ne 'PSPath') -and ($Property.Name -ne 'PSProvider')) { $PropName = $Property.Name $arguments = $arguments + " -$($Property.Name) `"$($Properties.$PropName)`"" } } } } } } catch { $arguments = '' }
This script block will get all registry keys in HKCU:\Software\EphingParams, and then check the filepath to see if it includes a child registry key. If it does, it grabs all the properties (excluding the 4 default properties) and adds them to the launch code as -PropertyName “PropertyValue”. The end result is your script being executed with the parameters in the registry!
Here’s the full script:
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Run In New Window", { If ($psISE.CurrentFile.IsUntitled) { $ScriptBlock = $psISE.CurrentFile.Editor.Text $newGuid = [guid]::NewGuid() $TempFilePath = "$env:TEMP\$newGuid.ps1" $ScriptBlock > $TempFilePath $Command = "$TempFilePath" Start-Process Powershell.exe -Wait -ArgumentList "-noexit","-ExecutionPolicy Bypass","-file $Command" Remove-Item -Path $TempFilePath -Force } else { $psISE.CurrentFile.save() $CurrentFile = $psIse.CurrentFile $CurrentFilePath = $CurrentFile.FullPath $arguments = '' try { Get-ChildItem HKCU:\Software\EphingParams | ForEach-Object { $Name = $_.PSChildName $NameToLower = $Name.ToLower() if(($CurrentFilePath.ToLower().Contains("\$nametolower\")) -or ($CurrentFilePath.ToLower().Contains("\$nametolower."))) { $Script:Properties = Get-ItemProperty -Path $_.PSPath $PropertyMembers = Get-Member -InputObject $Properties foreach ($property in $PropertyMembers) { if($Property.MemberType -eq 'NoteProperty') { if(($Property.Name -ne 'PSChildName') -and ($Property.Name -ne 'PSParentPath') -and ($Property.Name -ne 'PSPath') -and ($Property.Name -ne 'PSProvider')) { $PropName = $Property.Name $arguments = $arguments + " -$($Property.Name) `"$($Properties.$PropName)`"" } } } } } } catch { $arguments = '' } Start-Process Powershell.exe -Wait -ArgumentList "-noexit","-ExecutionPolicy Bypass","-file `"$CurrentFilePath`" $arguments" } },"ALT+F5") | out-Null
Leave a Comment