diff --git a/wakeup.ps1 b/wakeup.ps1 index 99e603b..548be40 100644 --- a/wakeup.ps1 +++ b/wakeup.ps1 @@ -56,40 +56,52 @@ if ( [String]::IsNullOrWhiteSpace($targetComputer)) { $targetComputer = Read-Host -Prompt 'Computer to wake-up' } 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 $broadcastIP = $db.WOLDatabase.Configuration.BroadcastAddress $port = $db.WOLDatabase.Configuration.Port $dnsSuffix = $db.WOLDatabase.Configuration.DnsSuffix - $targetComputers = [System.Collections.Generic.List[PSObject]]::new() $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 { - if ( [String]::IsNullOrWhiteSpace($_.friendlyName)) + $wolError = @() + $name = $_.name + $friendlyName = $_.friendlyName + if ( [String]::IsNullOrWhiteSpace($friendlyName)) { - $name = $_.name Write-Host "Processing request to wake-up '$name'... " -NoNewline } else { - $friendlyName = $_.friendlyName Write-Host "Processing request to wake-up '$friendlyName'... " -NoNewline } # send magic packet - $_.mac | Send-MagicPacket -BroadcastIP $broadcastIP -Port $port -WarningAction SilentlyContinue -WarningVariable wolErr -ErrorAction SilentlyContinue -ErrorVariable wolErr - if ($wolErr.Count -eq 0) + $_.mac | Send-MagicPacket -BroadcastIP $broadcastIP -Port $port -ErrorAction SilentlyContinue -ErrorVariable +wolError + if ($wolError.Count -eq 0) { Write-Host -ForegroundColor Green "[OK]" } else { 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 \ No newline at end of file