簡體   English   中英

測試連接 powershell

[英]Test-Connection powershell

我是 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

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM