Link to home
Start Free TrialLog in
Avatar of DBThomson76
DBThomson76Flag for United States of America

asked on

Powershell Script to reboot computer after I can ping it and it's online

I need a script to continuously ping a specific computer until it pings successfully and send a reboot command to reboot the same computer.

I know this is an odd request to reboot a computer when it first comes up but I need to do this to complete an install.

Any help is greatly appreciated.
Avatar of Jeremy Weisinger
Jeremy Weisinger

You could use something like this:
$targetname = "Server1"

do {
    If(Test-Connection $targetname -Quiet){shutdown /r /t 0 /f /m \\$targetname}
    sleep 5
    } while ($true)

Open in new window

Just change to the server's name. This loop won't stop until you cancel it (ctrl+C).
Avatar of DBThomson76

ASKER

Thank you!

Can it also tell me what date and time in rebooted?
Sure.
$targetname = "Server1"

do {
    If(Test-Connection $targetname -Quiet){
        shutdown /r /t 0 /f /m \\$targetname
        Get-Date
    }
    sleep 5
    } while ($true)

Open in new window

I tried the script and it worked but if it is unattended it will reboot the computer again once it is back up.

I just need it to reboot once after it comes online.

Thanks!
Ok, we can do that too.

$targetname = "Server1"
$a = $true
do {
    If(Test-Connection $targetname -Quiet){
        shutdown /r /t 0 /f /m \\$targetname
        Get-Date
        $a = $false
    }
    sleep 5
    } while ($a)

Open in new window

Worked great.

Once last request. Can the date and time of when it rebooted be sent to a file  for later review?

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Jeremy Weisinger
Jeremy Weisinger

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Worked like a charm.

Thank you very much!!
Glad to help. :)
Thank you!

Great help