Last active
August 9, 2024 13:37
-
-
Save jhochwald/56cf0897fa6b82e65f12 to your computer and use it in GitHub Desktop.
How to use a JSON based config file with PowerShell Modules or Scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"_Info": [ | |
"This is the JSON based Configuration for YOUR PROJECT OR SCRIPT HERE", | |
"", | |
"Please do not edit any fields without any permission of YOUR NAME HERE." | |
], | |
"InternalInfo": { | |
"project": "YOUR PROJECT OR SCRIPT HERE", | |
"Modified": "XX/XX/2015", | |
"Author": "YOUR NAME <[email protected]>", | |
"Support": "YOUR SUPPORT CONDITION OR URL", | |
"license": "YOUR LICENSE TERMS HERE" | |
}, | |
"Basic": { | |
"_ConfigVersion": "Configuration Version - For future usage only", | |
"ConfigVersion": "XX.XX.XX", | |
"_Customer": "Name of the customer company - For future usage only", | |
"Customer": "YOR CUSTOMER NAME HERE", | |
"_Environment": "Valid is: Production, Dryrun, Leaduser, Testing, Development", | |
"Environment": "Production", | |
"_Support": "This value will be provided by the support team, never change this by yourself!", | |
"Support": "VALUE HERE IF NEEDED" | |
}, | |
"Logging": { | |
"_enabled": "Boolean / Valid is: true or false", | |
"enabled": "false", | |
"Settings": { | |
"_Directory": "An Example could be: Microsoft.PowerShell.Core\\FileSystem::\\\\psf\\Home\\Documents\\dev\\clones.new\\sca-powershell\\ - Mind the JSON escapes!", | |
"Directory": "C:\\scripts\\PowerShell\\logs\\", | |
"_level": "Valid is: Loglevel from 0 (none) to 9 (debug)", | |
"level": "0", | |
"_Debug": "General debug switch", | |
"Debug": "false" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
{ | |
"info": { | |
"Statement": "Code is poetry", | |
"Author": "Joerg Hochwald", | |
"Contact": "[email protected]", | |
"Link": "http://hochwald.net", | |
"Support": "https://github.com/jhochwald/MyPowerShellStuff/issues" | |
}, | |
"Copyright": "(c) 2012-2015 by Joerg Hochwald. All rights reserved." | |
} | |
Redistribution and use in source and binary forms, with or without modification, | |
are permitted provided that the following conditions are met: | |
1. Redistributions of source code must retain the above copyright notice, this list of | |
conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation and/or | |
other materials provided with the distribution. | |
3. Neither the name of the copyright holder nor the names of its contributors may | |
be used to endorse or promote products derived from this software without | |
specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR | |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY | |
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
POSSIBILITY OF SUCH DAMAGE. | |
By using the Software, you agree to the License, Terms and Conditions above! | |
#> | |
# Base Directory | |
# This must match with the UpdateService/LocalePath entry ($Config.UpdateService.LocalePath) | |
# in the JSON configuration file if you want to use the automated update/Distribution features! | |
$global:BaseDirectory = "C:\scripts\PowerShell\" | |
# JSON configuration filename to use | |
$global:BaseConfig = "config.json" | |
# Load and parse the JSON configuration file | |
try { | |
$global:Config = Get-Content "$BaseDirectory$BaseConfig" -Raw -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue | ConvertFrom-Json -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue | |
} catch { | |
Write-PoshError -Message "The Base configuration file is missing!" -Stop | |
} | |
# Check the configuration | |
if (!($Config)) { | |
Write-PoshError -Message "The Base configuration file is missing!" -Stop | |
} | |
<# | |
Any further Script here | |
#> | |
<# | |
Now we read the JSON File and use it | |
#> | |
# Internal Version information (For future use) | |
$global:ConfigVersion = ($Config.basic.ConfigVersion) | |
# Customer Info (For future use) | |
$global:Company = ($Config.basic.Customer) | |
# Environment (Production, Leaduser, Testing, Development) | |
$global:environment = ($Config.basic.environment) | |
<# | |
Any further Script here | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You are right!
The script above uses global variables as a show case. If declare variables in a script and try to use them in another script, it might cause issues.
Within a module I would use $script: to define variables that stays intact with the module. And I also delete all variables after using them and don’t need them anymore.