Help with reboot script

I have a script that I am porting over to PS7 from version 5.1. If I run the commands in 5.1 the script works as expected but as soon as I put it into PS7, the script fails to run and will just hang. I am not sure what is keeping the script from running.

$ListOfServers | Foreach-Object -ThrottleLimit 25 -Parallel {
    function Get-TimeStamp {
        return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)   
    }
    Add-Content -path $logfile "$(Get-Timestamp) Sending reboot command to $_"
    Restart-Computer -Force -ComputerName $_ -ErrorAction SilentlyContinue
    $NetLogon = "Stopped"
    start-sleep 15
    Do{
        Try{
            Start-Sleep -Seconds 5
            $NetLogon = Get-Service -Computername $_ | Where-Object {$_.ServiceName -eq "NetLogon"}
        }Catch{
        }
    } Until ($NetLogon.Status -eq "Running")
    
    Add-Content -path $logfile "$(Get-Timestamp) Server $_ has been rebooted"
}

Hanging with a loop defined in the script typically indicates it is stuck in the loop.

Have a look at Get-Service in PS7, it no longer supports -ComputerName

2 Likes

That sounds pretty unlikely since the parameter -Parallel for the cmdlet Forach-Object was introduced with PowerShell version 7.0. :smirk:

Your script looks a little convoluted to me. I’d like to keep things a little simpler and would recommend another approach …

Restart-Computer -ComputerName $ListOfServers -Wait -For **WinRM** -Timeout 300 -Force

If you need to check the reboot on the servers I’d query the servers for their LastBootUpTime afterwards.

Get-CimInstance -ComputerName $ListOfServers -ClassName CIM_OperatingSystem -Property LastBootUpTime
1 Like

Olaf

Thank you for your response. I understand your skepticism on my script working in 5.1 but the for-each wrapper is different when I run it in 5.1. The stuff inside the For-Each is identical in my 5.1 script. I would like to make it less convoluted and easier for someone to read. I do have a question for you though.

In your one line script, the -wait -for WinRM, does it require that I enter something for the WinRM or is it looking for the WinRM services to start up before it continues on to the next server? I realize the way I have it written it will target the first 25 servers in my $ListOfServers variable and this is what I want as I am trying to use the script to reboot over 200 servers.

… since I couldn’t explain it better than the documentation … :man_shrugging:t3: …

The cmdlet Restart-Computer would more or less restart all provided computers at once. If you need to throttle this you’d need to do that “outside” of the command.

Olaf,

As always, thank you for your time and expertise.

Last question on this, at the ** needed or can they be dropped?

What does the doucmentation say about this? :wink: :wink: