# Modifying PowerShell script for listing latest windows update on each servers

**URL:** <https://forums.powershell.org/t/modifying-powershell-script-for-listing-latest-windows-update-on-each-servers/5594>\
**Category:** PowerShell Help\
**Created:** [January 24, 2016, 11:31am UTC](https://forums.powershell.org/t/modifying-powershell-script-for-listing-latest-windows-update-on-each-servers/5594 "2016-01-24T11:31:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![albertwt](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/albertwt/32/2550_2.png) [@albertwt](https://forums.powershell.org/u/albertwt)\
**Post date:** [January 24, 2016, 11:31am UTC](https://forums.powershell.org/t/modifying-powershell-script-for-listing-latest-windows-update-on-each-servers/5594/1 "2016-01-24T11:31:37Z")

</div>

People,

I’ve come up with this script below to perform:

Grab the content of text file for list of server name  
Test / check if the server is online, if it is not, then write the error in $ErrorLog = “C:\TEMP\retry.txt”  
For all online server grab the HotfixID, Computername, InstalledOn, InstalledBy, OSVersionattributes  
Export to CSV file as the result.  
Here’s the script that I can come up so far with the way to call the function:

function Get-PatchLevel1{  
#[CmdletBinding()]

# delete or edit select-object 1 in the hotfix parameter to scope the output with specific hotfix number

param (  
$serverlist = @(),  
[string]$ErrorLog = “C:\TEMP\retry.txt”,  
[switch]$LogErrors  
)  
$serverlist |  
% {  
$server = $\_  
If(Test-Connection -quiet -computername $server) {  
$ADC= Get-ADComputer $server –Property OperatingSystem  
$hotfix = Get-HotFix -ComputerName $server |  
#where-object {$_.hotfixid -ne “KB3116900”} |  
Select hotfixid, description, installedby, @{label=“InstalledOn”;e={[DateTime]::Parse($_.psbase.properties[“installedon”].value, $([System.Globalization.CultureInfo]::GetCultureInfo(“en-AU”)))}} |  
Sort-Object -Property InstalledOn |  
Select-Object -Last 1

```
  New-Object PSObject -Property @{
    Computername = $server
    OSVersion = $ADC.OperatingSystem
    Hotfix = $hotfix.HotfixID
    InstalledOn = $hotfix.InstalledOn
	InstalledBy = $hotfix.installedby
  }
}

```

} | Export-Csv C:\TEMP\PatchLevelAdServers1.csv -NoTypeInformation -UseCulture  
}

And this is how I call the function:

Get-PatchLevel1 -serverlist “TESTAPPS01-VM”, “DEVSQL02-VM”, “PRODDC02-VM”, “PRODNAS05-VM”

However, there are some issues here:

1. The offline server is not written to the $ErrorLog = “C:\TEMP\retry.txt”
2. When compared with the Control Panel\System and Security\Windows Update\View update history the result is not the same thus confusing me ?

Any kind of help would be greatly appreciated.

Thanks,

---

<div class="post-metadata">

**Author:** ![vincent-k](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@vincent-k](https://forums.powershell.org/u/vincent-k)\
**Post date:** [January 24, 2016, 7:55pm UTC](https://forums.powershell.org/t/modifying-powershell-script-for-listing-latest-windows-update-on-each-servers/5594/2 "2016-01-24T19:55:48Z")

</div>

U didn’t use Else Statement

Try this  
New-Object PSObject -Property @{  
Computername = $server  
…  
InstalledBy = $hotfix.installedby  
} | Export-Csv C:\TEst\PatchLevelAdServers1.csv -NoTypeInformation -UseCulture -Append  
} Else { Write-Output $server | Out-File $ErrorLog -Append }  
}#Foreach  
}#Function

---

<div class="post-metadata">

**Author:** ![albertwt](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/albertwt/32/2550_2.png) [@albertwt](https://forums.powershell.org/u/albertwt)\
**Post date:** [January 26, 2016, 11:12am UTC](https://forums.powershell.org/t/modifying-powershell-script-for-listing-latest-windows-update-on-each-servers/5594/3 "2016-01-26T11:12:47Z")

</div>

Vincent,

Thanks for the reply.

That does the trick for #1. What about number 2 ?

The script still returns all result of the patch installed, I just need to get the latest patch that was installed on the servers.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:43pm UTC](https://forums.powershell.org/t/modifying-powershell-script-for-listing-latest-windows-update-on-each-servers/5594/4 "2024-05-16T20:43:03Z")

</div>


