1

I'm currently in the process of upgrading all Windows 2012R2 and 2016 servers to 2019. I would like to run a Powershell cmdlet that will get all the computers on the domain that are powered on, and running these operating systems, as well as their hostnames and ip addresses. I have used Get-Computer in the past and got a long list of servers that had been turned off already, may have been using the wrong parameters, however would like something a bit cleaner and specific.

1
  • 3
    Run Get-Computer, store it in a variable, then send that in a foreach loop to test-connection to each computer. This will narrow it down to the systems that respond.
    – Davidw
    Jul 27 at 21:17

1 Answer 1

4

Here's an example command for building a list of online computers matching a specific filter:

Get-AdComputer -Filter {OperatingSystem -like 'Windows Server*' -and DNSHostName -like '*' -and Enabled -eq $true} -SearchBase "OU=Servers,DC=Contoso,DC=com" -Properties operatingSystem | Sort-Object DNSHostName | ForEach-Object { if(Test-Connection -ComputerName $_.DNSHostName -Count 1 -Quiet){$Computers += @($_.DNSHostName);Write-Output "$($_.DNSHostName) is available and added to the array"} else {Write-Warning "$($_.DNSHostName) is unreachable"}}

You can then use the resulting $Computers array to run additional commands against those computers:

Invoke-Command -ComputerName $Computers -ScriptBlock {Get-Service -Name Spooler | Select-Object DisplayName,Name,StartType,Status -ErrorAction SilentlyContinue} | Format-Table PSComputerName,DisplayName,Name,StartType,Status

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .