MagicPacket.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<#
.Synopsis
    Send a magic packet to a MAC address.
.DESCRIPTION
    Generates a magic packet to wake up a computer, identified by the MAC address, across the network.
 
.PARAMETER MACAddress
    The MAC address of the computer.
 
.PARAMETER IPBroadcast
    The broadcast address of the network.
 
.PARAMETER UDPPort
    The UDP port to send the magic packet to.
 
.EXAMPLE
    PS > Send-MagicPacket -MACAddress "AB:CD:EF:12:34:56"
 
.EXAMPLE
    PS > Send-MagicPacket -MACAddress "AB-CD-EF-12-34-56" -UDPPort 9 -IPBroadcast "192.168.9.255"
 
 .NOTES
    Author: Andre Hohenstein - https://github.com/AndreHohenstein
    
.COMPONENT
    MagicPacket - https://github.com/AndreHohenstein/PowerShell/tree/main/Network/MagicPacket
#>

Function Send-MagicPacket{

[CmdletBinding()]
param(
    [Parameter(Mandatory = $true)][string]$MACAddress,
    [string]$IPBroadcast = "255.255.255.255",
    [int]$UDPPort = 9
)

begin {
    $UDP = New-Object -TypeName System.Net.Sockets.UdpClient

    $MacOctets = $MACAddress.Split(":").Split("-")

    if ($MacOctets.Count -ne 6) {
        Write-Error -Message "The MAC address does not contain the right amound of octets." -Category InvalidData -RecommendedAction "Check the MAC address supplied." -TargetObject $MACAddress -ErrorAction Stop
    }

    $Bytes = @()

    for ($i = 1; $i -le 6; $i++) {
        $Bytes += [byte]255
    }

    for ($i = 1; $i -le 16; $i++) {
        foreach ($Octet in $MacOctets) {
            try {
                $Bytes += [System.Convert]::ToByte($Octet, 16)
            }
            catch [Exception] {
                $ErrorDetails = $_

                switch ($ErrorDetails.FullyQualifiedErrorId) {
                    "FormatException" {
                        Write-Error -Exception "FormatException" -Message "The octect '$($Octet)' contains an invalid character in the string." -Category InvalidData -ErrorId "FormatException" -TargetObject $Octet -ErrorAction Stop
                    }
                    Default {
                        Write-Error $ErrorDetails -ErrorAction Stop
                    }
                }
            }
        }
    }
}

process {

    [byte[]]$MagicPacket = $Bytes

    try {
        $UDP.Connect($IPBroadcast, $UDPPort)
        $UDP.Send($MagicPacket, $MagicPacket.Length)
    }
    catch [Exception] {
        $ErrorDetails = $_
        switch ($ErrorDetails.FullyQualifiedErrorId) {
            "ObjectDisposedException" { 
                Write-Error -Exception "ObjectDisposedException" -ErrorId "ObjectDisposedException" -Message "The UDPClient object's connection was closed." -Category CloseError -TargetObject $UDP -ErrorAction Stop
            }
            "ArgumentOutOfRangeException" {
                Write-Error -Exception "ArgumentOutOfRangeException" -ErrorId "ArgumentOutOfRangeException" -Message "UDP port is not within the valid port range." -Category ProtocolError -TargetObject $UDP -ErrorAction Stop
            }
            "SocketException" {
                Write-Error -Exception "SocketException" -ErrorId "SocketException" -Message "There was an error accessing the socket." -Category ResourceUnavailable -TargetObject $UDP -ErrorAction Stop
            }
            "ArgumentNullException" {
                Write-Error -Exception "ArgumentNullException" -ErrorId "ArgumentNullException" -Message "The magic packet data is null." -Category InvalidData -TargetObject $UDP -ErrorAction Stop
            }
            Default {
                Write-Error $ErrorDetails -ErrorAction Stop
            }
        }
    }

}

end {
    $UDP.Close()

    return New-Object -TypeName psobject -Property @{
        "MAC Address" = $MACAddress;
        "Broadcast" = $IPBroadcast;
        "Success" = $true
    }
}
}