获取IP并更改八位字节 - 脚本循环 - 为什么?

时间:2016-09-25 09:38:08

标签: vbscript

我有一个脚本获取辅助NIC的IP,将第二个八位字节更改为30,然后获取新IP,将最后一个八位字节更改为.254然后将此设置应用于NIC(以及子等)< / p>

一切正常但是,如果我取消评论我用来检查IP的.echo我可以看到脚本实际上运行了两次然后停止 - 为什么会这样?

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 " & _
            "WHERE NetConnectionId = '" & name & "'"
For Each adapter In wmi.ExecQuery(deviceQry)
  addressQry = "SELECT * FROM Win32_NetworkAdapterConfiguration " & _
               "WHERE Index = " & 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", """" & _
WScript.ScriptFullName & """" &_
 " 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 " & strIPAddress & " " & strSubnetMask & " " & strGateway & " " & intGatewayMetric, 0, True
objShell.Run "netsh interface ip set dns name=""NIC2"" static "& strDns1, 0, True
objShell.Run "netsh interface ip add dns name=""NIC2"" addr="& strDns2, 0, True
Set objShell = Nothing

WScript.Quit

0 个答案:

没有答案