Compare commits
7 Commits
4a8fff4384
...
cccd7ca3d6
Author | SHA1 | Date |
---|---|---|
Asif Bacchus | cccd7ca3d6 | |
Asif Bacchus | 140edc1cb1 | |
Asif Bacchus | 56a910d714 | |
Asif Bacchus | fc3a39b0b9 | |
Asif Bacchus | 47c313a26a | |
Asif Bacchus | 5332818990 | |
Asif Bacchus | 0119923ed7 |
119
wakeup.ps1
119
wakeup.ps1
|
@ -1,30 +1,37 @@
|
||||||
<#
|
<#
|
||||||
#>
|
#>
|
||||||
|
|
||||||
|
[CmdletBinding()]
|
||||||
param
|
param
|
||||||
(
|
(
|
||||||
# Array of strings listing computer name(s) to wake up. Names will be matched using RegEx
|
# Array of strings listing computer name(s) to wake up. Names will be matched using RegEx
|
||||||
[Alias("Computer")]
|
[Alias("Computer")]
|
||||||
[String[]]
|
[String[]]
|
||||||
$TargetComputer = "",
|
$TargetComputer = "",
|
||||||
# XML database file containing computer details
|
# XML database file containing computer details (Default: WOLDatabase.xml)
|
||||||
[Alias("List", "database", "db")]
|
[Alias("List", "Database")]
|
||||||
[String]
|
[String]
|
||||||
$WolDatabase = "WOLDatabase.xml",
|
$WolDatabase = "WOLDatabase.xml",
|
||||||
# Skip connectivity readiness check
|
# Skip connectivity check
|
||||||
[Alias("NoCheck", "NoReadiness")]
|
[Alias("NoCheck", "NoReadiness")]
|
||||||
[Switch]
|
[Switch]
|
||||||
$NoConnectivityCheck = $false,
|
$NoConnectivityCheck,
|
||||||
# Initial wait period in seconds before first connectivity check
|
# Delay before running first connectivity check (Default: 10)
|
||||||
|
[Alias("Delay")]
|
||||||
[Int]
|
[Int]
|
||||||
$WaitBeforeConnectivityCheck = 10,
|
$ConnectivityCheckDelay = 10,
|
||||||
# connectivity readiness check total timeout
|
# Seconds before aborting connectivity check (Default: 300)
|
||||||
|
[Alias("Timeout")]
|
||||||
[Int]
|
[Int]
|
||||||
$Timeout = 300,
|
$ConnectivityCheckTimeout = 300,
|
||||||
# connectivity readiness port to check
|
# Seconds between connectivity checks (Default: 30)
|
||||||
|
[Alias("Interval")]
|
||||||
|
[Int]
|
||||||
|
$ConnectivityCheckInterval = 30,
|
||||||
|
# Port to use for connectivity check (Default: 3389-RDP)
|
||||||
[Alias("CheckPort")]
|
[Alias("CheckPort")]
|
||||||
[Int]
|
[Int]
|
||||||
$ConnectivityPort = 3389
|
$ConnectivityCheckPort = 3389
|
||||||
)
|
)
|
||||||
|
|
||||||
function exitError($errMessage, $PSItem, $exitCode = 1)
|
function exitError($errMessage, $PSItem, $exitCode = 1)
|
||||||
|
@ -84,14 +91,14 @@ if ( [String]::IsNullOrWhiteSpace($TargetComputer))
|
||||||
Write-Host
|
Write-Host
|
||||||
|
|
||||||
# read database and assemble list of target computers
|
# read database and assemble list of target computers
|
||||||
[xml]$db = Get-Content -Path $WolDatabase
|
[xml]$wolDb = Get-Content -Path $WolDatabase
|
||||||
$broadcastIP = $db.WOLDatabase.Configuration.BroadcastAddress
|
$broadcastIP = $wolDb.WOLDatabase.Configuration.BroadcastAddress
|
||||||
$port = $db.WOLDatabase.Configuration.Port
|
$port = $wolDb.WOLDatabase.Configuration.Port
|
||||||
$dnsSuffix = $db.WOLDatabase.Configuration.DnsSuffix
|
$dnsSuffix = $wolDb.WOLDatabase.Configuration.DnsSuffix
|
||||||
$targetComputers = [System.Collections.Generic.List[PSObject]]::new()
|
$targetComputers = [System.Collections.Generic.List[PSObject]]::new()
|
||||||
$TargetComputer | ForEach-Object {
|
$TargetComputer | ForEach-Object {
|
||||||
$tgt = $_
|
$tgt = $_
|
||||||
$db.WOLDatabase.Computers.Computer | Where-Object { $_.name -match $tgt } | ForEach-Object { $targetComputers.Add($_) }
|
$wolDb.WOLDatabase.Computers.Computer | Where-Object { $_.name -match $tgt } | ForEach-Object { $targetComputers.Add($_) }
|
||||||
}
|
}
|
||||||
$removeFromTargetComputers = [System.Collections.Generic.List[String]]::new()
|
$removeFromTargetComputers = [System.Collections.Generic.List[String]]::new()
|
||||||
|
|
||||||
|
@ -125,6 +132,7 @@ $targetComputers | ForEach-Object {
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Write-Host -ForegroundColor Red "[ERROR]"
|
Write-Host -ForegroundColor Red "[ERROR]"
|
||||||
|
# queue for removal from computers on which to run a connectivity check
|
||||||
$removeFromTargetComputers.Add($name)
|
$removeFromTargetComputers.Add($name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -143,41 +151,78 @@ if ($NoConnectivityCheck)
|
||||||
}
|
}
|
||||||
elseif ($targetComputers.Count -eq 0)
|
elseif ($targetComputers.Count -eq 0)
|
||||||
{
|
{
|
||||||
exitError "No successful wake-up packets sent" -exitCode 5
|
exitError "No wake-up packets sent successfully" -exitCode 5
|
||||||
}
|
}
|
||||||
|
|
||||||
# wait for initialWaitTime seconds to let computer(s) wake up
|
# wait for initial delay seconds to let computer(s) wake up
|
||||||
for ($i = $WaitBeforeConnectivityCheck; $i -gt 0; $i--) {
|
for ($i = $ConnectivityCheckDelay; $i -gt 0; $i--) {
|
||||||
Write-Progress -Activity "Waiting for computer(s) to wake-up..." -SecondsRemaining $i
|
Write-Progress -Activity "Waiting for computer(s) to wake-up..." -SecondsRemaining $i
|
||||||
Start-Sleep 1
|
Start-Sleep 1
|
||||||
}
|
}
|
||||||
Write-Progress -Activity "Waiting for computer(s) to wake-up..." -Completed
|
Write-Progress -Activity "Waiting for computer(s) to wake-up..." -Completed
|
||||||
|
|
||||||
# iterate computers and test connectivity
|
# iterate computers and test connectivity
|
||||||
$targetComputers | ForEach-Object {
|
# TODO: run connectivity test as background job
|
||||||
$name = $_.name
|
$connCheckTotalTime = 0
|
||||||
$friendlyName = $_.friendlyName
|
$removeFromTargetComputers.Clear()
|
||||||
$fqdn = -join ($name, $dnsSuffix)
|
do
|
||||||
$connectionError = ""
|
{
|
||||||
|
$targetComputers | ForEach-Object {
|
||||||
|
$name = $_.name
|
||||||
|
$friendlyName = $_.friendlyName
|
||||||
|
$fqdn = -join ($name, $dnsSuffix)
|
||||||
|
$connectionError = @()
|
||||||
|
|
||||||
if ( [String]::IsNullOrWhiteSpace($friendlyName))
|
if ( [String]::IsNullOrWhiteSpace($friendlyName))
|
||||||
{
|
{
|
||||||
Write-Host "Testing connection readiness of '$name'... " -NoNewline
|
Write-Host "Testing connection readiness of '$name'... " -NoNewline
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Write-Host "Testing connection readiness of '$friendlyName'... " -NoNewline
|
||||||
|
}
|
||||||
|
if (!(Test-NetConnection -ComputerName $fqdn -Port $ConnectivityCheckPort -InformationLevel Quiet -WarningAction SilentlyContinue -ErrorAction SilentlyContinue -ErrorVariable +connectionError))
|
||||||
|
{
|
||||||
|
if (($connCheckTotalTime + $ConnectivityCheckInterval) -gt $ConnectivityCheckTimeout)
|
||||||
|
{
|
||||||
|
# last iteration, display 'error'
|
||||||
|
Write-Host -ForegroundColor Red "[ERROR]"
|
||||||
|
$connectionError | ForEach-Object {
|
||||||
|
$errMsg = $_.ToString()
|
||||||
|
Write-Verbose "`t(Additional information: $errMsg)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
# more iterations pending, display 'retrying'
|
||||||
|
Write-Host -ForegroundColor Yellow "[RETRYING]"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Write-Host -ForegroundColor Green "[OK]"
|
||||||
|
# queue for removal from computers to check next round
|
||||||
|
$removeFromTargetComputers.Add($name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
# remove computers from targetComputers if connectivity check already successful
|
||||||
Write-Host "Testing connection readiness of '$friendlyName'... " -NoNewline
|
$removeFromTargetComputers | ForEach-Object {
|
||||||
|
$removeName = $_
|
||||||
|
$targetComputers.Remove(($targetComputers | Where-Object { $_.name -eq $removeName })) | Out-Null
|
||||||
}
|
}
|
||||||
if (!(Test-NetConnection -ComputerName $fqdn -Port $ConnectivityPort -InformationLevel Quiet -WarningAction SilentlyContinue -ErrorAction SilentlyContinue -ErrorVariable connectionError))
|
|
||||||
|
# sleep until next test interval and increase time counter
|
||||||
|
$connCheckTotalTime += $ConnectivityCheckInterval
|
||||||
|
if ($connCheckTotalTime -le $ConnectivityCheckTimeout -and $targetComputers.Count -gt 0)
|
||||||
{
|
{
|
||||||
Write-Host -ForegroundColor Red "[ERROR]"
|
for ($i = $ConnectivityCheckInterval; $i -gt 0; $i--) {
|
||||||
Write-Host -ForegroundColor Red "`tAdditional information:", $connectionError[-1].ToString()
|
Write-Progress -Activity "Waiting for next connectivity check..." -SecondsRemaining $i
|
||||||
|
Start-Sleep 1
|
||||||
|
}
|
||||||
|
Write-Progress -Activity "Waiting for next connectivity check..." -Completed
|
||||||
}
|
}
|
||||||
else
|
} while ($connCheckTotalTime -le $ConnectivityCheckTimeout -and $targetComputers.Count -gt 0)
|
||||||
{
|
|
||||||
Write-Host -ForegroundColor Green "[OK]"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# exit gracefully
|
# exit gracefully
|
||||||
exitGracefully
|
exitGracefully
|
Loading…
Reference in New Issue