title: Obtaining IP and change octet - script looping - why?
body: <p>I have a script which obtains the IP of a secondary NIC, changes the 2nd octet to 30, then takes the new IP, changes the last octet to .254 then applies this settings to the NIC (along with sub etc)</p>
  
  <p>Everything works fine but, if I un-comment the .echo which I was using to check the IP's I can see the script actually runs through twice and then stops - why is that?</p>
  
  <pre><code>Dim HostIPAddress : HostIPAddress = ""
  Dim objWMIService : Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
  Dim colAdapters : Set colAdapters = objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
  Dim objAdapter
  name = "NIC2"
  
  Set wmi = GetObject("winmgmts://./root/cimv2")
  
  deviceQry = "SELECT * FROM Win32_NetworkAdapter " &amp; _
              "WHERE NetConnectionId = '" &amp; name &amp; "'"
  For Each adapter In wmi.ExecQuery(deviceQry)
    addressQry = "SELECT * FROM Win32_NetworkAdapterConfiguration " &amp; _
                 "WHERE Index = " &amp; adapter.DeviceId
    For Each config In wmi.ExecQuery(addressQry)
      If Not IsNull(config.IPAddress) Then
        HostIPAddress = Trim(config.IPAddress(0))
        Exit For
      End If
    Next
  Next
  
  sIP = HostIPAddress
  aIP = Split(sIP, ".")
  ' WScript.Echo Join(aIP, "-")
   aIP(1) = 30
  ' WScript.Echo Join(aIP, ".")
  
  dIP = Join(aIP, ".")
  eIP = Split(dIP, ".")
  ' WScript.Echo Join(eIP, "-")
   eIP(3) = 254
  ' WScript.Echo Join(eIP, ".")
  
  Set WshShell = WScript.CreateObject("WScript.Shell")
  If WScript.Arguments.length = 0 Then
  Set ObjShell = CreateObject("Shell.Application")
  ObjShell.ShellExecute "wscript.exe", """" &amp; _
  WScript.ScriptFullName &amp; """" &amp;_
   " RunAsAdministrator", , "runas", 1
  Else
  end if 
  
  Dim strIPAddress
  Dim strSubnetMask
  Dim strGateway
  Dim intGatewayMetric
  Dim strDns1
  Dim strDns2
  
  strIPAddress = Join(aIP, ".")
  strSubnetMask = "255.255.255.0"
  strGateway = Join(eIP, ".")
  intGatewayMetric = 1
  strDns1 = "123.456.789.17 "
  strDns2 = "123.456.789.18"
  
  Set objShell = WScript.CreateObject("Wscript.Shell")
  objShell.Run "netsh interface ip set address name=""NIC2"" static " &amp; strIPAddress &amp; " " &amp; strSubnetMask &amp; " " &amp; strGateway &amp; " " &amp; intGatewayMetric, 0, True
  objShell.Run "netsh interface ip set dns name=""NIC2"" static "&amp; strDns1, 0, True
  objShell.Run "netsh interface ip add dns name=""NIC2"" addr="&amp; strDns2, 0, True
  Set objShell = Nothing
  
  WScript.Quit
  </code></pre>