Anderen Rechner aus einem Powershell Skript per WoL starten

henfri

Lieutenant
Registriert
Juni 2020
Beiträge
523
Hallo,

ich habe zwei Powershell-Funktionen gefunden, mit denen man einen Rechner per WoL wecken können sollte.
(mit einer anderen Software vom gleichen Rechner geht das auch).
Code:
function Invoke-WakeOnLan
{
  param
  (
    # one or more MACAddresses
    [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
    # mac address must be a following this regex pattern:
    [ValidatePattern('^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$')]
    [string[]]
    $MacAddress
  )
 
  begin
  {
    # instantiate a UDP client:
    $UDPclient = [System.Net.Sockets.UdpClient]::new()
  }
  process
  {
    foreach($_ in $MacAddress)
    {
      try {
        $currentMacAddress = $_
        
        # get byte array from mac address:
        $mac = $currentMacAddress -split '[:-]' |
          # convert the hex number into byte:
          ForEach-Object {
            [System.Convert]::ToByte($_, 16)
          }
 
        #region compose the "magic packet"
        
        # create a byte array with 102 bytes initialized to 255 each:
        $packet = [byte[]](,0xFF * 102)
        
        # leave the first 6 bytes untouched, and
        # repeat the target mac address bytes in bytes 7 through 102:
        6..101 | Foreach-Object {
          # $_ is indexing in the byte array,
          # $_ % 6 produces repeating indices between 0 and 5
          # (modulo operator)
          $packet[$_] = $mac[($_ % 6)]
        }
        
        #endregion
        
        # connect to port 400 on broadcast address:
        $UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
        
        # send the magic packet to the broadcast address:
        $null = $UDPclient.Send($packet, $packet.Length)
        Write-Verbose "sent magic packet to $currentMacAddress..."
      }
      catch
      {
        Write-Warning "Unable to send ${mac}: $_"
      }
    }
  }
  end
  {
    # release the UDF client and free its memory:
    $UDPclient.Close()
    $UDPclient.Dispose()
  }
}

function Send-WOL

{

<#

 .SYNOPSIS

   Send a WOL packet to a broadcast address

 .PARAMETER mac

  The MAC address of the device that need to wake up

 .PARAMETER ip

  The IP address where the WOL packet will be sent to

 .EXAMPLE

  Send-WOL -mac 00:11:22:33:44:55 -ip 192.168.2.100

#>

[CmdletBinding()]

param(

[Parameter(Mandatory=$True,Position=1)]

[string]$mac,

[string]$ip="255.255.255.255",

[int]$port=9

)

$broadcast = [Net.IPAddress]::Parse($ip)

$mac=(($mac.replace(":","")).replace("-","")).replace(".","")

$target=0,2,4,6,8,10 | % {[convert]::ToByte($mac.substring($_,2),16)}

$packet = (,[byte]255 * 6) + ($target * 16)

$UDPclient = new-Object System.Net.Sockets.UdpClient

$UDPclient.Connect($broadcast,$port)

[void]$UDPclient.Send($packet, 102)

}



Invoke-WakeOnLan -MacAddress '00:22:4D:AA:xx:xx' -Verbose
Send-WOL -mac '00:22:4D:AA:xx:xx'

Leider funktionieren beide nicht, d.h. der Rechner wird nicht geweckt.

Ich teste das in der Power-Shell IDE ohne Admin Rechte.

Woran kann das liegen?

Gruß,
Hendrik
 
bei mir ging es - habe nur die erste funktion getestet.

du hast diesen code in der shell laufen lassen (einfach copy und paste der gesamten funktionsdeklaration + <enter>) und anschließend rufst du die neu verfügbare funktion in derselben shell auf, korrekt?
 
henfri schrieb:
Ich teste das in der Power-Shell IDE ohne Admin Rechte.
Hast du auch mal mit Admin-Berechtigung getestet?
 
  • Gefällt mir
Reaktionen: Brati23
Danke für's testen, @Redundanz
Redundanz schrieb:
und anschließend rufst du die neu verfügbare funktion in derselben shell auf, korrekt?
Nein, in der Powershell IDE (bzw ISE) habe ich den Code und führe ihn aus.

Mit Admin Rechent funktioniert es auch nicht.

Komisch, dass es bei dir funtioniert und bei mir nicht..

Edit: Auf einem anderen Rechner funktioniert Invoke-WakeOnLan... Das wird ja immer komischer.
 
Zuletzt bearbeitet:
copy+paste die erste funktion doch mal in die shell und drücke die eingabetaste.

1711972887994.png


und dann setze in derselben shell den funktions-befehl manuell ab.

wenn es mit anderen tools geht, dann schließen wir ja eh eine fehlende zielsystem bioskonfiguration o.ä. aus...
 
Hallo,

es funktioniert jetzt auch mit dem anderen Rechner.
Ich hab nix geändert. Ich weiß nicht, was sich da verschluckt hatte.

Gruß,
Hendrik
 
  • Gefällt mir
Reaktionen: Redundanz

Ähnliche Themen

Zurück
Oben