Skip to content

Commit

Permalink
Updated private functions for PowerShell Core compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jaapbrasser committed Sep 6, 2018
1 parent 5d3fa7d commit 3fa5152
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 56 deletions.
95 changes: 46 additions & 49 deletions Rubrik/Private/Format-JSON.ps1
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)
}
18 changes: 18 additions & 0 deletions Rubrik/Private/Invoke-RubrikWebRequest.ps1
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
}
}
7 changes: 4 additions & 3 deletions Rubrik/Private/Submit-Request.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
try {
Write-Verbose -Message 'Submitting the request'
# Because some calls require more than the default payload limit of 2MB, ExpandPayload dynamically adjusts the payload limit
$result = ExpandPayload -response (Invoke-WebRequest -Uri $uri -Headers $header -Method $method -Body $body)
$result = ExpandPayload -response (Invoke-RubrikWebRequest -Uri $uri -Headers $header -Method $method -Body $body)
}
catch {
switch -Wildcard ($_) {
Expand All @@ -35,7 +35,8 @@
}
}
}

return $result

return $result

}
}
7 changes: 7 additions & 0 deletions Rubrik/Private/Test-PowerShellSix.ps1
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
}
9 changes: 5 additions & 4 deletions Rubrik/Private/Test-VMwareConnection.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
# Potential future work: loop through all vCenter connections
# Code snipet blatantly stolen from Vester :)

If ($DefaultVIServers.Count -lt 1)
if ((Get-Module -ListAvailable -Name VMware.PowerCLI) -eq $null) {
Write-Warning -Message 'Please install VMware PowerCli PowerShell module before running this command.'
throw 'VMware.PowerCli module is required.'
} ElseIf ($DefaultVIServers.Count -lt 1)
{
Write-Warning -Message 'Please connect to vCenter before running this command.'
throw 'A single connection with Connect-VIServer is required.'
}
ElseIf ($DefaultVIServers.Count -gt 1)
{
} ElseIf ($DefaultVIServers.Count -gt 1) {
Write-Warning -Message 'Please connect to only one vCenter before running this command.'
Write-Warning -Message "Current connections: $($DefaultVIServers -join ' / ')"
throw 'A single connection with Connect-VIServer is required.'
Expand Down

0 comments on commit 3fa5152

Please sign in to comment.