44 lines
1.1 KiB
PowerShell
44 lines
1.1 KiB
PowerShell
function Get-Json {
|
|
<#
|
|
.SYNOPSIS
|
|
I get tired of having to type the right syntax to get the json object from a file
|
|
|
|
.PARAMETER Path
|
|
The file path
|
|
|
|
.PARAMETER AsHash
|
|
Convert the thing to an iterable Hash
|
|
#>
|
|
[CmdletBinding(DefaultParameterSetName = "FilePath")]
|
|
[OutputType([object])]
|
|
param (
|
|
[Parameter(Mandatory = $true, ParameterSetName = "FilePath", Position = 0)]
|
|
[ValidateNotNullorEmpty()]
|
|
[string]$Path,
|
|
[Parameter(Mandatory = $false, ParameterSetName = "FilePath")]
|
|
[switch]$AsHash,
|
|
[Parameter(Mandatory = $true, ParameterSetName = "JsonBlob")]
|
|
[ValidateNotNullorEmpty()]
|
|
[object]$JsonBlob
|
|
)
|
|
|
|
$logLead = (Get-LogLeadName)
|
|
|
|
if ($PSCmdlet.ParameterSetName -eq "FilePath") {
|
|
if (!(Test-Path $Path)) {
|
|
throw "$logLead : [$Path] does not exist"
|
|
}
|
|
|
|
$JsonBlob = (ConvertFrom-Json (Get-Content $Path -Raw))
|
|
|
|
if (!$AsHash) {
|
|
return $JsonBlob
|
|
}
|
|
}
|
|
|
|
if ($JsonBlob -is [string]) {
|
|
$JsonBlob = (ConvertFrom-Json $JsonBlob)
|
|
}
|
|
|
|
return (ConvertTo-Hashtable $JsonBlob)
|
|
} |