Powershell Script Settings System
It has been a short while since I have posted something here. I have been working, and have almost completed, an end-to-end failover system that uses VMWare Orchestrator and Windows Powershell to automate complete site failover (Handles VMWare, LUN(s) Attached Directly to VM Guest OS, Windows, Linux, SRM Failover, SAN Replication, SQL AlwaysOn DAG Failover, and more).
In building this mammoth of a system I needed a way to quickly tune my script to certain environments without having to re-write a lot of code or ask end-users to make changes to variables in my script. To accomplish this goal I created an ini-like settings system. Since I am in a giving mood today -- I will share this system with all of you. It is well documented and ready for use in any of your own projects. The only thing I ask is that you keep my information in the SettingsParser.ps1 file.
Since LinkedIn does a terrible job of letting me post code here, I have posted 3 files to pastebin. Copy each file and save them with the proper filenames in the same folder and then run SettingsTestScript.ps1 in powershell.
SettingsParser.ps1 - https://pastebin.com/rLhs8SDc
SettingsTestScript.ps1 - https://pastebin.com/FzUVSbAF
Settings.Test.Ini - https://pastebin.com/BM8rgqF4
The INI file itself uses the following formatting:
A # at the start of a line is a comment
A Settings Key is enclosed in []
Example Settings Key with no SubKey: [Global]
Example Settings Key with SubKey: [OS:Windows]
A Settings Key or SubKey can contain: a-z, A-Z, 0-9, Space, ., or -
Settings for a Key are defined inside a { } block
The Opening { belongs after you define the Key
Examples: [MySetting] { OR [Setting:SubKey] {
A setting should be in the form of MySettingKey=Value
Valid Setting Key Characters are a-z, A-Z, 0-9, and a .
other spaces and special symbols are not allowed.
The Value of a setting can be any valid character.
multiple setting values can be offset with a , and no space
example: My.Setting.Key=Value1,Value2,Value3
a setting can be a null value
example: My.Setting.Key=
to specify the end of a settings key use a single } on a new line
Example with no SubKeys:
[Global] {
AllowSkip=False
Setting2=Use,Multiple,Settings
}
Example using a SubKey (Allows you to Group things)
[OS:Default] {
UseWmi=false
Collect.Lun.Information=false
}
[OS:Windows] {
UseWmi=true
Collect.Lun.Information=true
}
Using this in you script is as easy as including the SettingsParser.ps1 file in your powershell project/script and then simply saying:
$settings = Import-Settings -Filename Path-To-Settings-File.ini
After that you can refer to your settings via $settings["Key"]["Setting"] or $settings["MainKey"]["SubKey"]["Setting"]
e.g. using the example above if I wanted to get the default subkey setting for useWmi it would be $settings["OS"]["Default"]["UseWmi"]
Now users do not have to monkey around in your script files and or you can compile your script files and still allow settings to be changed. If you find yourself having questions about usage please feel free to message me on here.
Product Support Engineer II at UiPath
8 年This is good stuff! Thanks Brian Clark
Manager Devops Engineering | Elevating Teams & Optimizing Processes with Servant Leadership
8 年Nice piece