2020-11-18 04:45:22 -05:00
<#
. SYNOPSIS
Allows to query Wmi information by either using Wmi directly or Cim . This provides a save handling
to call Wmi classes , as we are catching possible errors including missing permissions for better
and improved error output during plugin execution .
. DESCRIPTION
Allows to query Wmi information by either using Wmi directly or Cim . This provides a save handling
to call Wmi classes , as we are catching possible errors including missing permissions for better
and improved error output during plugin execution .
. PARAMETER ClassName
The Wmi class to fetch information from
. PARAMETER Filter
Allows to filter only for specific Wmi information . The syntax is identical to Get-WmiObject and Get-CimInstance
. PARAMETER Namespace
The Wmi namespace to lookup additional information . The syntax is identical to Get-WmiObject and Get-CimInstance
. PARAMETER ForceWMI
Forces the usage of ` Get-WmiObject ` instead of ` Get-CimInstance `
. EXAMPLE
PS > Get-IcingaWindowsInformation -ClassName Win32_Service ;
. EXAMPLE
PS > Get-IcingaWindowsInformation -ClassName Win32_Service -ForceWMI ;
. EXAMPLE
PS > Get-IcingaWindowsInformation -ClassName MSFT_NetAdapter -NameSpace 'root\StandardCimv2' ;
. EXAMPLE
PS > Get-IcingaWindowsInformation Win32_LogicalDisk -Filter 'DriveType = 3' ;
. INPUTS
System . String
. OUTPUTS
System . Boolean
#>
2020-07-27 05:32:36 -04:00
function Get-IcingaWindowsInformation ( )
{
param (
[ string ] $ClassName ,
2020-07-27 05:47:22 -04:00
$Filter ,
2020-08-05 13:48:18 -04:00
$Namespace ,
2020-07-27 05:47:22 -04:00
[ switch ] $ForceWMI = $FALSE
2020-07-27 05:32:36 -04:00
) ;
$Arguments = @ {
'ClassName' = $ClassName ;
}
if ( [ string ] :: IsNullOrEmpty ( $Filter ) -eq $FALSE ) {
$Arguments . Add (
'Filter' , $Filter
) ;
}
2020-08-05 13:48:18 -04:00
if ( [ string ] :: IsNullOrEmpty ( $Namespace ) -eq $FALSE ) {
$Arguments . Add (
'Namespace' , $Namespace
) ;
}
2020-07-27 05:32:36 -04:00
2020-07-27 05:47:22 -04:00
if ( $ForceWMI -eq $FALSE -And ( Get-Command 'Get-CimInstance' -ErrorAction SilentlyContinue ) ) {
2020-07-27 05:32:36 -04:00
try {
2020-11-19 06:55:36 -05:00
$CimData = ( Get-CimInstance @Arguments -ErrorAction Stop ) ;
Write-IcingaDebugMessage 'Debug output for "Get-IcingaWindowsInformation::Get-CimInstance"' -Objects $ClassName , $Filter , $Namespace , ( $CimData | Out-String ) ;
return $CimData ;
2020-07-27 05:32:36 -04:00
} catch {
$ErrorName = $_ . Exception . NativeErrorCode ;
$ErrorMessage = $_ . Exception . Message ;
2020-07-27 06:38:57 -04:00
$ErrorCode = $_ . Exception . StatusCode ;
2020-07-27 05:32:36 -04:00
2020-08-26 07:38:05 -04:00
if ( [ string ] :: IsNullOrEmpty ( $Namespace ) ) {
$Namespace = 'root/cimv2' ;
}
2020-07-27 06:38:57 -04:00
switch ( $ErrorCode ) {
# Permission error
2 {
2020-08-26 07:38:05 -04:00
Exit-IcingaThrowException -ExceptionType 'Permission' -ExceptionThrown $IcingaExceptions . Permission . CimInstance -CustomMessage ( [ string ] :: Format ( 'Class: "{0}", Namespace: "{1}"' , $ClassName , $Namespace ) ) -Force ;
2020-07-27 06:38:57 -04:00
} ;
2020-07-27 05:32:36 -04:00
# InvalidClass
5 {
Exit-IcingaThrowException -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions . Inputs . CimClassNameUnknown -CustomMessage $ClassName -Force ;
} ;
# All other errors
default {
2020-07-27 06:38:57 -04:00
Exit-IcingaThrowException -ExceptionType 'Custom' -InputString $ErrorMessage -CustomMessage ( [ string ] :: Format ( 'CimInstanceUnhandledError: Class "{0}": Error "{1}": Id "{2}"' , $ClassName , $ErrorName , $ErrorCode ) ) -Force ;
2020-07-27 05:32:36 -04:00
}
}
}
}
if ( ( Get-Command 'Get-WmiObject' -ErrorAction SilentlyContinue ) ) {
try {
2020-11-19 06:55:36 -05:00
$WmiData = ( Get-WmiObject @Arguments -ErrorAction Stop ) ;
Write-IcingaDebugMessage 'Debug output for "Get-IcingaWindowsInformation::Get-WmiObject"' -Objects $ClassName , $Filter , $Namespace , ( $WmiData | Out-String ) ;
return $WmiData ;
2020-07-27 05:32:36 -04:00
} catch {
$ErrorName = $_ . CategoryInfo . Category ;
$ErrorMessage = $_ . Exception . Message ;
2020-07-27 06:38:57 -04:00
$ErrorCode = ( $_ . Exception . HResult -band 0xFFFF ) ;
2020-07-27 05:32:36 -04:00
2020-08-26 07:38:05 -04:00
if ( [ string ] :: IsNullOrEmpty ( $Namespace ) ) {
$Namespace = 'root/cimv2' ;
}
2020-07-28 03:26:12 -04:00
switch ( $ErrorName ) {
2020-07-27 06:38:57 -04:00
# Permission error
2020-07-28 03:26:12 -04:00
'InvalidOperation' {
2020-08-26 07:38:05 -04:00
Exit-IcingaThrowException -ExceptionType 'Permission' -ExceptionThrown $IcingaExceptions . Permission . WMIObject -CustomMessage ( [ string ] :: Format ( 'Class: "{0}", Namespace: "{1}"' , $ClassName , $Namespace ) ) -Force ;
2020-07-27 06:38:57 -04:00
} ;
2020-07-28 03:26:12 -04:00
# Invalid Class
'InvalidType' {
2020-07-27 06:38:57 -04:00
Exit-IcingaThrowException -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions . Inputs . WmiObjectClassUnknown -CustomMessage $ClassName -Force ;
} ;
# All other errors
default {
Exit-IcingaThrowException -ExceptionType 'Custom' -InputString $ErrorMessage -CustomMessage ( [ string ] :: Format ( 'WmiObjectUnhandledError: Class "{0}": Error "{1}": Id "{2}"' , $ClassName , $ErrorName , $ErrorCode ) ) -Force ;
}
}
2020-07-27 05:32:36 -04:00
}
}
# Exception
Exit-IcingaThrowException -ExceptionType 'Custom' -InputString 'Failed to fetch Windows information by using CimInstance or WmiObject. Both commands are not present on the system.' -CustomMessage ( [ string ] :: Format ( 'CimWmiUnhandledError: Class "{0}"' , $ClassName ) ) -Force ;
}