In the past I had done something like:
- Get the list of computers I need to run against from Active Directory or a csv file.
- For each computer, attempt to ping it and if it responds run ye’ commands.
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 | $adComputers = Get-QADComputer -SearchRoot 'domain.com/OU/OU' ` -SizeLimit 0 ` -IncludedProperties lastlogontimestamp | Where-Object ` { (($_.useraccountcontrol -band 0x2) -eq 0) ` -and ($_.lastlogontimestamp -ne $null) ` -and (($_.lastlogontimestamp).adddays(30) -gt (Get-Date)) } $compNames = $adComputers | % { $_.name } $pingResults = ping $compNames -Count 2 -Quiet ` -AllAddresses -Asynchronous -ErrorAction silentlycontinue $compDict = @{} foreach ($comp in $adComputers) { $compDict.add($comp.name,$comp) } $upComps = @() foreach ($result in $pingResults) { if ($result.received -gt 1) { $cName = $result.hostname -replace "\.domain\.com" $upComps += $compDict[$cName] } } |
From there we can use PSRemoting or tools like psexec.exe with the –d switch to (relatively) quickly run a set of commands against our computers that are up and running!
Many thanks to Lee Holmes for the script that creates the above code block on the clipboard for pasting from any script, truly awesome.