I'd like to get the latest update installed and the OS version of a list of computers

Hi,

I’m new to the forum and pretty noob in regards to PowerShell.

I need to retrieve the following information from a list of servers:

  • the OS version
  • the latest update installed

I found the following scripts:

Get-Content "C:\Temp\Computers.txt"  | ForEach-Object{
$os_name=$null
$os_version=$null
$errorMsg=''
Try
{
$os_name = (Get-WmiObject Win32_OperatingSystem -ComputerName $_ ).Caption
}
catch
{
$errorMsg=$_.Exception.Message
}
if(!$os_name){
$os_name = "The machine is unavailable $errorMsg"
$os_version = "The machine is unavailable"
}
else{
$os_version = (Get-WmiObject Win32_OperatingSystem -ComputerName $_ ).Version 
}
New-Object -TypeName PSObject -Property @{
ComputerName = $_
OSName = $os_name
OSVersion = $os_version 
}} | Select ComputerName,OSName,OSVersion | 
Export-Csv "C:\Temp\OS_Details.csv" -NoTypeInformation -Encoding UTF8

and

(Get-HotFix | Sort-Object -Property InstalledOn)[-1]

How can i get those two scripts to be one and have all the information in the outpule CSV file formatted like:

ComputerName - OSName - OSVersion - Latest update installed?

Thanks in advance for your help. :slight_smile:

Dominique,
Welcome to the forum. :wave:t4:

You can use a PSCustomObject to combine the output of two or more commands into one result.

$ServerList = Get-Content -Path 'C:\Temp\Computers.txt'
$Result = 
foreach ($ComputerName in $ServerList) {
    Invoke-Command -ComputerName $ComputerName -ScriptBlock {
        $OS = Get-CimInstance -ClassName CIM_OperatingSystem
        $HF = Get-HotFix | Sort-Object -Property InstalledOn | Select-Object -Last 1
        [PSCustomObject]@{
            ComputerName = $ENV:COMPUTERNAME
            OS           = $OS.Caption
            Version      = $OS.Version
            LastHotfix   = $HF.HotFixID
        }
    }
}
$Result | Select-Object -Property ComputerName, OS, Version, LastHotFix

Since Get-HotFix only returns the install date and not the install time you might not really get latest installed updates.

Hello Olaf!
Thanks for your answer.
When I run this command: (Get-HotFix | Sort-Object -Property InstalledOn)[-1], I have the installation date:

Do you know if there a way to have this value?
Also, can I add a pipe to have the results in a file?

Thank you very much!

Do you want only the date or additionally? :wink:

I understood from your initial post …

“What’s the latest update what’s been installed on the computer” and not “when was the lastest update installed” :wink:

… and of course you can have anthing what’s returned by the command …

When you run the following command on your computer …

$HF = Get-HotFix | Sort-Object -Property InstalledOn | Select-Object -Last 1

You can get the HotFixID with the command

$HF.HotFixID

… as shown in my code suggestion. I’m sure you have an idea how to get the property “InstalledOn” instead of “HotFixID”. :point_up_2:t4: :wink:

Sure. You pipe the last command to whatever output cmdlet you like. Probably Export-Csv would make the most sense.

You understand well, it’s me who did not express myself correctly. Sorry for that. I would like to have the installation date too actually.

Again thanks for your help, I’ll try this out in a bit. :slight_smile:

If you get stuck do not hesitate to come back. We all have been beginners once. :wink: