The Deployment Bunny

OS Deployment, Virtualization, Microsoft based Infrastructure…

  • Archives

  • Meta

Working in the Datacenter – Wake on LAN using PowerShell

Posted by Mikael Nystrom on November 2, 2015

I have to admit, I’m lazy. So when working with computers, datacenters, at home or basically anywhere I don’t like to get up and push a button. My hands needs to be close to my keyboard, BTW, Wake on LAN is not something new, it is actually pretty old.

The first section in this post is about how it works, and the second part is how to use it.

How does this work?

The Wake On LAN Function

I don’t like to download utilities or application when I don’t really need to, if i can solve this with a simple PowerShell CMD-let or a simple function, I’ll use that instead. So browsing around the Internet lead me to this site http://www.adminarsenal.com/admin-arsenal-blog/powershell-sending-a-wake-on-lan-wol-magic-packet where the basic functionality to create a magic packet exist. So by using the fundamentals from Kris Powell I crerated a function of this:

image
Send-MagicPacket

Getting the MacAdress

But to be able to send a Magic Packet I do need a MacAddress, so I need a function for that to and here it is. I do need multiple functions here.

The first function is to grab the MAC from a “live” IP address, but then I need to know the IP.

image
Get-MacFromIP

So the second function is to get the IP from name.

image
Get-IPFromName

And combining them leads me into the last function.

image
Get-MacFromName

There are of course a bunch of other ways to get the MAC address, you can of course grab the MAC address from with in the OS using basically any command line, but it’s so handy to not logon to all the machines(Yes I know there are ways, but I have a massive amount of lab machines, not members of the domain and other strange machines).

Storing the MacAddress in a XML data file

Well getting the macaddress is easy when the machine is turned on, but, hey  that’s not going to be the case here. So while the machines are “live” I can get the MAC, IP and Computer name and store that in an XML File, then I can later use that information to wake my machines up when I need them, so I do need to store that information somehow.

image
New-ComputerDataFile

Get information from the XML data file

Now, lets see what’s in the data file by using this function

image
Get-ComputerDataFile

Using this

Load them up and lets get started!

#Import Module
Import-Module C:\Users\Administrator\OneDrive\PowerShellScript\WakeOnLan\WakeOnLan.psm1 -Force -Verbose

#Set Vars
$XMLfile = “C:\Users\Administrator\OneDrive\PowerShellScript\WakeOnLan\Computers.xml”
$Computers = “FABUILD01″,”DFLAB01”

With all functions loaded you can now run trough a couple of steps to create your XML file and have functions ready for Wake-On-LAN

image
Function is loaded and basic variables are set

Create the XML file and get the content

#Generate  New XML File
New-ComputerDataFile -Computers $Computers -XMLDatafile $XMLfile

#Get Content of XML File
Get-ComputerDataFile -XMLDatafile $XMLfile

image

Wake it up!

#Send Magic Packet to Computer
Send-MagicPacket -Mac $(Get-MacFromXML -ComputerName DFLAB01 -XMLDatafile $XMLfile)

image

Here is the PowerShell Module and a Sample Script on how to use it http://1drv.ms/1KUHGMi

/mike

 

For Ref:

WakeOnLan.psm1 – Listning:

Function Get-MacFromIP{
param(
$IP
)
$Ping = ( new-object System.Net.NetworkInformation.Ping ).Send($IP)
if($Ping.Status -eq “Success”){
RETURN (Get-NetNeighbor -IPAddress $IP).LinkLayerAddress
}
else
{
Write-Host “NA”
}
}
Function Get-IPFromName{
Param(
$ComputerName
)
Return (Test-Connection -ComputerName $ComputerName -Count 1 -BufferSize 32).IPV4Address.IPAddressToString
}
Function Get-MacFromName{
param(
$ComputerName
)
$IP = (Test-Connection -ComputerName $ComputerName -Count 1 -BufferSize 32).IPV4Address.IPAddressToString
$Ping = ( new-object System.Net.NetworkInformation.Ping ).Send($IP)
if($Ping.Status -eq “Success”){
RETURN (Get-NetNeighbor -IPAddress $IP).LinkLayerAddress
}
else
{
Write-Host “NA”
}
}
Function Get-MacFromXML{
Param(
$ComputerName,
$XMLDatafile
)

[XML]$XMLData = Get-Content -Path $XMLDatafile
RETURN $(($XMLData.Computers.Computer | Where-Object -Property Name -EQ -Value $ComputerName).Mac)
}
Function New-ComputerDataFile{
Param(
$Computers,
$XMLDatafile
)

$XMLData = New-Item -Path $XMLDatafile -ItemType File -Force
$ComputerID = 100

set-Content $XMLData ‘<?xml version=”1.0″ encoding=”utf-8″?>’
add-Content $XMLData ‘<Computers>’

foreach($computerName in $computers){
$ComputerID = $ComputerID + 1
add-Content $XMLData ” <Computer id=””$ComputerID””>”
add-Content $XMLData ”  <Name>$ComputerName</Name>”
add-Content $XMLData ”  <IP>$(Get-IPFromName -ComputerName $ComputerName)</IP>”
add-Content $XMLData ”  <Mac>$(Get-MacFromName -ComputerName $ComputerName)</Mac>”
add-Content $XMLData ‘ </Computer>’
}
add-Content $XMLData ‘</Computers>’
}
Function Get-ComputerDataFile{
Param(
$XMLDatafile
)

[XML]$XMLData = Get-Content -Path $XMLDatafile
$XMLData.Computers.Computer

}
Function Send-MagicPacket{
Param(
$Mac
)
Write-Host “Sending MagicPacket to $MAC”

$MacByteArray = $Mac -split “[:-]” | ForEach-Object { [Byte] “0x$_”}
[Byte[]] $MagicPacket = (,0xFF * 6) + ($MacByteArray  * 16)
$UdpClient = New-Object System.Net.Sockets.UdpClient
$UdpClient.Connect(([System.Net.IPAddress]::Broadcast),7)
$UdpClient.Send($MagicPacket,$MagicPacket.Length)
$UdpClient.Close()
}

Proj-WakeOnLan.ps1 – Listning:

#Import Module
Import-Module C:\Users\Administrator\OneDrive\PowerShellScript\WakeOnLan\WakeOnLan.psm1 -Force -Verbose

#Set Vars
$XMLfile = “C:\Users\Administrator\OneDrive\PowerShellScript\WakeOnLan\Computers.xml”
$Computers = “FABUILD01″,”DFLAB01″

#Generate  New XML File
New-ComputerDataFile -Computers $Computers -XMLDatafile $XMLfile

#Get Content of XML File
Get-ComputerDataFile -XMLDatafile $XMLfile

#Send Magic Packet to Computer
Send-MagicPacket -Mac $(Get-MacFromXML -ComputerName DFLAB01 -XMLDatafile $XMLfile)

 

Computers.xml –  – Listning:

<?xml version=”1.0″ encoding=”utf-8″?>
<Computers>
<Computer id=”101″>
<Name>FABUILD01</Name>
<IP>192.168.97.134</IP>
<Mac>2C-41-38-09-A4-89</Mac>
</Computer>
<Computer id=”102”>
<Name>DFLAB01</Name>
<IP>192.168.97.121</IP>
<Mac>B8-AE-ED-75-7A-FC</Mac>
</Computer>
</Computers>

2 Responses to “Working in the Datacenter – Wake on LAN using PowerShell”

  1. Mike said

    Will this work with any version of powershell? Just curious if there’s a minimum level

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: