2020-04-28 09:24:57 -04:00
<#
. SYNOPSIS
Reads the entire configuration and returns it as custom object
. DESCRIPTION
Reads the entire configuration and returns it as custom object
. FUNCTIONALITY
Reads the entire configuration and returns it as custom object
. EXAMPLE
PS > Read-IcingaPowerShellConfig ;
. OUTPUTS
System . Object
. LINK
https : / / github . com / Icinga / icinga-powershell -framework
#>
2019-10-05 15:56:23 -04:00
function Read-IcingaPowerShellConfig ( )
{
2021-08-21 06:28:42 -04:00
$ConfigDir = Get-IcingaPowerShellConfigDir ;
$ConfigFile = Join-Path -Path $ConfigDir -ChildPath 'config.json' ;
$ConfigObject = ( New-Object -TypeName PSObject ) ;
[ string ] $Content = Read-IcingaFileSecure -File $ConfigFile -ExitOnReadError ;
2019-10-05 15:56:23 -04:00
2021-08-21 06:28:42 -04:00
if ( [ string ] :: IsNullOrEmpty ( $Content ) -eq $FALSE ) {
try {
$ConfigObject = ( ConvertFrom-Json -InputObject $Content -ErrorAction Stop ) ;
} catch {
New-Item -ItemType Directory -Path ( Join-Path -Path $ConfigDir -ChildPath 'corrupt' ) -ErrorAction SilentlyContinue ;
$NewConfigFile = Join-Path -Path $ConfigDir -ChildPath ( [ string ] :: Format ( 'corrupt/config_broken_{0}.json' , ( Get-Date -Format " yyyy-MM-dd-HH-mm-ss-ffff " ) ) ) ;
Move-Item -Path $ConfigFile -Destination $NewConfigFile -ErrorAction SilentlyContinue ;
New-Item -ItemType File -Path $ConfigFile -ErrorAction SilentlyContinue ;
2019-10-05 15:56:23 -04:00
2021-12-09 11:42:06 -05:00
Write-IcingaEventMessage -EventId 1100 -Namespace 'Framework' -ExceptionObject $_ -Objects $ConfigFile , $Content ;
2021-08-21 06:28:42 -04:00
Write-IcingaConsoleError -Message 'Your configuration file "{0}" was corrupt and could not be read. It was moved to "{1}" for review and a new plain file has been created' -Objects $ConfigFile , $NewConfigFile ;
2019-10-05 15:56:23 -04:00
2021-08-21 06:28:42 -04:00
$ConfigObject = ( New-Object -TypeName PSObject ) ;
}
2019-10-05 15:56:23 -04:00
}
2021-08-21 06:28:42 -04:00
return $ConfigObject ;
2019-10-05 15:56:23 -04:00
}