Merge pull request #353 from Icinga:fix/file_writer_non_string_values

Fix: Icinga file writer fails on input not matching string

In case we add non-string values into our Icinga file writer, the output may not what we have been expected.
To resolve this, we will add support for these default inputs:

* Hashtable -> JSON
* Array -> Listed output
* PSCustomObject -> JSON
* Everything else -> simple string conversion
This commit is contained in:
Lord Hepipud 2021-08-21 14:15:48 +02:00 committed by GitHub
commit 8b29f505b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,6 +9,18 @@ function Write-IcingaFileSecure()
return; return;
} }
if ($null -eq $Value) {
$Value = '';
} elseif ($Value.GetType().Name.ToLower() -eq 'hashtable') {
$Value = ConvertTo-Json -InputObject $Value -Depth 100;
} elseif (([string]($Value.GetType().BaseType)).ToLower() -eq 'array') {
$Value = $Value | Out-String;
} elseif ($Value.GetType().Name.ToLower() -eq 'pscustomobject') {
$Value = ConvertTo-Json -InputObject $Value -Depth 100;
} else {
$Value = $Value | Out-String;
}
if ((Test-Path $File) -eq $FALSE) { if ((Test-Path $File) -eq $FALSE) {
try { try {
New-Item -ItemType File -Path $File -ErrorAction Stop | Out-Null; New-Item -ItemType File -Path $File -ErrorAction Stop | Out-Null;