Hi Peter,
I thought about using the eventlog, it is odd that I am not seeing any errors in script execution though. As you can see in the code snippet below, I have also added it as a Task, which returns the desired output when run… must be something with the monitor itself. FWIW this is SCOM 2016.
<?xml version="1.0" encoding="utf-8"?><ManagementPack ContentReadable="true" SchemaVersion="2.0" OriginalSchemaVersion="1.1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<Manifest>
<Identity>
<ID>BKLPRODReboot</ID>
<Version>1.0.0.0</Version>
</Identity>
<Name>BKL-PROD-Reboot</Name>
<References>
<Reference Alias="MicrosoftWindowsLibrary7585010">
<ID>Microsoft.Windows.Library</ID>
<Version>7.5.8501.0</Version>
<PublicKeyToken>31bf3856ad364e35</PublicKeyToken>
</Reference>
<Reference Alias="SystemLibrary7585010">
<ID>System.Library</ID>
<Version>7.5.8501.0</Version>
<PublicKeyToken>31bf3856ad364e35</PublicKeyToken>
</Reference>
<Reference Alias="PowerShellMonitoring">
<ID>Community.PowerShellMonitoring</ID>
<Version>1.1.1.2</Version>
<PublicKeyToken>3aa540324b898d3c</PublicKeyToken>
</Reference>
<Reference Alias="SystemCenter">
<ID>Microsoft.SystemCenter.Library</ID>
<Version>7.0.8437.0</Version>
<PublicKeyToken>31bf3856ad364e35</PublicKeyToken>
</Reference>
<Reference Alias="Health">
<ID>System.Health.Library</ID>
<Version>7.0.8437.0</Version>
<PublicKeyToken>31bf3856ad364e35</PublicKeyToken>
</Reference>
</References>
</Manifest>
<Monitoring>
<Tasks>
<Task ID="ConsoleTaskGeneratedByUI8980d9d7989343879312173f5ce8dbe0" Accessibility="Public" Enabled="true" Target="MicrosoftWindowsLibrary7585010!Microsoft.Windows.Server.OperatingSystem" Timeout="300" Remotable="true" CanSuspend="false" CanCancel="false">
<Category>Custom</Category>
<WriteAction ID="PA" TypeID="PowerShellMonitoring!Community.PowerShellMonitoring.WriteAction.SimplePowerShellScript">
<ScriptName>taskgetpendingreboot.ps1</ScriptName>
<Arguments>$Target/Host/Property[Type="MicrosoftWindowsLibrary7585010!Microsoft.Windows.Computer"]/NetbiosComputerName$</Arguments>
<ScriptBody> [CmdletBinding()]
Param([string]$Arguments = "Localhost")
$ScomAPI = New-Object -comObject "MOM.ScriptAPI"
$PropertyBag = $ScomAPI.CreatePropertyBag()
Try {
## Setting pending values to false to cut down on the number of else statements
$WUAURebootReq,$CompPendRen,$PendFileRename,$Pending,$SCCM = $false,$false,$false,$false,$false
$strWUAURebootReq,$strCompPendRen,$strPendFileRename,$strPending,$strSCCM = "No","No","No","No","No"
## Setting CBSRebootPend to null since not all versions of Windows has this value
$CBSRebootPend = $null
$strCBSRebootPend = "N/A"
## Querying WMI for build version
$WMI_OS = Get-WmiObject -Class Win32_OperatingSystem -Property BuildNumber, CSName -ComputerName $Arguments -ErrorAction Stop
## Making registry connection to the local/remote computer
$HKLM = [UInt32] "0x80000002"
$WMI_Reg = [WMIClass] "\\$Arguments\root\default:StdRegProv"
## If Vista/2008 & Above query the CBS Reg Key
If ([Int32]$WMI_OS.BuildNumber -ge 6001) {
$RegSubKeysCBS = $WMI_Reg.EnumKey($HKLM,"SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\")
$CBSRebootPend = $RegSubKeysCBS.sNames -contains "RebootPending"
If ($CBSRebootPend) {
$strCBSRebootPend = "Yes"
}
Else{
$strCBSRebootPend = "No"
}
}
## Query WUAU from the registry
$RegWUAURebootReq = $WMI_Reg.EnumKey($HKLM,"SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\")
$WUAURebootReq = $RegWUAURebootReq.sNames -contains "RebootRequired"
If ($WUAURebootReq) {
$strWUAURebootReq = "Yes"
}
## Query PendingFileRenameOperations from the registry
$RegSubKeySM = $WMI_Reg.GetMultiStringValue($HKLM,"SYSTEM\CurrentControlSet\Control\Session Manager\","PendingFileRenameOperations")
If (($RegSubKeySM.sValue -like '*SEP*') -or $RegSubKeySM.sValue -like '*spool*') {
}
Else{
$RegValuePFRO = $RegSubKeySM.sValue
}
## Query JoinDomain key from the registry - These keys are present if pending a reboot from a domain join operation
$Netlogon = $WMI_Reg.EnumKey($HKLM,"SYSTEM\CurrentControlSet\Services\Netlogon").sNames
$PendDomJoin = ($Netlogon -contains 'JoinDomain') -or ($Netlogon -contains 'AvoidSpnSet')
If ($PendDomJoin) {
$strPendDomJoin = "Yes"
}
## Query ComputerName and ActiveComputerName from the registry
$ActCompNm = $WMI_Reg.GetStringValue($HKLM,"SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName\","ComputerName")
$CompNm = $WMI_Reg.GetStringValue($HKLM,"SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\","ComputerName")
If (($ActCompNm -ne $CompNm) -or $PendDomJoin) {
$CompPendRen = $true
If ($CompPendRen) {
$strCompPendRen = "Yes"
}
}
## If PendingFileRenameOperations has a value set $RegValuePFRO variable to $true
If ($RegValuePFRO) {
$PendFileRename = $true
$strPendFileRename = "Yes"
$strRegValuePFRO = $RegValuePFRO
}
Else {
$strRegValuePFRO = "N/A"
}
## Determine SCCM 2012 Client Reboot Pending Status
## To avoid nested 'if' statements and unneeded WMI calls to determine if the CCM_ClientUtilities class exist, setting EA = 0
$CCMClientSDK = $null
$CCMSplat = @{
NameSpace='ROOT\ccm\ClientSDK'
Class='CCM_ClientUtilities'
Name='DetermineIfRebootPending'
ComputerName=$Arguments
ErrorAction='Stop'
}
## Try CCMClientSDK
Try {
$CCMClientSDK = Invoke-WmiMethod@CCMSplat
} Catch [System.UnauthorizedAccessException] {
$CcmStatus = Get-Service -Name CcmExec -ComputerName $Arguments -ErrorAction SilentlyContinue
If ($CcmStatus.Status -ne 'Running') {
Write-Warning "$Arguments`: Error - CcmExec service is not running."
$CCMClientSDK = $null
}
} Catch {
$CCMClientSDK = $null
}
If ($CCMClientSDK) {
If ($CCMClientSDK.ReturnValue -ne 0) {
Write-Warning "Error: DetermineIfRebootPending returned error code $($CCMClientSDK.ReturnValue)"
}
If ($CCMClientSDK.IsHardRebootPending -or $CCMClientSDK.RebootPending) {
$SCCM = $true
If ($SCCM) {
$strSCCM = "Yes"
}
}
}
Else {
$SCCM = $null
$strSCCM = "No"
}
$Pending= ($CompPendRen -or $CBSRebootPend -or $WUAURebootReq -or $SCCM -or $PendFileRename)
If ($Pending) {
$strPending = "Yes"
}
}
Finally {
write-host "Computer: " $WMI_OS.CSName
write-host "CBServicing: " $strCBSRebootPend
write-host "WindowsUpdate: " $strWUAURebootReq
write-host "CCMClientSDK: " $strSCCM
write-host "PendComputerRename: " $strCompPendRen
write-host "PendFileRename: " $strPendFileRename
write-host "PendFileRenVal: " $strRegValuePFRO
write-host "RebootPending: " $strPending
<#
write-host "Computer: " $WMI_OS.CSName.GetType().FullName
write-host "CBServicing: " $strCBSRebootPend.GetType().FullName
write-host "WindowsUpdate: " $strWUAURebootReq.GetType().FullName
write-host "CCMClientSDK: " $strSCCM.GetType().FullName
write-host "PendComputerRename: " $strCompPendRen.GetType().FullName
write-host "PendFileRename: " $strPendFileRename.GetType().FullName
write-host "PendFileRenVal: " $strRegValuePFRO.GetType().FullName
write-host "RebootPending: " $strPending.GetType().FullName
#>
# Context for alert or performance collection
$PropertyBag.AddValue("Computer",[string]$WMI_OS.CSName)
$PropertyBag.AddValue("CBServicing",[string]$strCBSRebootPend)
$PropertyBag.AddValue("WindowsUpdate",[string]$strWUAURebootReq)
$PropertyBag.AddValue("CCMClientSDK",[string]$strSCCM)
$PropertyBag.AddValue("PendComputerRename",[string]$strCompPendRen)
$PropertyBag.AddValue("PendFileRename",[string]$strPendFileRename)
$PropertyBag.AddValue("PendFileRenVal",[string]$strRegValuePFRO)
$PropertyBag.AddValue("RebootPending",[string]$strPending)
# Send output to SCOM
#$PropertyBag
}</ScriptBody>
<TimeoutSeconds>60</TimeoutSeconds>
</WriteAction>
</Task>
</Tasks>
<Monitors>
<UnitMonitor ID="UIGeneratedMonitor6fa21531da0f4660a3dd348159f87876" Accessibility="Public" Enabled="false" Target="MicrosoftWindowsLibrary7585010!Microsoft.Windows.Server.OperatingSystem" ParentMonitorID="Health!System.Health.ConfigurationState" Remotable="true" Priority="Normal" TypeID="PowerShellMonitoring!Community.PowerShellMonitoring.UnitMonitors.PowerShellTwoState" ConfirmDelivery="false">
<Category>Custom</Category>
<AlertSettings AlertMessage="UIGeneratedMonitor6fa21531da0f4660a3dd348159f87876_AlertMessageResourceID">
<AlertOnState>Warning</AlertOnState>
<AutoResolve>true</AutoResolve>
<AlertPriority>Low</AlertPriority>
<AlertSeverity>Warning</AlertSeverity>
<AlertParameters>
<AlertParameter1>$Target/Host/Property[Type="MicrosoftWindowsLibrary7585010!Microsoft.Windows.Computer"]/DNSName$</AlertParameter1>
</AlertParameters>
</AlertSettings>
<OperationalStates>
<OperationalState ID="UIGeneratedOpStateId091500b459884706b9a5a9e65eec8664" MonitorTypeStateID="Healthy" HealthState="Success" />
<OperationalState ID="UIGeneratedOpStateId08b30f50e3424eb18cce49f82e7968da" MonitorTypeStateID="Unhealthy" HealthState="Warning" />
</OperationalStates>
<Configuration>
<IntervalSeconds>300</IntervalSeconds>
<SyncTime />
<ScriptName>GetPendingReboot.ps1</ScriptName>
<ScriptBody> [CmdletBinding()]
Param([string]$Arguments = "Localhost")
$ScomAPI = New-Object -comObject "MOM.ScriptAPI"
$PropertyBag = $ScomAPI.CreatePropertyBag()
Try {
## Setting pending values to false to cut down on the number of else statements
$WUAURebootReq,$CompPendRen,$PendFileRename,$Pending,$SCCM = $false,$false,$false,$false,$false
$strWUAURebootReq,$strCompPendRen,$strPendFileRename,$strPending,$strSCCM = "No","No","No","No","No"
## Setting CBSRebootPend to null since not all versions of Windows has this value
$CBSRebootPend = $null
$strCBSRebootPend = "N/A"
## Querying WMI for build version
$WMI_OS = Get-WmiObject -Class Win32_OperatingSystem -Property BuildNumber, CSName -ComputerName $Arguments -ErrorAction Stop
## Making registry connection to the local/remote computer
$HKLM = [UInt32] "0x80000002"
$WMI_Reg = [WMIClass] "\\$Arguments\root\default:StdRegProv"
## If Vista/2008 & Above query the CBS Reg Key
If ([Int32]$WMI_OS.BuildNumber -ge 6001) {
$RegSubKeysCBS = $WMI_Reg.EnumKey($HKLM,"SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\")
$CBSRebootPend = $RegSubKeysCBS.sNames -contains "RebootPending"
If ($CBSRebootPend) {
$strCBSRebootPend = "Yes"
}
Else{
$strCBSRebootPend = "No"
}
}
## Query WUAU from the registry
$RegWUAURebootReq = $WMI_Reg.EnumKey($HKLM,"SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\")
$WUAURebootReq = $RegWUAURebootReq.sNames -contains "RebootRequired"
If ($WUAURebootReq) {
$strWUAURebootReq = "Yes"
}
## Query PendingFileRenameOperations from the registry
$RegSubKeySM = $WMI_Reg.GetMultiStringValue($HKLM,"SYSTEM\CurrentControlSet\Control\Session Manager\","PendingFileRenameOperations")
If (($RegSubKeySM.sValue -like '*SEP*') -or $RegSubKeySM.sValue -like '*spool*') {
}
Else{
$RegValuePFRO = $RegSubKeySM.sValue
}
## Query JoinDomain key from the registry - These keys are present if pending a reboot from a domain join operation
$Netlogon = $WMI_Reg.EnumKey($HKLM,"SYSTEM\CurrentControlSet\Services\Netlogon").sNames
$PendDomJoin = ($Netlogon -contains 'JoinDomain') -or ($Netlogon -contains 'AvoidSpnSet')
If ($PendDomJoin) {
$strPendDomJoin = "Yes"
}
## Query ComputerName and ActiveComputerName from the registry
$ActCompNm = $WMI_Reg.GetStringValue($HKLM,"SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName\","ComputerName")
$CompNm = $WMI_Reg.GetStringValue($HKLM,"SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\","ComputerName")
If (($ActCompNm -ne $CompNm) -or $PendDomJoin) {
$CompPendRen = $true
If ($CompPendRen) {
$strCompPendRen = "Yes"
}
}
## If PendingFileRenameOperations has a value set $RegValuePFRO variable to $true
If ($RegValuePFRO) {
$PendFileRename = $true
$strPendFileRename = "Yes"
$strRegValuePFRO = $RegValuePFRO
}
Else {
$strRegValuePFRO = "N/A"
}
## Determine SCCM 2012 Client Reboot Pending Status
## To avoid nested 'if' statements and unneeded WMI calls to determine if the CCM_ClientUtilities class exist, setting EA = 0
$CCMClientSDK = $null
$CCMSplat = @{
NameSpace='ROOT\ccm\ClientSDK'
Class='CCM_ClientUtilities'
Name='DetermineIfRebootPending'
ComputerName=$Arguments
ErrorAction='Stop'
}
## Try CCMClientSDK
Try {
$CCMClientSDK = Invoke-WmiMethod@CCMSplat
} Catch [System.UnauthorizedAccessException] {
$CcmStatus = Get-Service -Name CcmExec -ComputerName $Arguments -ErrorAction SilentlyContinue
If ($CcmStatus.Status -ne 'Running') {
Write-Warning "$Arguments`: Error - CcmExec service is not running."
$CCMClientSDK = $null
}
} Catch {
$CCMClientSDK = $null
}
If ($CCMClientSDK) {
If ($CCMClientSDK.ReturnValue -ne 0) {
Write-Warning "Error: DetermineIfRebootPending returned error code $($CCMClientSDK.ReturnValue)"
}
If ($CCMClientSDK.IsHardRebootPending -or $CCMClientSDK.RebootPending) {
$SCCM = $true
If ($SCCM) {
$strSCCM = "Yes"
}
}
}
Else {
$SCCM = $null
$strSCCM = "No"
}
$Pending= ($CompPendRen -or $CBSRebootPend -or $WUAURebootReq -or $SCCM -or $PendFileRename)
If ($Pending) {
$strPending = "Yes"
}
}
Finally {
<#
write-host "Computer: " $WMI_OS.CSName
write-host "CBServicing: " $strCBSRebootPend
write-host "WindowsUpdate: " $strWUAURebootReq
write-host "CCMClientSDK: " $strSCCM
write-host "PendComputerRename: " $strCompPendRen
write-host "PendFileRename: " $strPendFileRename
write-host "PendFileRenVal: " $strRegValuePFRO
write-host "RebootPending: " $strPending
write-host "Computer: " $WMI_OS.CSName.GetType().FullName
write-host "CBServicing: " $strCBSRebootPend.GetType().FullName
write-host "WindowsUpdate: " $strWUAURebootReq.GetType().FullName
write-host "CCMClientSDK: " $strSCCM.GetType().FullName
write-host "PendComputerRename: " $strCompPendRen.GetType().FullName
write-host "PendFileRename: " $strPendFileRename.GetType().FullName
write-host "PendFileRenVal: " $strRegValuePFRO.GetType().FullName
write-host "RebootPending: " $strPending.GetType().FullName
#>
# Context for alert or performance collection
##$PropertyBag.AddValue("Computer",[string]$WMI_OS.CSName)
$PropertyBag.AddValue("CBServicing",[string]$strCBSRebootPend)
$PropertyBag.AddValue("WindowsUpdate",[string]$strWUAURebootReq)
$PropertyBag.AddValue("CCMClientSDK",[string]$strSCCM)
$PropertyBag.AddValue("PendComputerRename",[string]$strCompPendRen)
$PropertyBag.AddValue("PendFileRename",[string]$strPendFileRename)
##$PropertyBag.AddValue("PendFileRenVal",[string]$strRegValuePFRO)
$PropertyBag.AddValue("RebootPending",[string]$strPending)
# Send output to SCOM
$PropertyBag
##$ScomAPI.Return($PropertyBag)
}
</ScriptBody>
<Arguments>$Target/Host/Property[Type="MicrosoftWindowsLibrary7585010!Microsoft.Windows.Computer"]/NetbiosComputerName$</Arguments>
<TimeoutSeconds>60</TimeoutSeconds>
<UnhealthyExpression>
<SimpleExpression>
<ValueExpression>
<XPathQuery>Property[@Name='RebootPending']</XPathQuery>
</ValueExpression>
<Operator>Equal</Operator>
<ValueExpression>
<Value Type="String">Yes</Value>
</ValueExpression>
</SimpleExpression>
</UnhealthyExpression>
<HealthyExpression>
<SimpleExpression>
<ValueExpression>
<XPathQuery>Property[@Name='RebootPending']</XPathQuery>
</ValueExpression>
<Operator>Equal</Operator>
<ValueExpression>
<Value Type="String">No</Value>
</ValueExpression>
</SimpleExpression>
</HealthyExpression>
</Configuration>
</UnitMonitor>
</Monitors>
<Overrides>
<MonitorPropertyOverride ID="Alias412b243bff814093a1df6415f56528bcOverrideForMonitorUIGeneratedMonitor6fa21531da0f4660a3dd348159f87876ForContextMicrosoftWindowsServerOperatingSystem" Context="MicrosoftWindowsLibrary7585010!Microsoft.Windows.Server.OperatingSystem" Enforced="false" Monitor="UIGeneratedMonitor6fa21531da0f4660a3dd348159f87876" Property="Enabled">
<Value>true</Value>
</MonitorPropertyOverride>
</Overrides>
</Monitoring>
<Presentation>
<Folders>
<Folder ID="Folder_02e878bf601a4064875be2bfb13c2a26" Accessibility="Public" ParentFolder="SystemCenter!Microsoft.SystemCenter.Monitoring.ViewFolder.Root" />
</Folders>
<StringResources>
<StringResource ID="UIGeneratedMonitor6fa21531da0f4660a3dd348159f87876_AlertMessageResourceID" />
</StringResources>
</Presentation>
<LanguagePacks>
<LanguagePack ID="ENU" IsDefault="false">
<DisplayStrings>
<DisplayString ElementID="BKLPRODReboot">
<Name>BKL-PROD-Reboot</Name>
</DisplayString>
<DisplayString ElementID="Folder_02e878bf601a4064875be2bfb13c2a26">
<Name>BKL-PROD-Reboot</Name>
</DisplayString>
<DisplayString ElementID="UIGeneratedMonitor6fa21531da0f4660a3dd348159f87876">
<Name>Pending Reboot</Name>
</DisplayString>
<DisplayString ElementID="UIGeneratedMonitor6fa21531da0f4660a3dd348159f87876_AlertMessageResourceID">
<Name>Pending Reboot</Name>
<Description>The system {0} has a reboot pending.</Description>
</DisplayString>
<DisplayString ElementID="UIGeneratedMonitor6fa21531da0f4660a3dd348159f87876" SubElementID="UIGeneratedOpStateId091500b459884706b9a5a9e65eec8664">
<Name>Healthy</Name>
</DisplayString>
<DisplayString ElementID="UIGeneratedMonitor6fa21531da0f4660a3dd348159f87876" SubElementID="UIGeneratedOpStateId08b30f50e3424eb18cce49f82e7968da">
<Name>Unhealthy</Name>
</DisplayString>
<DisplayString ElementID="ConsoleTaskGeneratedByUI8980d9d7989343879312173f5ce8dbe0">
<Name>Get Pending Reboot</Name>
</DisplayString>
</DisplayStrings>
</LanguagePack>
<LanguagePack ID="ENA" IsDefault="false">
<DisplayStrings>
<DisplayString ElementID="UIGeneratedMonitor6fa21531da0f4660a3dd348159f87876" SubElementID="UIGeneratedOpStateId091500b459884706b9a5a9e65eec8664">
<Name>Healthy</Name>
</DisplayString>
<DisplayString ElementID="UIGeneratedMonitor6fa21531da0f4660a3dd348159f87876" SubElementID="UIGeneratedOpStateId08b30f50e3424eb18cce49f82e7968da">
<Name>Unhealthy</Name>
</DisplayString>
<DisplayString ElementID="ConsoleTaskGeneratedByUI8980d9d7989343879312173f5ce8dbe0">
<Name>Get Pending Reboot</Name>
</DisplayString>
</DisplayStrings>
</LanguagePack>
</LanguagePacks>
</ManagementPack>