Skip to content

Instantly share code, notes, and snippets.

@hagatorn
Last active March 19, 2021 18:46
Show Gist options
  • Save hagatorn/96960b1830ba029c302919e1cdeb9645 to your computer and use it in GitHub Desktop.
Save hagatorn/96960b1830ba029c302919e1cdeb9645 to your computer and use it in GitHub Desktop.
WOL with pdq utility
[cmdletbinding()]
param(
[Parameter(ParameterSetName=’NamedHost’)]
[string]$HostName,
[Parameter(ParameterSetName=’AllHosts’)]
[switch]$All,
[switch]$DHCP,
[Parameter(ParameterSetName=’AllHosts’)]
[ValidateSet(“Site1”,”Site2”)]
[string]$site = "Site1"
)
#Constants
$WOLpath = "\\server\c$\Scripts\WOL\"
$subnetmask = "255.255.0.0"
#Variables
$scope, $broadcastIp, $server = switch($site){
"Site1" {@("192.168.0.0", "192.168.255.255", "Server1")}
"Site2" {@("10.10.0.0" , "10.10.255.255", "Server2")}
}
$queryType = if($DHCP){"DHCP"}else{"PDQ"}
#Logic
function WOL{
[cmdletbinding()]
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-Verbose "Attempting: ./WolCmd.exe $Mac $ip $subnetmask"
./WolCmd.exe $Mac $ip $subnetmask | Write-Verbose
}
end{
popd
}
}
function QueryPDQ{
[cmdletbinding()]
param(
[Parameter(ParameterSetName=’NamedHosts’,Position=1)]
[Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[string]
$HostName,
[Parameter(ParameterSetName=’AllHosts’)]
[switch]
$All,
[Parameter(ParameterSetName=’AllHosts’)]
[ValidateSet(“Site1”,”Site2”)]
[string]$site = "Site1"
)
$db = "\\spice\c$\programdata\Admin Arsenal\PDQ Inventory\Database1.db"
$sql1 = @"
SELECT Computers.MacAddress
FROM Computers
LEFT JOIN CollectionComputers ON Computers.ComputerId = CollectionComputers.ComputerId
LEFT JOIN Collections ON Collections.CollectionId = CollectionComputers.CollectionId
WHERE Collections.Name LIKE '$Site' AND CollectionComputers.IsMember = 1;
"@
$sql2 = @"
SELECT Computers.MacAddress
FROM Computers
LEFT JOIN CollectionComputers ON Computers.ComputerId = CollectionComputers.ComputerId
LEFT JOIN Collections ON Collections.CollectionId = CollectionComputers.CollectionId
WHERE Computers.Name LIKE '$HostName' AND Collections.Name IN ('Site1','Site2') AND CollectionComputers.IsMember = 1;
"@
$sql = if($all){$sql1}
else{$sql2}
pushd "\\spice\c$\Program Files (x86)\Admin Arsenal\PDQ Inventory"
$sql | .\sqlite3.exe $db | Select @{N='Mac';E={$_ -replace ":" , ""}}
popd
}
function QueryDHCP{
[cmdletbinding()]
param(
[Parameter(ParameterSetName=’NamedHosts’,Position=1)]
[Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[string]
$HostName,
[Parameter(ParameterSetName=’AllHosts’)]
[switch]
$All,
[string]$scope = "192.168.0.0",
[string]$server = "Server1"
)
$Query = if($all){ {Get-DhcpServerv4Lease -scope $using:scope -AllLeases} }
else{ {Get-DhcpServerv4Lease -scope $using:scope -AllLeases |? {$_.HostName -like "$using:HostName*"}} }
Invoke-Command -ComputerName $server -ScriptBlock $Query | Select @{N='ip';E={$_.IPAddress}}, @{N='Mac';E={$_.ClientID.replace("-","")}}
}
if($All){
Write-Verbose "Attempting to Wake all computers in $site`: $scope"
$computers = if($DHCP){QueryDHCP -All -scope $scope -server $server}else{QueryPDQ -All -site $site}
$computers.Mac | WOL -subnetmask $subnetmask -ip $scope
}
else{
if([string]::IsNullOrEmpty($HostName)){
Write-Error "Please give a hostname or use the -all switch"
}
else{
if(Test-Connection $HostName -ErrorAction Ignore){
Write-Error "$HostName is already on"
}
else{
Write-Verbose "Looking for $HostName in $queryType"
$Computer = if($DHCP){QueryDHCP $HostName -scope $scope -server $server}else{QueryPDQ $HostName}
if([string]::IsNullOrEmpty($Computer.Mac))
{
Write-Error "Unable to find $hostname in $queryType"
}
else{
if($broadcast){$ip = $broadcastIp}
Write-Verbose ("Passing WOL Mac:"+$Computer.Mac+" IP:"+$scope+" Subnetmask:"+$subnetmask)
WOL -Mac $Computer.Mac -ip $scope -subnetmask $subnetmask
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment