fix(wakeup): fix error handling from Send-MagicPacket

- adapt to changes in error handling in upstream module
This commit is contained in:
Asif Bacchus 2021-09-05 18:33:24 -06:00
parent 56bc398bf0
commit cda29f2ef1
1 changed files with 20 additions and 8 deletions

View File

@ -56,40 +56,52 @@ if ( [String]::IsNullOrWhiteSpace($targetComputer))
{ {
$targetComputer = Read-Host -Prompt 'Computer to wake-up' $targetComputer = Read-Host -Prompt 'Computer to wake-up'
} while ( [String]::IsNullOrWhiteSpace($targetComputer)) } while ( [String]::IsNullOrWhiteSpace($targetComputer))
Write-Host
} }
Write-Host
# find computer in database # read database and assemble list of target computers
[xml]$db = Get-Content -Path $wolDatabase [xml]$db = Get-Content -Path $wolDatabase
$broadcastIP = $db.WOLDatabase.Configuration.BroadcastAddress $broadcastIP = $db.WOLDatabase.Configuration.BroadcastAddress
$port = $db.WOLDatabase.Configuration.Port $port = $db.WOLDatabase.Configuration.Port
$dnsSuffix = $db.WOLDatabase.Configuration.DnsSuffix $dnsSuffix = $db.WOLDatabase.Configuration.DnsSuffix
$targetComputers = [System.Collections.Generic.List[PSObject]]::new() $targetComputers = [System.Collections.Generic.List[PSObject]]::new()
$db.WOLDatabase.Computers.Computer | Where-Object { $_.name -match "$targetComputer" } | ForEach-Object { $targetComputers.Add($_) } $db.WOLDatabase.Computers.Computer | Where-Object { $_.name -match "$targetComputer" } | ForEach-Object { $targetComputers.Add($_) }
$removeFromTargetComputers = [System.Collections.Generic.List[String]]::new()
# send WOL magic packets
$targetComputers | ForEach-Object { $targetComputers | ForEach-Object {
if ( [String]::IsNullOrWhiteSpace($_.friendlyName)) $wolError = @()
$name = $_.name
$friendlyName = $_.friendlyName
if ( [String]::IsNullOrWhiteSpace($friendlyName))
{ {
$name = $_.name
Write-Host "Processing request to wake-up '$name'... " -NoNewline Write-Host "Processing request to wake-up '$name'... " -NoNewline
} }
else else
{ {
$friendlyName = $_.friendlyName
Write-Host "Processing request to wake-up '$friendlyName'... " -NoNewline Write-Host "Processing request to wake-up '$friendlyName'... " -NoNewline
} }
# send magic packet # send magic packet
$_.mac | Send-MagicPacket -BroadcastIP $broadcastIP -Port $port -WarningAction SilentlyContinue -WarningVariable wolErr -ErrorAction SilentlyContinue -ErrorVariable wolErr $_.mac | Send-MagicPacket -BroadcastIP $broadcastIP -Port $port -ErrorAction SilentlyContinue -ErrorVariable +wolError
if ($wolErr.Count -eq 0) if ($wolError.Count -eq 0)
{ {
Write-Host -ForegroundColor Green "[OK]" Write-Host -ForegroundColor Green "[OK]"
} }
else else
{ {
Write-Host -ForegroundColor Red "[ERROR]" Write-Host -ForegroundColor Red "[ERROR]"
$removeFromTargetComputers.Add($name)
} }
} }
# remove computers from targetComputers if WOL packet was not successfully sent
$removeFromTargetComputers | ForEach-Object {
$removeName = $_
$targetComputers.Remove(($targetComputers | Where-Object { $_.name -eq $removeName })) | Out-Null
}
# process RDP readiness check
$targetComputers
Exit 0 Exit 0