Welcome › Forums › General PowerShell Q&A › Filter by computer name then find last Windows client update
- This topic has 4 replies, 2 voices, and was last updated 6 months, 1 week ago by
Participant.
-
AuthorPosts
-
-
July 2, 2020 at 11:03 am #239837
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.
PowerShell123Get-ADComputer -filter * | Where-Object {$_.Name -like "abcad*"} | select -ExpandProperty Nameforeach{Get-HotFix -ComputerName $_ | Sort-object InstalledOn -Descending | Select-object -first 1}Any assistance would be appreciated!
Thanks
John
-
July 2, 2020 at 11:43 am #239843
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. 😉
Something like this should run actually
PowerShell12345678$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.
-
July 5, 2020 at 5:00 pm #240449
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!
-
July 10, 2020 at 8:59 am #241529
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:
PowerShell1234567$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}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 : CommandNotFoundExceptionI would be grateful for any assistance.
Many thanks!
JF
-
July 10, 2020 at 9:21 am #241538
-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.
-
-
AuthorPosts
- The topic ‘Filter by computer name then find last Windows client update’ is closed to new replies.