[powershell] Shutdown / Wake Up Computer remote

Gepostet am: Jul 17, 2014 4:17:28 PM

Shutdown

Vorausgesetzt man hat die Rechte dazu, kann man einen Remote Computer mittels

> Stop-Computer -ComputerName $ComputerName 

herunterfahren.

Wird dieser Computer jedoch verwendet, weil beispielsweise ein Programm noch auf eine Netzwerkfreigabe zugreift, dann bekommt man die Fehlermeldung:

> Stop-Computer : This command cannot be run on target computer('ComputerName') due to following error: The system shutdown cannot be initiated because there are other users logged on to the computer.

Da hilft ein ... dagegen.

> Stop-Computer -ComputerName $ComputerName -force

WakeUp

Um einen Computer aufzuwecken, bedarf es eines soganannten Wake-Up-Befehls. Als Standalone Programm empfiehlt sich WOL2. Aber es geht auch mit der Powershell.

siehe dazu:

meine Shutdown und WakeUp-Skripte

WOL

function isComputerAlive([string]$ComputerName) {     if (Test-Connection  $ComputerName -Quiet)         {             return,$true         } else {             return,$false         } }   function WOL([string]$MACStr, [string]$ComputerName) {     if (((Get-Service spooler -ComputerName $ComputerName -ErrorAction SilentlyContinue).status -eq "Running") -and (isComputerAlive -ComputerName $ComputerName))     {         Write-Host "$ComputerName is already on"         return,$true     }    else {         Write-Host "Try to power on $ComputerName"         $MACAddr = $MACStr.split(':') | %{ [byte]('0x' + $_)}         $MACAddrParts = $MACStr.split(':')          if ($MACAddrParts.Length -ne 6)                  {                          Write-Host 'MAC address must be format xx:xx:xx:xx:xx:xx' -ForegroundColor Red                  }         $UDPclient = new-Object System.Net.Sockets.UdpClient         $UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)         $packet = [byte[]](,0xFF * 6)         $packet += $MACAddr * 16         [void] $UDPclient.Send($packet, $packet.Length)         write "Wake-On-Lan magic packet sent to $MACStr, length $($packet.Length)"         [bool]$serverup = $false         [int]$counter = 0         Do          {             if (((Get-Service spooler -ComputerName $ComputerName -ErrorAction SilentlyContinue).status -eq "Running") -and (isComputerAlive -ComputerName $ComputerName))             {                 $serverup =  $True             }             sleep -Seconds 10             $counter += 1             if ($counter -eq 20)             {                 $serverup =  $True             }         } while ($serverup -eq $false)         if (isComputerAlive -ComputerName $ComputerName)         {             return, $True         } else {             return, $False         }     } }  $MACStr = "XX:XX:XX:XX:XX:XX"$CompName = "fileserver" [bool]$success = (WOL -MACStr $MACStr -ComputerName $CompName) if ($success -eq $true) {     Write-Host "Server $CompName successfully powered on" -ForegroundColor green } else {     Write-Host "Problems powering on $CompName" -ForegroundColor red }

Shutdown

function isComputerAlive([string]$ComputerName) { if (Test-Connection  $ComputerName -Quiet) {     return,$true } else {     return,$false } }  function shutdownComputer([string]$ComputerName) {     [bool]$alive = (isComputerAlive -Computername "fileserver3")     if($alive -eq $true)     {         #Computer is up => Stop-Computer        Stop-Computer -ComputerName $ComputerName -Force         #Check the Shutting Down        [bool]$serverup = $true         [int]$counter = 0         Do          {             if (isComputerAlive -ComputerName $ComputerName)             {                 $serverup = $true             } else {                 $serverup = $false             }             sleep -Seconds 10             $counter += 1             if ($counter -eq 20)             {                 $serverup = $false             }         } while ($serverup -eq $true)         #Check the Server still up        if (isComputerAlive -ComputerName $ComputerName)         {             return,$false         } else {             return,$true          }     } else {         #Computer is already down        return,$true     } }  [string]$CompName = "fileserver" [bool]$success = shutdownComputer -ComputerName $CompNameif ($success -eq $true) {     Write-Host "Server $CompName successfully shut down" -ForegroundColor green } else {     Write-Host "Problems shutting down Computer $CompName" -ForegroundColor red }