This is a quick, simple post. The requirement is simple. Get a report from all the computers in the network reporting the last Windows update date, Windows update installed KB, and the Windows uptime.
The script gets the content from a file containing all the computer names as an array and saves the result on the user’s desired destination.
To use the script, use the following format.
MyScript.ps1 -FilePath C:\MyComputerlist.txt -SaveTo C:\SaveResult.csv
The result is a CSV file that
Here is the script. Feel free to use it, and let me know if you recommend any updates.
Param
(
# File Path
[Parameter(Mandatory=$true)]
$FilePath,
[parameter(mandatory=$true)]
$SaveTo
)
$Computers = Get-Content -Path $FilePath
$Result=@()
Foreach ($Computer in $Computers) {
$ResultObject=[PSCustomObject]@{
'Server Name'=''
'Uptime'=''
'Days Since Last Patch'=''
'Last Update KB'=''
'Last Patch Date & Time'=''
}
Try{
Write-host "Processing $($computer)"
$ResultObject.'Server Name' = $Computer
# Get Uptime
Write-host "Processing Uptime for $($computer)"
$uptime = (Get-CimInstance -Class Win32_OperatingSystem -ComputerName $computer -ErrorAction Stop).LastBootUpTime
$ResultObject.Uptime = ((Get-Date) - $uptime).days
# Get Last Patch Date & Time
Write-host "Processing Last patch for $($computer)"
$lastPatch = Get-CimInstance -Class Win32_QuickFixEngineering -ComputerName $computer -ErrorAction Stop| Sort-Object -Property InstalledOn -Descending | Select-Object -First 1
$ResultObject.'Last Patch Date & Time' = ($lastPatch.InstalledOn).Date.ToString("dd/MM/yyy")
$ResultObject.'Last Update KB'=$lastPatch.HotFixID
# Get Days Since Last Patch
Write-host "Processing Last Days between patch for $($computer)"
$Dateforpatch=(Get-Date) - $lastPatch.InstalledOn
$ResultObject.'Days Since Last Patch' =($dateforpatch).Days
$Result+=$ResultObject
}
Catch{
Write-Host "Ops, Error for $($computer)"
$Result+=$ResultObject
$Error[0]
}
}
$result
$result | Export-Csv $SaveTo -NoTypeInformation
I hope this script helps. There will be more scripts like that. Let me know if you have any ideas in the comment section below.
That’s it for today; take a look at the following post. You will love it.
Hi Faris,
Some mistakes in your script
– Try … catch statement : you must use -ErrorAction Stop for the cmdlets un the Try section, otherwise (in case of error), this never pass to the Catch section.
– Forget Get-WMIObject cmdlet (deprecated) : Use Get-CimInstance.
Other (advice) :
– Transform your code to a advanced function (Attention point : a function do something with input and return something else in a output). Code re-use. After that, in a script :
– Loading function
– Gathering Input (servers list) for the function (Get-content file, import-csv, query AD, …)
– Using function against servers list and put the result in a var
– Display output in console $Output | format-Table
– Other use like $Output | Export-Csv …
regards
Oliv,
Thanks for the comment, You are correct about the mistake point. I updated them “This happens when writing a script in 1 min :)”
Regarding the other point, I always do this with all the script and the module I wrote, but in this post, I thought of making everything shorter and quicker for users who want to copy and paste, and also making the script much easier for users who don’t know certain things in powershell such as functions… This is why the name is QuickScript 🙂