Powershell: ForEach-Object -Parallel Never reconnects after a remote Restart-Computer

Created on 5 Nov 2019  路  6Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

1. Install Powershell 7 Release 5 x64 on a up-to-date Windows 10 Computer
2. Run Powershell 7
3. Run the following script...

$ComputerNames = "Computer1","Computer2", "Computer3"
$ComputerNames | ForEach-Object -Parallel {
     Restart-Computer -ComputerName $_ -Force -Wait -Timeout 1800
}

Where Computer1-3 are test computers on the same network.

4. Wait the allotted Timeout period of 30 minutes.

Expected behavior

The remote computer will reboot and the script will continue without error.

Actual behavior

The computers reboot, but PowerShell never reconnects to them.

After 30 Minutes, the following errors are received;

Restart-Computer: Failed to restart the computer Computer1 with the following error
 message: The computer did not finish restarting within the specified time-out period..
Restart-Computer: Failed to restart the computer Computer2 with the following error
 message: The computer did not finish restarting within the specified time-out period..
Restart-Computer: Failed to restart the computer Computer3 with the following error
 message: The computer did not finish restarting within the specified time-out period..

Environment data

Name                           Value
----                           -----
PSVersion                      7.0.0-preview.5
PSEdition                      Core
GitCommitId                    7.0.0-preview.5
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Area-Cmdlets-Core Issue-Question Resolution-Answered

Most helpful comment

This appears to be an error in the Restart-Computer cmdlet. When I run it alone in the latest preview build, it restarts the remote computer as expected, but never returns and eventually crashes PowerShell. I get the same behavior when running it within a foreach -parallel script block.

This appears to have nothing to do with foreach -parallel, except that foreach -parallel doesn't currently support progress data (by design) and so there is no indicator of how the computer restart is proceeding.

I feel this Issue should be closed and two new issues open:
a. Restart-Computer -cn -Force -Wait -Timeout is broken in latest build.
b. ForEach -Parallel to support progress data stream (not sure how this would work and it would be a new feature request anyway).

All 6 comments

I don't think ForEach-Object -parallel is designed to reconnect. Do jobs / threadjobs normally do that?

/Cc @PaulHigin

Start-Job and Start-Threadjob are similar, but run jobs in the background which require you to issue other commands to monitor their progress. ForEach-Object -Parallel runs in the foreground and can display progress as it goes. So it would be more practical and easier to use for some use-cases (such as installing software on multiple computers in at the same time).

I think a parallel loop should be able to handle anything that a normal ForEach-Object loop can.

After reboot the remote session doesn't exist and we could only create new session with _clear_ state so that we don't know a script point to continue from.

After reboot the remote session doesn't exist and we could only create new session with _clear_ state so that we don't know a script point to continue from.

My understanding of the reported issue is that Restart-Computer along with -Wait is supposed to determine when the restarted computer is back online, but is failing to do so when ran in a background thread.

I also initially read it as ForEach-Object isn't reconnecting, but I don't think that's what they're referring to.

This appears to be an error in the Restart-Computer cmdlet. When I run it alone in the latest preview build, it restarts the remote computer as expected, but never returns and eventually crashes PowerShell. I get the same behavior when running it within a foreach -parallel script block.

This appears to have nothing to do with foreach -parallel, except that foreach -parallel doesn't currently support progress data (by design) and so there is no indicator of how the computer restart is proceeding.

I feel this Issue should be closed and two new issues open:
a. Restart-Computer -cn -Force -Wait -Timeout is broken in latest build.
b. ForEach -Parallel to support progress data stream (not sure how this would work and it would be a new feature request anyway).

Thanks for the explanation Paul. I will take your suggestion and post new issue/enhancement.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

concentrateddon picture concentrateddon  路  3Comments

HumanEquivalentUnit picture HumanEquivalentUnit  路  3Comments

aragula12 picture aragula12  路  3Comments

garegin16 picture garegin16  路  3Comments

andschwa picture andschwa  路  3Comments