Functions/Get-DryADCommentedJson.ps1
Function Get-DryADCommentedJson { [CmdletBinding()] Param ( [ValidateScript({(Test-Path $_ -PathType 'leaf') -and (($_ -match ".jsonc$") -or ($_ -match ".json$"))})] [Parameter(Mandatory,ParameterSetName='stringpath')] [String]$Path, [ValidateScript({($_.exists -and (($_.name -match ".jsonc$") -or ($_.name -match ".json$")))})] [Parameter(Mandatory,ParameterSetName='fileinfo')] [System.IO.FileInfo]$File ) Try { If ($Path) { [System.IO.FileInfo]$Item = Get-Item -Path $Path -ErrorAction Stop } Else { [System.IO.FileInfo]$Item = $File } # Get all lines that does not start with comment, i.e "//" [Array]$ContentArray = $Item | Get-Content -ErrorAction Stop | Where-Object { $_.Trim() -notmatch "^//" } # Remove any comment at the end of line For ($Line = 0; $Line -lt $ContentArray.Count; $Line++) { If ($ContentArray[$Line] -match "//") { $ContentArray[$Line] = $ContentArray[$Line].Substring(0, $ContentArray[$Line].IndexOf('//')) } } [String]$ContentString = $ContentArray | Out-String -ErrorAction 'Stop' # Convert to PSObject and return ConvertFrom-Json -InputObject $ContentString -ErrorAction 'Stop' } Catch { $PSCmdlet.ThrowTerminatingError($_) } } |