Debian Codegraph @

ansible_collections/community/windows/plugins/modules/win_wakeonlan.ps1

raw
1 #!powershell
2
3 # Copyright: (c) 2017, Dag Wieers (@dagwieers) <[email protected]>
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 #AnsibleRequires -CSharpUtil Ansible.Basic
7
8 $spec = @{
9 options = @{
10 mac = @{ type = 'str'; required = $true }
11 broadcast = @{ type = 'str'; default = '255.255.255.255' }
12 port = @{ type = 'int'; default = 7 }
13 }
14 supports_check_mode = $true
15 }
16
17 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
18
19 $module.Result.changed = $false
20
21 $mac = $module.Params.mac
22 $mac_orig = $module.Params.mac
23 $broadcast = $module.Params.broadcast
24 $port = $module.Params.port
25
26 $broadcast = [Net.IPAddress]::Parse($broadcast)
27
28 # Remove possible separator from MAC address
29 if ($mac.Length -eq (12 + 5)) {
30 $mac = $mac.Replace($mac.Substring(2, 1), "")
31 }
32
33 # If we don't end up with 12 hexadecimal characters, fail
34 if ($mac.Length -ne 12) {
35 $module.FailJson("Incorrect MAC address: $mac_orig")
36 }
37
38 # Create payload for magic packet
39 # TODO: Catch possible conversion errors
40 $target = 0, 2, 4, 6, 8, 10 | ForEach-Object { [convert]::ToByte($mac.Substring($_, 2), 16) }
41 $data = (, [byte]255 * 6) + ($target * 20)
42
43 # Broadcast payload to network
44 $udpclient = new-Object System.Net.Sockets.UdpClient
45 if (-not $module.CheckMode) {
46 $udpclient.Connect($broadcast, $port)
47 [void] $udpclient.Send($data, 102)
48 }
49
50 $module.Result.changed = $true
51
52 $module.ExitJson()