Object reference not set to an instance of an object. [13-Mon]

Viewed 0

Windows Server 2012 R2 - error consistent with three other VMs but many other vms are working with the same script Remote RM enabled and able to ping the box from the PA Monitor Server

$alert_output = "" $Updates = "" $UpdateSession = "" $UpdateSearcher = "" $Updatesresult = "" $Count = "" $Update = "" $Title = "" $KB = "" $IsDownloaded = "" $updatehistory = "" $updatehistorycount = "" $fwAlert = 0 $systename = $mon.ComputerName $session = New-PSSession -ComputerName "$systename" $sesoutput = $session.Availability if($session.Availability -ne "Available") { $mon.FireActions = $true $mon.Details = "Cannot connect" } else { $systenamecheck = Invoke-Command { $systename = $env:computername Return $systename } -Session $session $osversion = Invoke-Command { $osversion = [System.Environment]::OSVersion.Version Return $osversion } -Session $session
$Updatescount = Invoke-Command { $UpdateSession = New-Object -ComObject Microsoft.Update.Session $UpdateSearcher = $UpdateSession.CreateupdateSearcher() $Updates = @($UpdateSearcher.Search("IsHidden=0 and IsInstalled=0").Updates) $Updatesresult = $Updates | Select-Object -Property Title | out-string; $Updatescount = $Updates.count Return $Updatescount } -Session $session

if($Updatescount -gt 0)
{
    $fwAlert = 1
    $pending_output = Invoke-Command {
            $alert_output = ""
            $systename = $env:computername
                            $UpdateSession = New-Object -ComObject Microsoft.Update.Session
            $UpdateSearcher = $UpdateSession.CreateupdateSearcher()
            $Updates = @($UpdateSearcher.Search("IsHidden=0 and IsInstalled=0").Updates)
            $Updatesresult = $Updates | Select-Object -Property Title | out-string;
            $Updatescount = $Updates.count
            For ($i=0; $i -lt $Updatescount; $i++) 
            {
                $Update  = $Updates.Item($i)
                $Title =  $Update.Title
                $KB =  $Update.KBArticleIDs
                $MsrcSeverity = $Update.MsrcSeverity
                $IsDownloaded = $Update.IsDownloaded
                $Categories =  ($Update.Categories  | Select-Object  -ExpandProperty Name)
                $alert_output = $alert_output + "$i-$systename-$KB-$Categories-$Title-$IsDownloaded `n" 
            } 
    Return $alert_output                    
    } -Session $session
    $history_output = Invoke-Command {
        $alert_output = "`nUpdate History (Last 90 days) `n" 
        $updatehistory =  Get-HotFix | Where-Object {$_.InstalledOn -gt ((Get-Date).AddDays(-90))} |Sort-Object -Property @{Expression = "InstalledOn"; Descending = $True} 
        For ($j=0; $j -lt $updatehistory.count; $j++) 
        {
            $uh  = $updatehistory[$j]
            $Title =  $uh.Title
            $installedon = $uh.InstalledOn 
            $HotFixID = $uh.HotFixID
            $Description = $uh.Description
            $alert_output = $alert_output + "Date: " + $installedon + "   Description: " + $Description + "   HotFixID: " + $HotFixID + "`n"
        }
    Return $alert_output                    
    } -Session $session

}
else
{
    $fwAlert = 0
}

if($fwAlert -eq 1)
        {
            $mon.FireActions = $fwAlert 
            $mon.Details = "ALERT-$systenamecheck `n`r PENDING UPDATES $Updatescount`n`r$pending_output `n`r $history_output" 
        }
        else
        {
            $mon.FireActions = $fwAlert 
            $mon.Details = "Healthy-$systenamecheck-NO UPDATES $Updatescount "
        }

}

1 Answers

Hi poweradminmj,

The error "Object reference not set to an instance" is a command error and most of the time it means that some variable or function’s return value is null. Please check your code to see if you can check for null.

Another option to all this would be to use the Inventory Collector monitor. One of the features of this monitor is to get a count of Updates needing to be installed. You can then use the Inventory Alerter monitor to fire alerts based on the collectors data.

Thanks
Quinn

Please make sure to mark your questions accepted when you have your answer by clicking the gray checkmark to the left of the answer.

Related