【问题标题】:Is there a powershell command that would power on another host?是否有可以启动另一台主机的 powershell 命令?
【发布时间】:2019-12-20 09:43:20
【问题描述】:

我有一组计算机,我想关闭然后重新打开电源,是否有一个 Powershell 命令可以执行此操作?我有'shutdown'命令来关闭它们,我不能使用reboot 命令。

提前致谢!

【问题讨论】:

    标签: powershell powershell-2.0 powershell-3.0 powershell-4.0 powershell-remoting


    【解决方案1】:

    如果您使用 powershell 5.0 + ,您可以使用 Restart-Computer cmdlet。

    $computers = @('name1','name2')
    
    foreach ($computer in $computers)
    {
        Restart-Computer -ComputerName $computer -Force
    }
    
    

    另一种方法是使用 shutdownCMD

    shutdown -i
    

    会弹出一个漂亮的 GUI 来满足您的要求。

    【讨论】:

      【解决方案2】:

      如果您愿意在您的 PowerShell 脚本中使用 WMI,Win32_OperatingSystem WMI 类对象具有称为 Win32ShutdownWin32ShutdownTracker 的方法,它们都允许您关闭或重新启动计算机,或强制注销远程用户。我创建了一个 scriptlet/script cmdlet/advanced 函数,它使用后者来完成您想要做的事情;它适用于从 2.0 开始的任何版本的 Windows PowerShell:

      function Close-UserSession {
      <#
      .SYNOPSIS
      Logs off a remote user, or reboots a remote computer.
      .DESCRIPTION
      Logs off a remote user, or reboots a remote computer.
      Optionally, forces the logoff or reboot without waiting for running programs to terminate.
      .INPUTS
      This cmdlet can accept a computer object from the pipeline.
      Default is to act on the local computer.
      .OUTPUTS
      Returns the success or failure of the attempt to logoff/reboot the remote computer.
      .PARAMETER ComputerName
      The name of the computer to log off or reboot.
      .PARAMETER Reboot
      If present, causes the computer to reboot instead of logging off the current user.
      .PARAMETER Force
      If present, forces the logoff/reboot without waiting for running programs to shut down.
      .Parameter Delay
      Defaults to 0. Specifies the number of seconds to wait before logging off or rebooting
      .EXAMPLE
      PS C:\> Close-UserSession -ComputerName WPBBX-LW57359
      (Would cause the current user on the indicated computer to be logged off immediately)
      .EXAMPLE
      PS C:\> Close-UserSession -Reboot -ComputerName WPBBX-LW57359 -Delay 30
      (Would cause the indicated computer to reboot after 30 seconds)
      .EXAMPLE
      PS C:\> Close-UserSession -ComputerName WPBBX-LW57359 -Reboot -Force
      (Forces an immediate reboot of the indicated computer without waiting for programs to shut down.)
      #>
          [CmdletBinding(SupportsShouldProcess=$true)]
      
          param(
          [Parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
          [String]$ComputerName = $env:COMPUTERNAME,
      
          [Switch]$Force,
      
          [Alias("Boot","Restart")]
          [Switch]$Reboot,
      
          [int]$Delay = 0
          )
          $logoff   =  0
          $shutdown =  1
          $rebootit =  2
          $forceit  =  4
          $poweroff =  8
      
          $func = 0 #default is to just log the user off
          $message = "Logging you off for ITSD troubleshooting "
          if ($Reboot) {
              $func = $func -bor $rebootit #reboot the indicated computer
              $message = "Rebooting the computer for ITSD troubleshooting "
              }
          if ($Force)  { $func = $func -bor $forceit  } #Force the logoff or reboot without worrying about closing files
          if ($Delay -eq 0) {
              $message = $message + "now!"
          } else {
              $message = $message + "in $Delay seconds."
          }
          $RemoteOS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName
          if ($psCmdlet.ShouldProcess($ComputerName)) {
              ($RemoteOS.Win32ShutdownTracker($Delay,$message,0,$func)).ReturnValue
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-03-16
        • 1970-01-01
        • 2020-07-05
        • 1970-01-01
        • 2016-08-07
        • 1970-01-01
        • 2017-05-20
        • 2019-07-29
        • 1970-01-01
        相关资源
        最近更新 更多