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
LVL 1
Ajoy RajanManaged Service ConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
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 ConsultantAuthor 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 ManagerCommented:
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
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

oBdACommented:
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 ConsultantAuthor 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.
oBdACommented:
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 ConsultantAuthor Commented:
Hi oBdA,

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

oBdACommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ajoy RajanManaged Service ConsultantAuthor Commented:
The script works mate. Thanks for the help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.