Created
May 18, 2016 17:05
-
-
Save Xainey/70caeb4f7d61f25db22bc07fb9e45264 to your computer and use it in GitHub Desktop.
Send WOL Packet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Send-WakeOnLan | |
{ | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$True, Position=1, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)] | |
[Alias("Name")] | |
[string[]] $ComputerName | |
) | |
Begin | |
{ | |
Write-Verbose "Initialize UDP Client." | |
$Udp = New-Object System.Net.Sockets.UdpClient | |
} | |
Process | |
{ | |
ForEach ($Computer in $ComputerName) | |
{ | |
$MacAddresses = (Get-WmiObject -Class SMS_R_SYSTEM -Namespace "Root\SMS\Site_100" -ComputerName "<AD Controller>" -Filter "Name='$Computer'").MACAddresses | |
ForEach ($MacAddress in $MacAddresses) | |
{ | |
Write-Verbose ("Sending Packet to Computer:{0} @ MacAddress: {1}" -f $Computer, $MacAddress) | |
$MByteArr = $MacAddress -split "[:-]" | % { [Byte] "0x$_" } | |
[Byte[]] $MPacket = (,0xFF * 6) + ($MByteArr * 16) | |
$Udp.Connect(([System.Net.IPAddress]::Broadcast),7) | |
$Udp.Send($MPacket,$MPacket.Length) | out-null | |
} | |
} | |
} | |
End | |
{ | |
Write-Verbose "Close UDP Connection." | |
$Udp.Close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment