Script to get Patch information for a particular KB

Ajoy Rajan
Ajoy Rajan used Ask the Experts™
on
Hi,

I am trying to get a powershell script to get information from a list of servers about a particular KB installed.

I want to export the information to a CSV format.

Regards,

Ajoy
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Try this; errors when accessing a remote server will be logged directly in the csv in the column 'Exception'.
$hotFixID = 'KB0000000'
$outFile = 'C:\Temp\Hotfix.csv'
$computerList = Get-Content -Path 'C:\Temp\serverlist.txt'

$properties = 'PSComputerName', 'HotFixID', 'Installed', 'Description', 'InstalledBy', 'InstalledOn', 'Exception'
$computerList | ForEach-Object {
	Write-Host "Processing $($_) ..."
	Try {
		$out = $exception = $null 
		$out = Get-WmiObject -Query "Select * From Win32_QuickFixEngineering Where HotFixID='$($hotFixID)'" -ComputerName $_ -ErrorAction Stop | Select-Object -Property $properties
	} Catch {
		$exception = $_.Exception.Message 
	}
	If ($out) {
		$out.Installed = $true
	} Else  {
		$out = '' | Select-Object -Property $properties
		$out.PSComputerName = $_
		$out.HotFixID = $hotFixID
		If ($exception) {
			$out.Exception = $exception
		} Else {
			$out.Installed = $false
		}
	}
	$out
} | Export-Csv -NoTypeInformation -Path $outFile

Open in new window

Ajoy RajanManaged Service Consultant

Author

Commented:
Hi oBdA,

I tried the script, I am getting the error:

Exception setting "Exception": "The property 'Exception' cannot be found on this object. Verify that the
property exists and can be set."
At C:\Temp\KB499175-Patchlookup-Final.ps1:22 char:4
+             $out.Exception = $exception
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting

Sorry I am basic with powershell.
Vikas BhatExperienced IT Infrastructure Services/operations Manager

Commented:
Try this Powershell script to query a particular patch is installed on remote computers

https://gallery.technet.microsoft.com/scriptcenter/Powershell-Query-a-patch-67cf35f8
How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
The line number in your error doesn't match what I posted above.
Please download the script from the code box above again (use the "Select All" link below the box); save as Get-Hotfix.ps1 or Whatever.ps1.
Change only lines 1-3 according to your settings.
Then just run as .\Get-Hotfix.ps1 from a PS console.
Ajoy RajanManaged Service Consultant

Author

Commented:
I have tried this, the problem is that I want to run this from a server which has access to all other servers and do not have excel installed. This script needs excel application. This scripts calls out the excel application.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Note: if you only have a small list of servers, or for testing, you can define the server names directly in the script instead of using an input file; just set $computerList to a list of names instead of "Get-Content ..."
$computerList = 'Server1', 'Server2', 'Server3'

Open in new window

Ajoy RajanManaged Service Consultant

Author

Commented:
Hi oBdA,

The list is quite big. Servers are nearly 100-175.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
That's why I said "or for testing" ;)
For a first test, you can set the list to one server where you know the patch to be installed, one server where you know the patch to be not installed, and one server that does not exist.
Vikas BhatExperienced IT Infrastructure Services/operations Manager

Commented:
$OutputFileLocation = "C:\temp\Logs-$(get-date -uformat '%Y-%m-%d-%H_%M').log" 
  
 ###InputLocation 
 $Computers = Get-Content "c:\temp\computers.txt" 
 # Enter KB to be checked here 
 $Patch = Read-Host 'Enter the KB number? '
  

 
 

 
foreach ($Computer in $Computers) 
 {
 

 $OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computer 
 $sheetS = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Computer 
 $sheetPU = Get-WmiObject -Class Win32_Processor -ComputerName $Computer 
 $drives = Get-WmiObject -ComputerName $Computer Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3} 
 $pingStatus = Get-WmiObject -Query "Select * from win32_PingStatus where Address='$Computer'" 
 $OSRunning = $OS.caption + " " + $OS.OSArchitecture + " SP " + $OS.ServicePackMajorVersion 
 $systemType=$sheetS.SystemType 
 $date = Get-Date 
 $uptime = $OS.ConvertToDateTime($OS.lastbootuptime) 
   
 if  
 ($kb=$(try {get-hotfix -id $Patch -ComputerName $computer} catch {$null}))
 
 { 
 Write-Output "$patch is installed $Computer" | Out-File $OutputFileLocation -Append
 } 
 else 
 { 
 Write-Output "$patch is not installed on $Computer" | Out-File $OutputFileLocation -Append 
 } 
 
  
 } 
  
 
$erroractionpreference = “SilentlyContinue”

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Just to be sure, I copied my script form the code box above back, changed only $computerList and $hotFixID, and it worked as it should:
PS C:\PS> .\Get-HotfixReport.ps1
Processing server01 ...
Processing foo ...
Processing server02 ...
PS C:\PS> Import-Csv C:\Temp\Hotfix.csv | select PSComputerName, HotfixID, Installed, Exception | ft -au

PSComputerName HotFixID  Installed Exception
-------------- --------  --------- ---------
server01       KB2778344 True
foo            KB2778344           The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
server02       KB2778344 False

Open in new window

Ajoy RajanManaged Service Consultant

Author

Commented:
The script works mate. Thanks for the help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial