测试连接电源壳

问题描述

我是PS的初学者,但我想通过创建一个按钮来重新启动计算机,然后告诉我何时联机来使我的生活更轻松。我一直在尝试-Wait参数,但抛出错误。如果有人能指出我正确的方向,那就太好了。

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
}

}

解决方法

好运-我实际上已经在此问题上花费了大量时间,因为我每个月必须重新启动数百台服务器才能应用更新。我了解您希望-wait命令可以执行的操作,而且恐怕我知道为什么它不起作用。 restart-computer命令只是向计算机发送重启信号。 Powershell中没有逻辑说“重启计算机并等待其重新联机”,因此等待命令将无济于事。

相反,您的脚本需要执行几个额外的步骤:

  • 检查计算机的上次重新启动时间(这将告诉您计算机是否已重新启动)
  • 检查系统是否已完全启动备份
  • 如果重启未完成,请稍等片刻,然后重试

请参阅下面的修改后的代码。我尝试添加大量注释,以便您可以在添加步骤时看到我的逻辑。我不得不将自己的脚本从针对多台计算机的运行改编为单台计算机,但这应该可以解决问题。我的一个假设是,您已经弄清楚了脚本中的按钮单击部分,只需要等待重启的帮助即可。

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

    }
}