
close
close
Chance to win $250 in Petri 2023 Audience Survey
I think I’m long overdue in giving you an example of PowerShell in action. A few months ago, I followed a thread in a discussion forum about finding installed hot fixes and creating some type of report. Instead of jumping to the finished product, let’s spend some time going through the process to arrive at the final solution, or at least what I’m using. This process is just as important to learn and understand as the final result. We’ll be working with installed hot fixes, but it could be just about anything you need to manage.
To begin, we need a basic requirement or business need: “Find installed hot fixes on a remote computer.” In the forum, I believe the poster already knew what command to work with, but let’s say he didn’t. The first thing to do is ask PowerShell for help.
help hotfix
I’m not saying you will get a result the first time, although in this case, you’ll see there is a cmdlet, Get-HotFix that we can use. Sometimes you need to try a variety of terms and maybe even use wildcard characters. Once you have a basic cmdlet, read the complete help and examples.
With Get-HotFix, you can get a specific patch by its ID number or all of them. Since commands should work just fine locally, you should first test on your computer if you can.
Running Get-Hotfix locally (Image Credit: Jeff Hicks)
Testing Get-Hotfix remotely (Image Credit: Jeff Hicks)
Viewing all hotfix properties (Image Credit: Jeff Hicks)
get-hotfix -ComputerName chi-web02 | select -first 5 -Property PSComputername,HotFixID,Description,InstalledBy,InstalledOn,Caption
Running this code verifies I get the information I want.
Getting select hotfix properties (Image Credit: Jeff Hicks)
get-hotfix -ComputerName chi-web02 | select -Property PSComputername,HotFixID,Description,InstalledBy,InstalledOn,Caption | Export-CSV -Path c:\work\chi-web02.csv -NoTypeInformation
Or formatted as a nice report.
get-hotfix -ComputerName chi-web02 | Sort Description,HotFixID | Format-Table -GroupBy Description -Property PSComputername,HotFixID,InstalledBy,InstalledOn,Caption
Formatted HotFix Report (Image Credit: Jeff Hicks)
Retesting with a different computername (Image Credit: Jeff Hicks)
Checking Get-Hotfix help (Image Credit: Jeff Hicks)
Testing multiple computer names (Image Credit: Jeff Hicks)
#requires -version 3.0 #Basic-HotFixReport.ps1 Param([string[]]$Computername = $env:COMPUTERNAME) Get-Hotfix -ComputerName $Computername | Select-Object -Property PSComputername,HotFixID,Description,InstalledBy,InstalledOn, @{Name="Online";Expression={$_.Caption}}
Note that the script doesn’t do any formatting or exporting. All we want it to do is write objects to the pipeline.
Testing the basic script (Image Credit: Jeff Hicks)
C:\scripts\Basic-HotfixReport.ps1 -Computername chi-p50,chi-test02,chi-web02 | out-gridview -title "Report"
Testing with multiple computer names (Image Credit: Jeff Hicks)
More in PowerShell
Most popular on petri