Functions/Send-WOL.ps1


function Send-WOL {

    [CmdletBinding()]
    param (
        [Parameter()] [ValidateSet("Zolder", "NAS01")] [string] $DeviceName,
        [Parameter()] [string] $MacAddress
    )

    if (!($DeviceName) -and !($MacAddress)) {
        Write-Warning "Please provide DeviceName of MacAddress"
        break
    }

    $DeviceName = "Zolder"
    $Devices = @{
        Zolder = "20:CF:30:69:14:EF"
        NAS01  = "10:7B:EF:BC:4D:A8"
    }
    if ($DeviceName) {
        $MacAddress = $Devices[$DeviceName]
    }

    $MacAddress
    # $MacAddress = "1A:2B:3C:4D:5E:6F"



    $Broadcast = ([System.Net.IPAddress]::Broadcast)

    ## Create UDP client instance
    $UdpClient = New-Object Net.Sockets.UdpClient

    ## Create IP endpoints for each port
    $IPEndPoint = New-Object Net.IPEndPoint $Broadcast, 9

    ## Construct physical address instance for the MAC address of the machine (string to byte array)
    $MAC = [Net.NetworkInformation.PhysicalAddress]::Parse($MacAddress.ToUpper())

    ## Construct the Magic Packet frame
    $Packet = [Byte[]](, 0xFF * 6) + ($MAC.GetAddressBytes() * 16)

    ## Broadcast UDP packets to the IP endpoint of the machine
    $UdpClient.Send($Packet, $Packet.Length, $IPEndPoint) | Out-Null
    $UdpClient.Close()






    # $MacByteArray = $MacAddress -split "[:-]" | ForEach-Object { [Byte] "0x$_" }
    # [Byte[]] $MagicPacket = (, 0xFF * 6) + ($MacByteArray * 16)
    # # $MagicPacket
    # $UdpClient = New-Object System.Net.Sockets.UdpClient
    # # $UdpClient.EnableBroadcast = $true
    # $UdpClient.Connect(([System.Net.IPAddress]::Broadcast), 4000)
    # $UdpClient.Send($MagicPacket, $MagicPacket.Length)
    # $UdpClient.Close()



    # $UDPclient = [System.Net.Sockets.UdpClient]::new()
    # $mac = $MacAddress -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)

    # $UDPclient.Close()
    # $UDPclient.Dispose()



}