Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.