From 2fca1d3e2fbb15ae9d1b5c8a91af998564a0ff3a Mon Sep 17 00:00:00 2001 From: Christian Stein Date: Mon, 30 Mar 2020 09:32:54 +0200 Subject: [PATCH] Adds function to deserialize PSObject properties into name/value keypair --- .../tools/Get-IcingaPSObjectProperties.psm1 | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 lib/core/tools/Get-IcingaPSObjectProperties.psm1 diff --git a/lib/core/tools/Get-IcingaPSObjectProperties.psm1 b/lib/core/tools/Get-IcingaPSObjectProperties.psm1 new file mode 100644 index 0000000..f811b91 --- /dev/null +++ b/lib/core/tools/Get-IcingaPSObjectProperties.psm1 @@ -0,0 +1,48 @@ +function Get-IcingaPSObjectProperties() +{ + param( + $Object = $null, + [array]$Include = @(), + [array]$Exclude = @() + ); + + [hashtable]$RetValue = @{}; + + if ($null -eq $Object) { + return $RetValue; + } + + foreach ($property in $Object.PSObject.Properties) { + [string]$DataType = $property.TypeNameOfValue; + + if ($Include.Count -ne 0 -And -Not ($Include -Contains $property.Name)) { + continue; + } + + if ($Exclude.Count -ne 0 -And $Exclude -Contains $property.Name) { + continue; + } + + if ($DataType.Contains('string') -or $DataType.Contains('int') -Or $DataType.Contains('bool')) { + $RetValue.Add( + $property.Name, + $property.Value + ); + } else { + try { + $RetValue.Add( + $property.Name, + (Get-IcingaPSObjectProperties -Object $property.Value) + ); + } catch { + $RetValue.Add( + $property.Name, + ([string]$property.Value) + ); + } + + } + } + + return $RetValue; +}