Exception calling "wait" with 1 argument in PowerShell

845 Views Asked by At

I've got a file with a few hundred servers where I need to check connection over a specific port. The script I'm running is:

$connection = (New-Object System.Net.Sockets.TcpClient).ConnectAsync("$Hostname", $Port).Wait(1000)

    If($connection -eq 'True') {
        Write-Host "$Server can be reached on $Hostname over port $Port" -BackgroundColor Magenta
    }
    Else{
        Write-Host "$Server cannot be reached on $Hostname over port $Port" -BackgroundColor DarkCyan
    }

For the most of the servers I'm checking, this is working without issue. Some come back successful, some not, but most return a valid value based on the query. But for some reason I'm also seeing this error on quite a few attempts - and many of them seem to return a result as well:

Exception calling "Wait" with "1" argument(s): "One or more errors occurred."
At D:\File\PowerShell\PortCheck\PorteCheck.ps1:87 char:9
+         $connection = (New-Object System.Net.Sockets.TcpClient).Conne ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : AggregateException

Any clue what I'm doing wrong?

0

There are 0 best solutions below