(This article was written originally in Portuguese. If you are Brazilian, Portuguese or simply prefer to read in Portuguese, click here)
Good morning! Here I have the first version of that function I created to show how to use that little script that sends UDP packets over local network. This can be very helpful in corporative environments, specially when you have information about all machines. There are many implementations of WakeOnLan on the Internet, many of them free, written in many programming languages and technologies. My intention here is only show how to do this using only PowerShell and .Net Framework (and a text editor, of course :)
PowerShell language is very close to C#, so if you are looking for how to do the same in C#, this code can be easily modified. The main advantage of mine is the simplicity and the lack of dependencies... all you need is PowerShell (comes with Windows 7!) and Notepad (comes with all Windows versions)!
WakeOnLan is very simple: first, the computer to be waken needs to be compatible with WOL (the mainboard needs to be PCI2.2 compliant (at least), the computer needs to have ATX PSU, NIC needs to be compatible, and so on). This can be identified in system BIOS, that should have an option to enable or disable WakeOnLan. Of course, check if the computer to be waken is correctly configured.
But how this stuff works? All computer with a PSU supply stay in standby. If you enable WOL the computer also instructs its network card to stay in standby and monitor medium looking for a Magic Packet. When NIC receives the packet, it turns the computer on. And this Magic Packet is only a UDP packet sent to network broadcast address, usually on port 7 or 9, with FF FF FF FF FF FF on 6 first bytes followed by NIC MAC address repeated 16 times (total: 102 bytes). There's the possibility to send a password, but this is not very used and I didn't implemented. More information on Wikipedia.
My script is also simple. Save it on a .PS1 file (for instance, wake-computer.ps1) and call it using what we call dot-sourcing. It's like execute the script, but the internal functions (and also variables, data, constants, aliases) are exported, being visible to the user. Then you need to call a funcion called wake-computer with two parameters: NIC's MAC address and broadcast address.
Here's an example:
. '.\wake-computer.ps1'
wake-computer 00:16:76:75:01:00 192.168.1.255
Then, the code
# wake-computer function
# by Vinicius Canto - MVP
#
# Usage: wake-computer
#
# : MAC address of remote machine. The mac-address must be in the
# format AA:BB:CC:DD:EE:FF, six numbers in hexadecimal, like the
# output of IPconfig command
#
# Address used to broadcast packets on local network. This
# address must be in the format a.b.c.d, with a, b, c and d
# numbers between 0 and 255, the common form of an IP Address
#
# Example:
# wake-computer 00:16:76:75:AA:00 192.168.1.255
#
# For more information, contact-me: scripterbr (at) gmail (dot) com
#
# I'm looking for people to improve the code... if you want, contact me!
# example line that sends an UDP packet
# (new-object System.Net.Sockets.Socket(2,2,17)).SendTo("Vinicius",(new-object System.Net.IPEndPoint(([System.Net.IPAddress]::Parse("192.168.1.2")),2000)))
function wake-computer([string]$macAddress, [string]$broadcastAddress) {
$socket = new-object System.Net.Sockets.Socket(2,2,17)
$destination = [System.Net.IPAddress]::Parse($broadcastAddress)
$endpoint = new-object System.Net.IPEndpoint($destination,9)
[byte[]]$buffer = @(255,255,255,255,255,255)
$buffer += (($macAddress.split(':') | foreach {[byte]('0x' + $_)}) * 16)
$sent = $socket.Sendto($buffer, $buffer.length, 0, $endpoint)
"$sent bytes sent. The computer $macAddress may be initializing."
}
I recommend to be careful with line breaks. Blogger don't like source code... Also check if your current PowerShell configuration allows you to run scripts with Get-ExecutionPolicy command. I also recommend to look Wesley's blog... he is currently studying some new technologies like IAMT, and they help, among other things, remote computer management. He'll create some cool thing and post there. The link is here.
See you!
Escrito por
Vinicius Canto
em 23:40.
(1 comentários).
Link permanente.
Categorias dessa postagem:
powershell