Powershell–Wake on LAN from //o// and DHCP Parse from “The Admin Guy”–WHAT A COMBO!

powershell

Had to pull my list of MAC addresses together for a Wake on LAN script the other day.   The actual technique is posted by Marcus Van Orsow (//o// The Powershell Guy) right here and lets you place a MAC address into a Powershell script to send the Wake on Lan to any device supporting it.

“…..coooooolllll…..” I said imagining Powering on hundreds of systems with a Single Script.

His original script looked like this

$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)

He later described some alternate techniques to build the object for the MAC address.  I found one I particularly liked which took a source address in TEXT and spun it into the MacAddress in the format the script liked.

$mac = [byte[]]("00-0F-1F-20-2D-35".split(‘-‘) |% {[int]"0x$-"})

So a Quick modification of the script with a simple param to pass in the MacAddress

param(

$MacAddress=””

)

$mac = [byte[]]($MacAddress).split(‘-‘) |% {[int]"0x$-"})


$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)

And saving it as SEND-WOL.PS1 I was in business.  But a small problem.    Just HOW to get all of those MAC addresses?  I mean I could get about four Jolt Colas and go madly typing away, but really, the data is already THERE.

I will keep referring to this post because I love it.   Using Powershell to mine data from NETSH, the Admin Guy found a GREAT way to pull out DHCP data and make it useful.   Your DHCP scope not only contains the workstation name and current IP address.  It ALSO contains the MacAddress of the computer in question.

In my case, I didn’t want an HTML report.  I simply wanted a list of MAC addresses in USE and their particular Workstation names.  With this list in a simple CSV file I knew it would be a Powershell Oneliner I could use to Schedule a Wake On LAN for the entire Domain or targeted groups.

In his original script there was a static entry to the DHCP scope.  Believe it or not, I have 8 scopes to query for my task and different servers.   So the first modification was to take in parameters for the DHCP scope and the DHCPServer

param(

$DHCPScope,

$DHCPServer

)

So his first line now looked like this

$a = (netsh dhcp server $DHCPServer scope $DHCPScope show clients 1)

Next, an additional field for MAC in the properties.

$Row = "" | select MAC,Hostname,IP

I had to make sure the MAC address wasn’t removed so I commented out the line removing the MACaddress

#    $l = $l -replace ‘[0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}’, ”

So far, so good.   But some extra cleanup was needed.  It was  producing some unwanted output.  Turns out I needed to make sure a dash became a comma.

    $l = $l -replace ‘-D-‘,’,’

And of course, make sure the Mac Address was added to the output this time

     $Row.IP = ($l.Split(","))[0]

    #Subnet mask not used, but maybe in a later version, so let’s leave it in there:

    #$Row.SubNetMask = ($l.Split(","))[1]

   
    $Row.Mac = ($l.Split(","))[2]

    $Row.Hostname = ($l.Split(","))[3]

   

I removed the entire section for outputting to HTML.   His solution was already building a CSV file of the output.  I left that line in but dropped a RETURN to Return the $CSVfile value.   With that returned by the Script I could just build a single Variable rerunning the script each time supplying different values.

RETURN $CSVFile

Saving his script as GET-DHCPInfo.ps1 I was able to pull this off

$DHCPDATA+=(./GET-DHCPInfo.Ps1 –Dhcpscope ‘192.168.45.0’ –Dhcpserver ‘192.168.45.5’)

$DHCPDATA+=(./GET-DHCPInfo.Ps1 –Dhcpscope ‘192.168.22.0’ –Dhcpserver ‘192.168.22.5’)

$DHCPDATA+=(./GET-DHCPInfo.Ps1 –Dhcpscope ‘10.0.10.0’ –Dhcpserver ‘10.0.10.0’)

$DHCPDATA | export-csv C:powershellDHCPLIST.CSV

With this handy unsorted raw pile of MacAddresses in a text file I was able to pull off THIS with Marco’s Wake on lan Script.

IMPORT-CSV C:powershellDHCPLIST.CSV | foreach { (./SEND-WOL.PS1 –Macaddress $-.Mac)}

A mad cackling echoed throughout the office as staff twisted their heads ala “The Exorcist” while I screamed out “I HAVE WAKE ON LAN!!!!”

Now here’s another tip.  If you NEED the Macaddress and CAN do a Network scan you can use the “Advanced IP Lan Scanner” from Famatech (Free) which will probe a network and gather IP addresses and goodies.  You can export the output as CSV. 

Yes it’s free but not as automatic as Powershell.   It is handy for systems with Static IP addresses (like Servers) if you really don’t feel like typing.

Ah yes.  Life is good when armed with great technology that also happens to be free.

FEEL the Power of Shell

Sean

The Energized Tech

Leave a Reply