Link to home
Start Free TrialLog in
Avatar of Ajoy Rajan
Ajoy RajanFlag for Australia

asked on

Script to get Patch information for a particular KB

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
Avatar of oBdA
oBdA

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

Avatar of Ajoy Rajan

ASKER

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.
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
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.
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.
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

Hi oBdA,

The list is quite big. Servers are nearly 100-175.
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.
$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

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The script works mate. Thanks for the help.