Created
June 20, 2014 20:50
-
-
Save Gunslap/8c9e2018bb6240c3c043 to your computer and use it in GitHub Desktop.
Script to take in list of computers (Name, IP, MAC Addresses) either from CSV or object array passed in and send a Wake On LAN command to each computer
This file contains hidden or 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
<# | |
******************************** | |
* Multi Wake On LAN Script * | |
******************************** | |
By: Gunslap | |
Last Updated: June 20th/2014 | |
Purpose: | |
This script will loop through a provided .csv list of MAC addresses, or a list passed in from | |
another function and send a Wake on LAN packet to each address. Assuming the machine is capable | |
of and configured to receive WOL commands, it will wake up. | |
#> | |
#This Function loops through MAC's | |
Function Multi-WOL | |
{ | |
param( | |
$CompList = "C:\TestMACs.csv" #Might be a CSV, or an array of objects from another script | |
) | |
$isCSV = $false | |
#if CompList is a location of a .csv file import it (instead of a list passed by another function) | |
if ($CompList -like "*.csv") | |
{ | |
#Imports the information from that file | |
$CompList = import-csv $MACList -erroraction stop | |
$isCSV = $true | |
} | |
#loop through each computer entry | |
foreach ($Computer in $CompList) | |
{ | |
$current = $Computer."ClientID" | |
$currentName = $Computer.HostName | |
#split the MAC address into a byte array: | |
if ($isCSV) | |
{ | |
#Use this in conjunction with a CSV list exported from DHCP | |
$current -match "(..)(..)(..)(..)(..)(..)" | out-null | |
$current = [byte[]]($matches[1..6] |% {[int]"0x$_"}) | |
} | |
else | |
{ | |
#Use this line instead to parse from the Invoke-SchoolMWideWakeOnLan script | |
$current = [byte[]]($current.split('-') |% {[int]"0x$_"}) | |
} | |
#Output the name of the machine we are attempting to wake | |
"Waking: $currentName" | |
#Pass the byte array into the WOL function | |
WOL -mac $current | |
} | |
} | |
#This function takes a MAC address (as a byte array) and sends a WOL command | |
Function WOL | |
{ | |
param( | |
$mac = [byte[]](0x00, 0x0F, 0x1F, 0x20, 0x2D, 0x35) | |
) | |
$UDPclient = new-Object System.Net.Sockets.UdpClient | |
$UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000) | |
$packet = [byte[]](,0xFF * 102) | |
6..101 |% { $packet[$_] = $mac[($_%6)]} | |
$UDPclient.Send($packet, $packet.Length) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment