简体   繁体   English

测试连接 powershell

[英]Test-Connection powershell

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.我是 PS 的初学者,但我试图让我的生活更轻松,创建一个重新启动计算机的按钮,然后告诉我它何时在线。 I've been trying the -Wait parameter but throws off errors.我一直在尝试 -Wait 参数,但会抛出错误。 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.我了解您希望 -wait 命令能做什么,恐怕我知道它为什么不起作用。 The restart-computer command just sends a reboot signal to the computer. restart-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. powershell 中没有内置逻辑来说“重新启动计算机并等待它重新联机”,因此等待命令将无济于事。

Instead, what your script needs is a couple of extra steps:相反,您的脚本需要几个额外的步骤:

  • Check the last reboot time of the computer (this will tell you if it has rebooted yet)检查计算机的上次重新启动时间(这将告诉您它是否已重新启动)
  • Check if the system has completely booted back up检查系统是否已完全启动备份
  • If reboot not complete, pause for a bit and try again如果重新启动未完成,请暂停一下,然后重试

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

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM