Last active
July 26, 2017 22:01
-
-
Save Andersama/10f74885fa4a905aa0d729ef0ecd660c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Modified from https://gist.github.com/larsks/4011808 | |
#Waits forever if $tries is not > 0 | |
function Wait-ForNetwork () { | |
param( | |
[int]$tries | |
) | |
$try = 0 | |
while (1) { | |
# Get a list of interfaces | |
$z = gwmi -class Win32_NetworkAdapterConfiguration; | |
# Filter out DHCP enabled devices that have a default gateway | |
$x = $z | where { $_.DHCPEnabled -eq $true -and $_.DefaultIPGateway -ne $null } | |
# If there is (at least) one DHCP enabled device available, exit the loop. | |
if ( ($x | measure).count -gt 0 ) { | |
break | |
} | |
# Filter out DHCP disabled devices which have the gateway and ipaddress specified | |
$y = $z | where { $_.DHCPEnabled -eq $false -and $_.DefaultIPGateway -ne $null -and $_.IPAddress -ne $null} | |
# If there is (at least) one DHCP disabled device w/ an ip and gateway available, exit the loop. | |
if ( ($y | measure).count -gt 0 ) { | |
break | |
} | |
# If $tries > 0 and we have tried $tries times without | |
# success, throw an exception. | |
if ( $tries -gt 0 -and $try++ -ge $tries ) { | |
throw "Network unavailable after $try tries." | |
} | |
# Wait one second. | |
if ( $tries -gt 0 ){ | |
$j = ($try/$tries)*100; | |
Write-Progress -Id 1 -Activity "Waiting for network (Max $tries attempts)" -Status "Checking for connection" -PercentComplete $j -CurrentOperation "Try # $try" | |
} else { | |
Write-Progress -Id 1 -Activity "Waiting for network (indefinite)" -Status "Checking for connection" | |
} | |
start-sleep -s 1 | |
} | |
} | |
function Wait-ForNetworkLocation(){ | |
param( | |
[int]$tries, | |
[string]$path | |
) | |
$try = 0; | |
while(1){ | |
# Get a list of interfaces | |
$z = gwmi -class Win32_NetworkAdapterConfiguration; | |
# Filter out DHCP enabled devices that have a default gateway | |
$x = $z | where { $_.DHCPEnabled -eq $true -and $_.DefaultIPGateway -ne $null } | |
# If there is (at least) one DHCP enabled device available, exit the loop. | |
if ( ($x | measure).count -gt 0 ) { | |
break | |
} | |
# Filter out DHCP disabled devices which have the gateway and ipaddress specified | |
$y = $z | where { $_.DHCPEnabled -eq $false -and $_.DefaultIPGateway -ne $null -and $_.IPAddress -ne $null} | |
# If there is (at least) one DHCP disabled device w/ an ip and gateway available, exit the loop. | |
if ( ($y | measure).count -gt 0 ) { | |
break | |
} | |
# If $tries > 0 and we have tried $tries times without | |
# success, throw an exception. | |
if ( $tries -gt 0 -and $try++ -ge $tries ) { | |
throw "Network unavailable after $try tries." | |
} | |
if ( $tries -gt 0 ){ | |
$j = ($try/$tries)*100; | |
Write-Progress -Id 1 -Activity "Waiting for network (Max $tries attempts)" -Status "Checking for network" -PercentComplete $j -CurrentOperation "Try # $try" | |
} else { | |
Write-Progress -Id 1 -Activity "Waiting for network (indefinite)" -Status "Checking for network" | |
} | |
# Wait one second. | |
start-sleep -s 1 | |
} | |
#Check to ensure that the path exists | |
while(1){ | |
if(Test-Path $path){ | |
break | |
} | |
if ( $tries -gt 0 -and $try++ -ge $tries ) { | |
throw "Network location $path unavailable after $try tries." | |
} | |
if ( $tries -gt 0 ){ | |
$j = ($try/$tries)*100 | |
Write-Progress -Id 1 -Activity "Waiting for network location (Max $tries attempts)" -Status "Checking for network location" -PercentComplete $j -CurrentOperation "Try # $try" | |
} else { | |
Write-Progress -Id 1 -Activity "Waiting for network location (indefinite)" -Status "Checking for network location" | |
} | |
# Wait one second. | |
start-sleep -s 1 | |
} | |
} | |
function Assert-Network(){ | |
Wait-ForNetwork -tries 0 | |
} | |
function Assert-NetworkLocation(){ | |
param( | |
[string]$path | |
) | |
Wait-ForNetworkLocation -tries 0 -path $path | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment