【问题标题】:Test-Connection powershell测试连接powershell
【发布时间】:2020-12-03 08:15:04
【问题描述】:

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

}

【问题讨论】:

  • 我不确定您要的是什么。您想知道如何在脚本中暂停吗?如果是这样,您可以使用Start-Sleep
  • 基本上我正在寻找单击我的按钮i 它会询问计算机名称它提示我从 -confirm 参数中询问我是否确定然后它会跟踪计算机直到它重新联机
  • 有什么错误?
  • 我今天回复了类似的东西。在这里查看:stackoverflow.com/questions/63389087/… 这能回答你的问题吗?
  • 如果您想制作一个按钮,您将不得不编写一个小的 Windows 窗体或 Windows Presentation Foundation 对话框。那里有很多关于如何做到这一点的东西,只需谷歌它。至于Restart-Computer命令你可以使用-For-Wait来等待特定服务上线,你也可以添加-TimeOut。这一切都取决于,但你可能不需要睡眠等待循环等......

标签: powershell scripting powershell-3.0


【解决方案1】:

真幸运——我实际上已经在这个问题上花费了大量时间,因为我每个月都必须重新启动数百台服务器才能应用更新。我了解您希望 -wait 命令能做什么,恐怕我知道它为什么不起作用。 restart-computer 命令只是向计算机发送重新启动信号。 powershell 中没有内置逻辑来表示“重新启动计算机并等待它重新联机”,因此等待命令将无济于事。

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

  • 检查计算机的上次重新启动时间(这会告诉您它是否已经重新启动)
  • 检查系统是否已完全启动备份
  • 如果重启未完成,请暂停一下,然后重试

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

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

    }
}

【讨论】:

  • 我相信 restart-computer -wait 取决于正在设置的远程 powershell。
  • @js2010 这可能取决于 PowerShell 版本。 7.x 的文档与 5.1 的文档有很大不同。 5.1 的文档似乎表明了具有 WSMan 功能的 DCOM 的默认协议,但我不会说它很清楚。 7.x 文档没有提及协议,但仍然谈论-WSManAuthentication,我们可以假设它不再支持 DCOM?但是我认为一般建议是您应该启用和使用 WinRM,之后问题就没有实际意义了。
  • Get-WMIObject 已弃用并从 PowerShell 7.x 中删除。请改用 Get-CimInstance 和其他 CIM cmdlet。但是,就我之前的观点而言,他们默认使用 WinRM。您必须使用 CimSessionOption 创建一个 CIM 会话,以将协议设置为 DCOM。并不疯狂复杂,但知道是成功的一半......
猜你喜欢
  • 2018-06-18
  • 1970-01-01
  • 2015-12-14
  • 2022-07-22
  • 1970-01-01
  • 1970-01-01
  • 2017-05-07
  • 2011-09-09
  • 2012-12-18
相关资源
最近更新 更多