question

ChrisChris-4965 avatar image
0 Votes"
ChrisChris-4965 asked RichMatheisen-8856 answered

Invoke-Command - Attempting to reconnect for up to 4 minutes...


Hi,

If you execute the following command on a remote computer and that remote computer looses network connection, the command tries to reconnect for a long time.

Invoke-Command -ComputerName computer01 -ScriptBlock {Start-Sleep -Seconds 3600}

Errors you get are: "WARNING: The network connection to sbc-2019-pvs11 has been interrupted. Attempting to reconnect for up to 4 minutes..."

How do you prevent the reconnecting? I just want it to stop after a few seconds when a network issue occurs.

Thanks,
Chris

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

You can try cresting a PSSession option object and limiting the maximum number of retries.

I think the problem may be caused by a TCP session timeout. An hour of "nothing" is pretty exceptional.

Knowing the context in which this happens (beyond just idling for an hour!) would help. If, for instance, you have a long-running script, you could use the "-AsJob" switch on the Invoke-Command and then pipe the Invoke-Command's output to a Receive-Job and assign the result to a variable.

If you're invoking the same script-block/script on multiple machines the Invoke-Command will offer you some degree of parallelism. So instead of running Invoke-Command inside a loop, just use an array of computer names instead of a single name.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.