private/Initialize-PSOSDCloud.ps1
function Initialize-PSOSDCloud { [CmdletBinding()] param ( # Specifies a path to one or more locations. [Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [ValidateNotNullOrEmpty()] [string[]] $Path = "$($MyInvocation.MyCommand.Module.ModuleBase)\OSDCloud.json", [System.Management.Automation.SwitchParameter] $AsJson ) #================================================= $Error.Clear() Write-Verbose "[$(Get-Date -format G)][$($MyInvocation.MyCommand.Name)] Start" $ModuleName = $($MyInvocation.MyCommand.Module.Name) Write-Verbose "[$(Get-Date -format G)][$($MyInvocation.MyCommand.Name)] ModuleName: $ModuleName" $ModuleBase = $($MyInvocation.MyCommand.Module.ModuleBase) Write-Verbose "[$(Get-Date -format G)][$($MyInvocation.MyCommand.Name)] ModuleBase: $ModuleBase" $ModuleVersion = $($MyInvocation.MyCommand.Module.Version) Write-Verbose "[$(Get-Date -format G)][$($MyInvocation.MyCommand.Name)] ModuleVersion: $ModuleVersion" #================================================= # Import the RAW content of the JSON file $rawJsonContent = Get-Content -Path $Path -Raw if ($AsJson) { return $rawJsonContent } # https://stackoverflow.com/questions/51066978/convert-to-json-with-comments-from-powershell $JsonContent = $rawJsonContent -replace '(?m)(?<=^([^"]|"[^"]*")*)//.*' -replace '(?ms)/\*.*?\*/' $hashtable = [ordered]@{} (ConvertFrom-Json $JsonContent).psobject.properties | ForEach-Object { $hashtable[$_.Name] = $_.Value } $global:PSOSDCloud = $hashtable #================================================= # End the function $Message = "[$(Get-Date -format G)][$($MyInvocation.MyCommand.Name)] End" Write-Verbose -Message $Message; Write-Debug -Message $Message #================================================= } |