Skip to content

Instantly share code, notes, and snippets.

@gitfvb
Created August 23, 2018 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gitfvb/22fd85208306bcfd53ed54a8d17a7c84 to your computer and use it in GitHub Desktop.
Save gitfvb/22fd85208306bcfd53ed54a8d17a7c84 to your computer and use it in GitHub Desktop.
settings in json files - an easy example... this can be nested, also pscustomobjects can be nested
################################################
#
# SCRIPT ROOT
#
################################################
# script root path
if ($MyInvocation.MyCommand.CommandType -eq "ExternalScript")
{ $scriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition }
else
{ $scriptPath = Split-Path -Parent -Path ([Environment]::GetCommandLineArgs()[0]) }
$settings = [pscustomobject]@{
template = "value a";
data = "value b";
attachments = @("array content A";"array content B"); # array
}
################################################
#
# PACK TOGETHER SETTINGS AND SAVE AS JSON
#
################################################
$json = $settings | ConvertTo-Json -Depth 8 # -compress
$json
$json | Set-Content -path "$( $scriptPath )\settings.json" -Encoding UTF8
################################################
#
# PREPARATION / ASSEMBLIES
#
################################################
# Load scriptpath
if ($MyInvocation.MyCommand.CommandType -eq "ExternalScript") {
$scriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
} else {
$scriptPath = Split-Path -Parent -Path ([Environment]::GetCommandLineArgs()[0])
}
# Load settings
$settings = Get-Content -Path "$( $scriptPath )\settings.json" -Encoding UTF8 | ConvertFrom-Json
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment