Private/ConvertTo-HypervisorPayloadJson.ps1
|
function Remove-NullProperties { param([object]$Object) if ($null -eq $Object) { return $null } if ($Object.PSObject.Properties) { $hash = [ordered]@{} foreach ($prop in $Object.PSObject.Properties) { if ($null -ne $prop.Value -and -not ($prop.Value -is [string] -and [string]::IsNullOrEmpty($prop.Value))) { $hash[$prop.Name] = $prop.Value } } return [pscustomobject]$hash } return $Object } function ConvertTo-CleanHypervisorEvent { param([object]$HypervisorEvent) $cleanedEvent = [ordered]@{ start_time = $HypervisorEvent.start_time duration = $HypervisorEvent.duration } foreach ($metric in @('cpu', 'disk', 'memory')) { if ($null -ne $HypervisorEvent.$metric) { $cleanedEvent[$metric] = Remove-NullProperties -Object $HypervisorEvent.$metric } } return [pscustomobject]$cleanedEvent } function ConvertTo-CleanHypervisorDataItem { param( [object]$DataItem, [bool]$ExcludeVirtualMachines, [bool]$ExcludeHypervisorEvents ) $cleanedDataItem = [ordered]@{ host = Remove-NullProperties -Object $DataItem.host } if (-not $ExcludeHypervisorEvents) { $cleanedDataItem.events = [System.Collections.Generic.List[object]]::new() foreach ($hypervisorEvent in $DataItem.events) { [void]$cleanedDataItem.events.Add((ConvertTo-CleanHypervisorEvent -HypervisorEvent $hypervisorEvent)) } } if (-not $ExcludeVirtualMachines -and $null -ne $DataItem.virtual_machines -and $DataItem.virtual_machines.Count -gt 0) { $cleanedDataItem.virtual_machines = [System.Collections.Generic.List[object]]::new() foreach ($virtualMachine in $DataItem.virtual_machines) { [void]$cleanedDataItem.virtual_machines.Add((Remove-NullProperties -Object $virtualMachine)) } } return $cleanedDataItem } function ConvertTo-HypervisorPayloadJson { [CmdletBinding()] param( [Parameter(Mandatory = $true)] [NutanixHypervisorPayload]$Payload, [switch]$ExcludeVirtualMachines, [switch]$ExcludeHypervisorEvents ) # Build cleaned payload structure $cleanedPayload = [pscustomobject][ordered]@{ schema_version = $Payload.schema_version source = $Payload.source customer_environment = $Payload.customer_environment version = $Payload.version data = [System.Collections.Generic.List[object]]::new() } foreach ($dataItem in $Payload.data) { $cleanedDataItem = ConvertTo-CleanHypervisorDataItem ` -DataItem $dataItem ` -ExcludeVirtualMachines $ExcludeVirtualMachines ` -ExcludeHypervisorEvents $ExcludeHypervisorEvents [void]$cleanedPayload.data.Add($cleanedDataItem) } return ($cleanedPayload | ConvertTo-Json -Depth 20 -Compress) } |