Do until loop not ending!

by fangol at 2013-02-06 04:46:50

Hi Guys

I’m sure this is something obvious but it’s been driving me nuts! Here’s what I am trying to do

I have a VM that has 2 NIC’s, one is a legacy NIC and one is virtual. We are having an issue where the legacy NIC comes up before the virtual NIC, the legacy NIC is used for PVS and therefore does not have a default gateway. We have other scripts running that should only start when the virtual NIC is UP. So I put together a script that will loop until the virtual NIC obtains a default gateway but although I tested it and I’m sure it worked before now it is not! Can anyone help?


do{Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object{$.description -like “Virtual”}|Select description,DefaultIPGateway}
until ($DefaultIPGateway -like “192”)

So when I run this I have a default gateway of 192.168.1.1 on my virtual NIC therefore it should end but it runs in an infinite loop!

I tried this instead
$gateway= Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object{$
.description -like “virtual” -and $defaultipgateway -eq $null}|Select description,DefaultIPGateway
Do{write-host $gateway}
until ($gateway -ne $null)

But again an infinite loop… any ideas?
by Klaas at 2013-02-06 06:58:42
If the code you wrote above is literally what you execute, then I suggest you add a dot ‘.’:
$
.defaultipgateway
Without it you create an empty variable that will always -eq $null.
by fangol at 2013-02-08 05:41:35
Sorry I did try it with $.defaultgateway but that looped too, is there anything I need to do to like a | to get the value from the do{Get-WmiObject Win32_NetworkAdapterConfiguration query?
by Klaas at 2013-02-08 07:12:50
Did you try with the first version?
In the second version of your script, the get-wmi is outside the do loop, so it will never change the value you got at the start of the script. You have to check for the current value with every iteration to get the new status.
I suggest you also insert a start-sleep for 5 or 10 seconds in the loop.
by fangol at 2013-02-14 06:07:52
Yes I did, i’ve also tried with -eq $Null and -ne $Null but no luck there either. SO the 2nd statement could be :-

Do{$gateway= Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object{$.description -like “virtual” -and $defaultipgateway -eq $null}|Select description,DefaultIPGateway
write-host $gateway}
until ($gateway -ne $null)
by mjolinor at 2013-02-14 07:56:53
I think the problem is that you’re testing for $, and that’s not going to work outside of the Where script block (that pipeline ended with the script block).

Does this work better?

do{
$config =
Get-WmiObject Win32_NetworkAdapterConfiguration |
Where-Object{$
.description -like “Virtual”}|
Select description,DefaultIPGateway
start-sleep -Milliseconds 100
}

until ($config.DefaultIPGateway -like “192”)
by fangol at 2013-02-14 08:23:34
Hey thanks for this, but it still loops. It can find the value outside of the loop but not using the until statement. Does this work for you?
by Klaas at 2013-02-14 09:18:36
Can you add a line in the loop and write $config.defaultIPGateway to the screen every iteration?