Filter by computer name then find last Windows client update

Hi!

I am very new to powershell so apologies if this question is noobish.

In a domain I am attempting to filter by computer name then show for each the last update for each filtered Windows 10 client.

The filtering part seems to function, but interogating each to get the last update is wrong.

Get-ADComputer -filter * | Where-Object {$_.Name -like "abcad*"} | select -ExpandProperty Name
foreach{Get-HotFix -ComputerName $_ | Sort-object InstalledOn -Descending | Select-object -first 1
}

Any assistance would be appreciated!

Thanks

John

John, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

When you post code, error messages, sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE”, in the “Visual” view you can use the format template “Preformatted”. You can go back edit your post and fix the formatting - you don’t have to create a new one.
Thanks in advance.

To reduce the stress you put on your DC with a query like this I urgently recommend using the -SearchBase parameter for Get-ADUser. It would also be faster when you use the -Filter parameter of Get-ADUser instead of filtering the results provided by it later with a Where-Object.

The actual mistake you did - you forgot to pipe the output of “select -ExpandProperty Name” to Foreach-Object. :wink:

Something like this should run actually

$SearchBase = 'OU=Computers,OU=Departement,DC=contoso,DC=com'

Get-ADComputer -Filter "Name -like 'abcad*'" -SearchBase $SearchBase |
ForEach-Object {
    Get-HotFix -ComputerName $_.Name |
    Sort-Object -Property InstalledOn |
    Select-Object -Last 1
}

… untested. I don’t have an environment to test at the moment.

And just for the sake of completeness - all computers you want to query that way have to be switched on and reachable on the network.

Many Thanks Olaf!

I will follow your advice and aim to get it working in the coming week. I will report back what happens.

 

Thanks again for your time!

Hi

Further to the earlier post, I’ve been attempting to filter by computer name then show for each the last update for each filtered Windows 10 client using this script:

[pre]

$SearchBase = “OU=PHS,OU=RSS,OU=Computers,DC=org,DC=uk”
Get-ADComputer -Filter “Name -like ‘abcad*’” | -SearchBase $SearchBase |
ForEach-Object {
Get-HotFix -ComputerName $_.Name |
Sort-Object -Property InstalledOn -Descending |
Select-Object -first 1
}
[/pre]

 

In response I am getting the following error message:

-SearchBase : The term ‘-SearchBase’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
At line:1 char:47
+ Get-ADComputer -Filter “Name -like ‘abcad*’” | -SearchBase $SearchBase |
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (-SearchBase:String) , CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

I would be grateful for any assistance.

Many thanks!

JF

-SearchBase is parameter for the cmdlet Get-ADUser. Please always read the complete help including the examples for the cmdlets you’re about to use to learn how to use them.