mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-21 23:29:40 -05:00
Adds function to deserialize PSObject properties into name/value keypair
This commit is contained in:
parent
21e1d127c5
commit
2fca1d3e2f
1 changed files with 48 additions and 0 deletions
48
lib/core/tools/Get-IcingaPSObjectProperties.psm1
Normal file
48
lib/core/tools/Get-IcingaPSObjectProperties.psm1
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue