Skip to content

Instantly share code, notes, and snippets.

@hagatorn
Last active March 16, 2017 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hagatorn/261ad5c3084950bec65c35189eac39e0 to your computer and use it in GitHub Desktop.
Save hagatorn/261ad5c3084950bec65c35189eac39e0 to your computer and use it in GitHub Desktop.
WOL with DHCP utility
param(
[string]$HostName,
[string]$Mac,
[string]$ip,
[switch]$All,
[switch]$broadcast,
[ValidateSet(“Site1”,”Site2”)][string]$site = "Site1"
)
$WOLpath = "\\server\c$\Scripts\WOL\"
$subnetmask = "255.255.0.0"
$scope, $broadcastIp = switch($site){
"Site1" {@("192.168.0.0", "192.168.255.255")}
"Site2" {@("10.10.0.0" , "10.10.255.255" )}
}
function WOL{
param(
[Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[string]
$ip,
[Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[string]
$Mac,
[Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[string]
$subnetmask
)
begin{
pushd $WOLpath
}
process{
Write-Output "Attempting to Wake $HostName on $ip, $Mac"
"./WolCmd.exe $Mac $ip $subnetmask"
./WolCmd.exe $Mac $ip $subnetmask
}
end{
popd
}
}
if($All){
Write-Output "Attempting to Wake all computers in $scope"
$computers = Invoke-Command -ComputerName $server -ScriptBlock {Get-DhcpServerv4Lease -scope $using:scope -AllLeases} | Select @{N='ip';E={$_.IPAddress.IPAddressToString}}, @{N='Mac';E={$_.ClientID}}, @{N='subnetmask';E={$subnetmask}}
$computers | WOL
}
#Something not quite right with this. Script seems take this logic tree fork even id $Mac is not set.
<#elseif($Mac -ne $null){
if($ip -eq $null -or $ip -eq ""){$ip = $broadcastIp}
Write-Output "$Mac $ip $subnetmask"
WOL -Mac $Mac -ip $ip -subnetmask $subnetmask
$scope = 192.168.0.0
}
#>
else{
if($HostName -eq $null -or $HostName -eq ""){
Write-Output "Please give a hostname or use the -all switch"
}
else{
if(Test-Connection $HostName -ErrorAction Ignore){
Write-Output "$Mac $ip $subnetmask"
Write-Output "$HostName is already on"
}
else{
Write-Output "Looking for $HostName in DHCP"
$ip, $Mac = Invoke-Command -ComputerName $server -ScriptBlock {Get-DhcpServerv4Lease -scope $using:scope -AllLeases |? {$_.HostName -like "$using:HostName*"}} | foreach{@($_.IPAddress,$_.ClientID.replace("-",""))}
if($Mac -eq $null -or $Mac -eq "")
{
Write-Output "Unable to find $hostname in dhcp"
}
else{
if($broadcast){$ip = $broadcastIp}
Write-Output "$Mac $ip $subnetmask"
WOL -Mac $Mac -ip $ip -subnetmask $subnetmask
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment