0

I want to get all of the computers in a specific OU and ping them, but Im having trouble with Get-ADComputer.

code:

# Enter CSV file location
$csv = "filepath.csv"
# Add the target OU in the SearchBase parameter
$Computers = Get-ADComputer -Filter * -SearchBase "OU=Servers,DC=mydomain,DC=com" | Select Name | Sort-Object Name
$Computers = $Computers.Name
$Headers = "ComputerName,IP Address"
$Headers | Out-File -FilePath $csv -Encoding UTF8
foreach ($computer in $Computers)
{
    Write-host "Pinging $Computer"
    $Test = Test-Connection -ComputerName $computer -Count 1 -ErrorAction SilentlyContinue -ErrorVariable Err
    if ($test -ne $null)
    {
        $IP = $Test.IPV4Address.IPAddressToString
        $Output = "$Computer,$IP"
        $Output | Out-File -FilePath $csv -Encoding UTF8 -Append
    }
    Else
    {
        $Output = "$Computer,$Err"
        $output | Out-File -FilePath $csv -Encoding UTF8 -Append
    }
    cls
}

and im getting:

Get-ADComputer : The object name has bad syntax
At script.ps1:2 char 14
+ ... omputers = Get-ADComputer -Filter * -SearchBase "OU=Servers, ...
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    +CategoryInfo          : NotSpecified: (:) [Get-ADComputer], ADException
    +FullyQualifiedErrorId : ActiveDirectoryServer:8335,Microsoft,ActiveDirectory,Management,Command.GetADComputer

ps. this code is taken from here. yes I know Im not supposed to do that but after getting this error time after time I wanted to try a code that works.

9
  • 1
    so ... can you run just that line on it's own? Commented Feb 14, 2021 at 10:04
  • 1
    so this line >>> Get-ADComputer -Filter * -SearchBase "OU=Servers,DC=mydomain,DC=com" | Select Name | Sort-Object Name <<< fails? what happens if you run just the 1st part? Commented Feb 14, 2021 at 10:12
  • 1
    You're not using "OU=Servers,DC=mydomain,DC=com". You're using something else, probably something that has spaces in the wrong spots. Look very closely. Commented Feb 14, 2021 at 10:18
  • 1
    When you leave off the SearchBase parameter, does it work? If yes, then your OU path is incorrect, it's as simple as that. Maybe it contains special characters that need to be escaped - hard to say when you don't show it. Commented Feb 14, 2021 at 10:39
  • 1
    @PIGEXPERT - kool! glad to know that you found the error & the fix ... [grin] Commented Feb 14, 2021 at 11:02

1 Answer 1

1

Double check that the OU you're using as the search base is correct. This error occurs when it's off.

Apart from that, I recommend using the System.Net.NetworkInformation.Ping class. It's a lot faster than Test-Connection because you have more control over the ping timeout.

$ping = New-Object System.Net.NetworkInformation.Ping
$pingTimeutMS = 200

$computers = Get-ADComputer -Filter * -SearchBase "OU=Servers,DC=mydomain,DC=com" 

$results = $computers | Sort-Object Name | ForEach-Object {
    $ComputerName = $_.Name
    Write-Host "Pinging $ComputerName..."
    $test = $ping.Send($ComputerName, $pingTimeutMS)
    [pscustomobject]@{
        "Computer" = $ComputerName
        "IP Address" = if ($test.Status -eq "Success") { $test.Address } else { $test.Status }
    }
}

$results | Export-Csv "filepath.csv" -Delimiter ',' -NoTypeInformation -Encoding UTF8

Not appending the lines to the CSV piecemeal feels a bit less clunky, too.

Sign up to request clarification or add additional context in comments.

13 Comments

@PIGEXPERT It's not much faster in the success case (maybe a little bit when you send very many pings), but in the error case you don't need to wait as long as with Test-Connection.
@PIGEXPERT Think about that sentence again. What defines a ping success?
@PIGEXPERT ...but how do you know if a machine is connected to the network? You ping it, it responds. This takes two network trips (a.k.a. one round-trip) to wherever the machine is at. In the LAN maybe 10 ms - 5 each way. In slower networks it might take longer. Pinging a machine consists of waiting for a response. There is no way to accelerate the waiting, it takes as long as it takes. You can only decide giving up early when the response does not arrive within X ms. And for Test-Connection X is rather large. For Ping.Send() X is configurable.
For sending email there is Send-MailMessage (docs)
@PIGEXPERT "if they arent at their docking station they arent connected" - No, if they're not connected they are not connected. They could be connected via wireless LAN and respond to pings, "docked" is not a status in TCP/IP.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.