-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated private functions for PowerShell Core compatibility
- Loading branch information
1 parent
5d3fa7d
commit 3fa5152
Showing
5 changed files
with
80 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,58 @@ | ||
<# | ||
Helper JSON functions to resolve the ConvertFrom-JSON maxJsonLength limitation, which defaults to 2 MB | ||
http://stackoverflow.com/questions/16854057/convertfrom-json-max-length/27125027 | ||
Uses Json.Net https://www.newtonsoft.com/json for increased performance and cross-platform compatibility on PowerShell 6 and later | ||
#> | ||
|
||
function ExpandPayload($response) | ||
{ | ||
[void][System.Reflection.Assembly]::LoadWithPartialName('System.Web.Extensions') | ||
return ParseItem -jsonItem ((New-Object -TypeName System.Web.Script.Serialization.JavaScriptSerializer -Property @{ | ||
MaxJsonLength = 67108864 | ||
}).DeserializeObject($response.Content)) | ||
function ExpandPayload($response) { | ||
$asm = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Newtonsoft.Json.dll") | ||
ConvertFrom-JsonNewtonsoft $response.Content | ||
} | ||
function ParseItem($jsonItem) | ||
{ | ||
if($jsonItem.PSObject.TypeNames -match 'Array') | ||
{ | ||
return ParseJsonArray -jsonArray ($jsonItem) | ||
} | ||
elseif($jsonItem.PSObject.TypeNames -match 'Dictionary') | ||
{ | ||
return ParseJsonObject -jsonObj ([HashTable]$jsonItem) | ||
} | ||
else | ||
{ | ||
return $jsonItem | ||
} | ||
|
||
function ConvertFrom-JObject($obj) { | ||
if ($obj -is [Newtonsoft.Json.Linq.JArray]) { | ||
$a = foreach($entry in $obj.GetEnumerator()) { | ||
@(convertfrom-jobject $entry) | ||
} | ||
return $a | ||
} | ||
elseif ($obj -is [Newtonsoft.Json.Linq.JObject]) { | ||
$h = [ordered]@{} | ||
foreach($kvp in $obj.GetEnumerator()) { | ||
$val = convertfrom-jobject $kvp.value | ||
if ($kvp.value -is [Newtonsoft.Json.Linq.JArray]) { $val = @($val) } | ||
$h += @{ "$($kvp.key)" = $val } | ||
} | ||
return [pscustomobject]$h | ||
} | ||
elseif ($obj -is [Newtonsoft.Json.Linq.JValue]) { | ||
return $obj.Value | ||
} | ||
else { | ||
return $obj | ||
} | ||
} | ||
|
||
function ParseJsonObject($jsonObj) | ||
{ | ||
$result = New-Object -TypeName PSCustomObject | ||
foreach ($key in $jsonObj.Keys) | ||
{ | ||
$item = $jsonObj[$key] | ||
if ($item) | ||
function ConvertFrom-JsonNewtonsoft { | ||
[CmdletBinding()] | ||
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)]$string) | ||
|
||
$HandleDeserializationError = | ||
{ | ||
$parsedItem = ParseItem -jsonItem $item | ||
param ([object] $sender, [Newtonsoft.Json.Serialization.ErrorEventArgs] $errorArgs) | ||
$currentError = $errorArgs.ErrorContext.Error.Message | ||
write-warning $currentError | ||
$errorArgs.ErrorContext.Handled = $true | ||
|
||
} | ||
else | ||
{ | ||
$parsedItem = $null | ||
|
||
$settings = new-object "Newtonsoft.Json.JSonSerializerSettings" | ||
if ($ErrorActionPreference -eq "Ignore") { | ||
$settings.Error = $HandleDeserializationError | ||
} | ||
$result | Add-Member -MemberType NoteProperty -Name $key -Value $parsedItem | ||
} | ||
return $result | ||
} | ||
$obj = [Newtonsoft.Json.JsonConvert]::DeserializeObject($string, [Newtonsoft.Json.Linq.JObject], $settings) | ||
|
||
function ParseJsonArray($jsonArray) | ||
{ | ||
$result = @() | ||
$jsonArray | ForEach-Object -Process { | ||
$result += , (ParseItem -jsonItem $_) | ||
} | ||
return $result | ||
return ConvertFrom-JObject $obj | ||
} | ||
|
||
function ParseJsonString($json) | ||
{ | ||
$config = $javaScriptSerializer.DeserializeObject($json) | ||
return ParseJsonObject -jsonObj ($config) | ||
} | ||
function ConvertTo-JsonNewtonsoft([Parameter(Mandatory=$true,ValueFromPipeline=$true)]$obj) { | ||
return [Newtonsoft.Json.JsonConvert]::SerializeObject($obj, [Newtonsoft.Json.Formatting]::Indented) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function Invoke-RubrikWebRequest { | ||
<# | ||
.SYNOPSIS | ||
Custom wrapper for Invoke-WebRequest, implemented to provide different parameter sets depending on PowerShell version | ||
#> | ||
param( | ||
$Uri, | ||
$Headers, | ||
$Method, | ||
$Body | ||
) | ||
|
||
if (Test-PowerShellSix) { | ||
Invoke-WebRequest -UseBasicParsing -SkipCertificateCheck @PSBoundParameters | ||
} else { | ||
Invoke-WebRequest -UseBasicParsing @PSBoundParameters | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
function Test-PowerShellSix { | ||
<# | ||
.SYNOPSIS | ||
Test if the PowerShell version is 6 or higher, this is to provide backwards compatibility for older version of PowerShell | ||
#> | ||
$PSVersionTable.PSVersion.Major -ge 6 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters