I'm a beginner to PS but i am trying to make my life easier creating a button to reboot a computer and then tell me when its online. I've been trying the -Wait parameter but throws off errors. if anyone can point me in the right direction that would be great.
Restart_Computer_Click ={
$again = 1
While($again -ne 'N'){
$ComputerName = Read-Host "ComputerName"
#output online
if (Test-connection $ComputerName -quiet -count 2){
write-host -foregroundcolor green "$ComputerName is online"
Restart-computer -computername $ComputerName -Force -Confirm
}
else{
#output -offline
write-host -foregroundcolor red "$ComputerName is offline"
}
$again = read-host "Reboot Again? y/n"
$again.toUpper() | out-null
}
}
What luck - I've actually spent a great deal of time on this problem as I have to reboot hundreds of servers each month to apply updates. I understand what you're hoping the -wait command will do, and I'm afraid I know why its not working. The restart-computer command just sends a reboot signal to the computer. There's no logic built into powershell to say "reboot the computer and wait for it to come back online", so waiting for the command won't help.
Instead, what your script needs is a couple of extra steps:
See the modified code below. I've tried to add extensive comments so that you can see my logic in adding steps. I've had to adapt my script from running against multiple computers to a single computer, but this should do the trick. My one assumption is that you've already figured out the button click part of the script and just need help waiting for the reboots.
Restart_Computer_Click =
{
$computer = "localhost"
$again = 1
While ($again -ne 'N')
{
$ComputerName = Read-Host "ComputerName"
#output online
if (Test-connection $ComputerName -quiet -count 2)
{
write-host -foregroundcolor green "$ComputerName is online"
Restart-computer -computername $ComputerName -Force -Confirm
$online = $true
}
else
{
#output -offline
write-host -foregroundcolor red "$ComputerName is offline"
$online = $false
}
# There's no point in checking the reboot sequence if the computer is offline, so
# let's ignore the process if its offline
if ($online -eq $true)
{
# here's the while loop that will keep checking for the status of the rebooted computer
while ($reboot_complete -ne $true)
{
# First, let's sleep for two minutes to give the system time to reboot
# My systems generally have to install updates, so I have to grant a
# generous reboot time
start-sleep -Seconds 120
# If the computer is in the middle of restarting, we can't check the boot status, so we
# need to wait a little longer
if (Test-Connection $computer -quiet -count 1)
{
$lastboot = Get-WMIObject Win32_OperatingSystem -ComputerName $computer -EA SilentlyContinue
$lastbootdate = $lastboot.converttodatetime($lastboot.LastBootUpTime)
$time_elapsed = (get-date) - $lastbootdate
# This is the main statement that's going to tell us if the system has rebooted
# The time elapsed is the time difference between right now and the time the computer
# was last rebooted. The time elapsed is basically the difference in time. If there's
# less than 10 minutes of difference, I assume its rebooted from the script. If its
# older than that, I assume it hasn't rebooted yet. For example, you can pull a system's
# time while its applying updates as part of the reboot process.
if ($time_elapsed.TotalMinutes -lt 10)
{
$reboot_complete = $true
}
}
}
}
$again = read-host "Reboot Again? y/n"
$again.toUpper() | out-null
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.