# Help with reboot script

**URL:** <https://forums.powershell.org/t/help-with-reboot-script/22663>\
**Category:** PowerShell Help\
**Created:** [July 25, 2023, 11:11pm UTC](https://forums.powershell.org/t/help-with-reboot-script/22663 "2023-07-25T23:11:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbingeman](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tbingeman/32/2122_2.png) [@tbingeman](https://forums.powershell.org/u/tbingeman)\
**Post date:** [July 25, 2023, 11:11pm UTC](https://forums.powershell.org/t/help-with-reboot-script/22663/1 "2023-07-25T23:11:03Z")

</div>

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.

```auto
$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"
}

```

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [July 25, 2023, 11:38pm UTC](https://forums.powershell.org/t/help-with-reboot-script/22663/2 "2023-07-25T23:38:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 25, 2023, 11:39pm UTC](https://forums.powershell.org/t/help-with-reboot-script/22663/3 "2023-07-25T23:39:09Z")

</div>

> [@tbingeman](#):
>
> 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.

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

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

```auto
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.

```auto
Get-CimInstance -ComputerName $ListOfServers -ClassName CIM_OperatingSystem -Property LastBootUpTime

```

---

<div class="post-metadata">

**Author:** ![tbingeman](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tbingeman/32/2122_2.png) [@tbingeman](https://forums.powershell.org/u/tbingeman)\
**Post date:** [July 26, 2023, 12:07am UTC](https://forums.powershell.org/t/help-with-reboot-script/22663/5 "2023-07-26T00:07:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 26, 2023, 12:16am UTC](https://forums.powershell.org/t/help-with-reboot-script/22663/6 "2023-07-26T00:16:47Z")

</div>

> [@tbingeman](#):
>
> 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?

… since I couldn’t explain it better than the documentation … 🤷🏼‍♂️ …

> **[Restart-Computer (Microsoft.PowerShell.Management) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/restart-computer?view=powershell-7.3#-for)**
>
> This cmdlet is only available on the Windows platform. The Restart-Computer cmdlet restarts the operating system on the local and remote computers. You can use the parameters of Restart-Computer to run the restart operations, to specify the...

> [@tbingeman](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![tbingeman](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tbingeman/32/2122_2.png) [@tbingeman](https://forums.powershell.org/u/tbingeman)\
**Post date:** [July 26, 2023, 1:19am UTC](https://forums.powershell.org/t/help-with-reboot-script/22663/7 "2023-07-26T01:19:02Z")

</div>

Olaf,

As always, thank you for your time and expertise.

---

<div class="post-metadata">

**Author:** ![tbingeman](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tbingeman/32/2122_2.png) [@tbingeman](https://forums.powershell.org/u/tbingeman)\
**Post date:** [July 26, 2023, 1:26am UTC](https://forums.powershell.org/t/help-with-reboot-script/22663/8 "2023-07-26T01:26:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 26, 2023, 6:00am UTC](https://forums.powershell.org/t/help-with-reboot-script/22663/9 "2023-07-26T06:00:53Z")

</div>

> [@tbingeman](#):
>
> at the \*\* needed or can they be dropped?

What does the doucmentation say about this? 😉 😉

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:14pm UTC](https://forums.powershell.org/t/help-with-reboot-script/22663/10 "2024-05-16T20:14:44Z")

</div>


