PowerShell for Windows Admins

Oct 24 2015   7:48AM GMT

Testing connectivity before Invoke-Command

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:
Powershell

A question on the forum asked about testing if a remote machine could be reached before using Invoke-Command against it.

The usual way to test if you can reach a remote machine is to ping it

PS> Test-Connection -ComputerName $env:COMPUTERNAME -Quiet
True

That shows you can reach the machine but it doesn’t mean that you can use Invoke-Command to send a request.

I think a better test is to use Test-WSMan

It will test if the WinRm service is running (won’t test if remoting is enabled)

$computers = “$env:COMPUTERNAME”, ‘NotFound’

foreach ($computer in $computers){
$target = $computer
if (Test-WSMan -ComputerName $computer -ErrorAction Ignore) {
Invoke-Command -ComputerName $computer -ScriptBlock {Get-Service}
}
else {
Write-Warning -Message “Couldn’t connect to $computer”
}

}

You can push the output to file or put the unreachable machine names into a file if you need to record them.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: