Welcome to ciysys blog

The Powershell way to keep configuration in file

Published on: 27th Nov 2019

Updated on: 16th Jan 2022

Explanation

In Windows environment, we are so used to the INI file or configuration in XML format. You may consider a modern way to keep configuration in a file which uses JSON format.

In Powershell, handling JSON format is quite easy

The next recipe is to call Set-Content and Get-Content to update file and load from file respectively.

We develop a simple configuration Powershell which handles the maintenance to the config file. Let's call this file config-my.ps1.

param (
    # list, add, set, delete
    [string]$axn,

    # the name of the setting to be updated.
    [string]$name,

    # new value
    [string]$value
)

# validate user parameter
if ($axn.Length -eq 0) {
    # default param
    $axn = "list"
}
elseif (($axn -eq "add") -or ($axn -eq "set")) {
    if ($name.Length -eq 0) {
        Write-Output "name or param cannot be blank"
        return
    }

    if ($value.Length -eq 0) {
        Write-Output "value param cannot be blank"
        return
    }
}
elseif ($axn -eq "delete") {
    if ($name.Length -eq 0) {
        Write-Output "name or param cannot be blank"
        return
    }    
}

#-------------------------------
# shared functions
#-------------------------------

function Get-TimeStamp {   
    return "[{0:yyMMdd} @ {0:HH:mm:ss}]" -f (Get-Date)   
}

$result = New-Object System.Collections.ArrayList

function Append-to-result ($msg2) {
    $ts2 = Get-TimeStamp   

    $o = New-Object psobject
    $o | Add-Member -MemberType NoteProperty -Name "time" -Value $ts2
    $o | Add-Member -MemberType NoteProperty -Name "result" -Value $msg2
    $dummy =  $result.Add($o)
}

#-------------------------------
# main process
#-------------------------------

$f = "$PSScriptRoot\my-ps.config"

if (Test-Path $f) {
    $config = (Get-Content -Path $f) | ConvertFrom-Json
}
else {
    $config = New-Object psobject
}

#-------------------
# run the process
#-------------------

if ($axn -eq "list") {
    # returns the settings
    $result = $config
}
elseif ($axn -eq "add") {
    # update the settings
    $b = [bool]($config.PSobject.Properties.name -match $name)

    if ($b) {
        # returns the result
        Append-to-result "$name setting already exists. skip updating"
    }
    else {
        # add new setting
        $config | Add-Member -MemberType NoteProperty -Name $name -Value $value

        # save to file
        $j = $config | ConvertTo-Json  | Set-Content -Path $f

        # returns the result
        Append-to-result "Updated $name setting"
    }    
}
elseif ($axn -eq "set") {    
    # update the settings
    $b = [bool]($config.PSobject.Properties.name -match $name)

    if ($b) {
        # update the value
        $config."$name" = $value
    }
    else {
        # add new setting
        $config | Add-Member -MemberType NoteProperty -Name $name -Value $value
    }

    # save to file
    $j = $config | ConvertTo-Json  | Set-Content -Path $f

    # returns the result
    Append-to-result "Updated $name setting"
}
elseif ($axn -eq "delete") {
    $b = [bool]($config.PSobject.Properties.name -match $name)

    if ($b) {
        $config.psobject.Properties.Remove($name)

        # save to file
        $j = $config | ConvertTo-Json  | Set-Content -Path $f

        # returns the result
        Append-to-result "Deleted $name"
    }
    else {        
        # returns the result
        Append-to-result "$name setting does not exist"
    }    
}

#-------------------------------
# result
#-------------------------------

$result | ConvertTo-Json

Here's how you use it

Jump to #POWERSHELL blog

Author

Lau Hon Wan, software developer.