Created
November 4, 2012 12:56
-
-
Save larsks/4011808 to your computer and use it in GitHub Desktop.
Wait until networking is available with PowerShell
This file contains hidden or 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
# Wait for a DHCP-enabled interface to develop | |
# a default gateway. | |
# | |
# usage: wait-for-network [ <tries> ] | |
function wait-for-network ($tries) { | |
while (1) { | |
# Get a list of DHCP-enabled interfaces that have a | |
# non-$null DefaultIPGateway property. | |
$x = gwmi -class Win32_NetworkAdapterConfiguration ` | |
-filter DHCPEnabled=TRUE | | |
where { $_.DefaultIPGateway -ne $null } | |
# If there is (at least) one available, exit the loop. | |
if ( ($x | 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 unavaiable after $try tries." | |
} | |
# Wait one second. | |
start-sleep -s 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment