Skip to content

Instantly share code, notes, and snippets.

@larsks
Created November 4, 2012 12:56
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save larsks/4011808 to your computer and use it in GitHub Desktop.
Save larsks/4011808 to your computer and use it in GitHub Desktop.
Wait until networking is available with PowerShell
# 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