PowerShell script config by Dmitry Sotnikov 21 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1824"></script>download | new post
Stores script configuration on computer. This one is generic and would work with any PowerShell script/tool. A more PowerGUI specific one can be found at: http://dmitrysotnikov.wordpress.com/2010/05/07/storing-powergui-add-on-configuration
- #################################################
- # Sample code showing how to save/load PowerShell script
- # configuration to disk
- #
- # (c) Dmitry Sotnikov
- # http://dmitrysotnikov.wordpress.com/2010/05/07/storing-powergui-add-on-configuration
- #################################################
- # Assign unique folder and config names
- # This would sore data in
- # C:\Users\username\AppData\Roaming\MyPowerGUIAddOn\MyAddOn.Config.xml
- $FolderName = "MyPowerGUIAddOn"
- $ConfigName = "MyAddOn.Config.xml"
- # keep all add-on parameters in a map
- $parameters = @{}
- # read parameters
- if ( Test-Path -Path "$($env:AppData)\$FolderName\$ConfigName") {
- $parameters = Import-Clixml -Path "$($env:AppData)\$FolderName\$ConfigName"
- # now you can use them, e.g.
- $parameters['A']
- $parameters['B']
- } else {
- # Config does not exist yet - set defaults
- $parameters['A'] = 5
- $parameters['B'] = "Qwerty"
- }
- # store parameters
- if ( -not (Test-Path -Path "$($env:AppData)\$FolderName")) {
- mkdir "$($env:AppData)\$FolderName"
- }
- $parameters | Export-Clixml -Path "$($env:AppData)\$FolderName\$ConfigName"
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
PowerShell Code Repository