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 "
        }

}

asked 12 Oct, 14:26

poweradminmj's gravatar image

poweradminmj
1
accept rate: 0%


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.

link

answered 12 Oct, 15:44

Quinn's gravatar image

Quinn ♦♦
14.3k3925
accept rate: 36%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×34
×15
×10
×1

Asked: 12 Oct, 14:26

Seen: 164 times

Last updated: 12 Oct, 15:44