By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,255 Members | 2,773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,255 IT Pros & Developers. It's quick & easy.

Help with VBScript

P: 2
All,

I have been working with a scripter on writing a script to change the ipaddress of a network interface on a windows server. We think the script is right but when I run it I get an error that says "Invalid Parameter". They guy who has been writing the script for me says that the issue is

"
It seems that the EnableStatic method is not allowing the dynamic array created like,

Dim arrIPAddress()

It allows only the static Arrays created using Arrays() like

arrIPAddress = Arrays("192.168.10.11")
"

Any help would be great. Below you will find a copy of the code.

Don

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,(Shutdown)}!\ \" & strComputer & "\root\cimv2")

Set colNetAdapters = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

Dim arrIPAddress()

strGatewayMetric = Array(1)

strSubnetMask1 = Array("255.255.255.0")
strSubnetMask2 = Array("255.255.0.0")

strGateway1 = Array("192.168.30.1")
arrDNSServers1 = Array("192.168.30.21", "192.168.30.22")
strWINSPrimaryServer1 = "192.168.30.21"
strWINSSecondaryServer1 = "192.168.30.22"

strGateway2 = Array("172.16.17.1")
arrDNSServers2 = Array("172.16.17.21", "172.16.17.22")
strWINSPrimaryServer2 = "172.16.17.21"
strWINSSecondaryServer2 = "172.16.17.22"

strGateway3 = Array("10.0.1.1 ")
arrDNSServers3 = Array("10.0.1.21", "10.0.1.22")
strWINSPrimaryServer3 = "10.0.1.21"
strWINSSecondaryServer3 = "10.0.1.22"

strSubnetMask = Array("")
strGateway = Array("")
arrDNSServers = Array("", "")
strWINSPrimaryServer = ""
strWINSSecondaryServer = ""

For Each objNetAdapter in colNetAdapters
count = 0
ReDim arrIPAddress(0)

strSubnet = objNetAdapter.IPSubnet(0)
If strSubnet <> "" Then
subnet = Split(strSubnet, ".")

For Each strAddress in objNetAdapter.IPAddress

address = Split(strAddress, ".")
strNetworkId = ""
For i = LBound(address) to UBound(address)
IPByte = address(i) And subnet(i)
strNetworkId = strNetworkId & IPByte
if i <> UBound(address) Then
strNetworkId = strNetworkId & "."
End If
Next

strHostId = ""
networdId = Split(strNetworkId, ".")
For i = LBound(address) to UBound(address)
IPByte = address(i) Xor networdId(i)
strHostId = strHostId & IPByte
if i <> UBound(address) Then
strHostId = strHostId & "."
End If
Next

if strNetworkId = "128.222.82.0" Then
strNetworkId = "128.222.92.0"
strSubnetMask = strSubnetMask1
strGateway = strGateway1
arrDNSServers = arrDNSServers1
strWINSPrimaryServer = strWINSPrimaryServer1
strWINSSecondaryServer = strWINSSecondaryServer1
End If

if strNetworkId = "172.16.0.0" Then
strNetworkId = "172.17.0.0"
strSubnetMask = strSubnetMask2
strGateway = strGateway2
arrDNSServers = arrDNSServers2
strWINSPrimaryServer = strWINSPrimaryServer2
strWINSSecondaryServer = strWINSSecondaryServer2
End If

if strNetworkId = "10.10.0.0" Then
strNetworkId = "10.0.0.0"
strGateway = strGateway3
strSubnetMask = strSubnetMask3
arrDNSServers = arrDNSServers3
strWINSPrimaryServer = strWINSPrimaryServer3
strWINSSecondaryServer = strWINSSecondaryServer3
End If

networkId = Split(strNetworkId, ".")
hostId = Split(strHostId, ".")
strNewIPAddress = ""
For i = LBound(networkId) to UBound(networkId)
IPByte = networkId(i) Or hostId(i)
strNewIPAddress = strNewIPAddress & IPByte
if i <> UBound(networkId) Then
strNewIPAddress = strNewIPAddress & "."
End If
Next

ReDim arrIPAddress(count+1)

arrIPAddress(count) = strNewIPAddress
count = count + 1
Next

errAddress = objNetAdapter.EnableStatic( _
arrIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(_
strGateway, strGatewaymetric)
errDNS = objNetAdapter.SetDNSServerSearchOrder(_
arrDNSServers)
errWINS = objNetAdapter. SetWINSServer(_
strWINSPrimaryServer, strWINSSecondaryServer)
End If
Next

' This will reboot the system.
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")

For Each objOperatingSystem in colOperatingSystems
ObjOperatingSystem.Reboot()
Next
May 8 '07 #1
Share this Question
Share on Google+
1 Reply


danp129
Expert 100+
P: 321
The problem I found was that you were making the arrIPAddress array larger even if there was no new IP address for it to hold and it didn't like that. I think I changed all the IP's back to what you had after testing but you should double check.

Expand|Select|Wrap|Line Numbers
  1. strComputer = "."
  2.  
  3. Set objWMIService = GetObject("winmgmts:" _
  4. & "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")
  5.  
  6. Set colNetAdapters = objWMIService.ExecQuery _
  7. ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
  8.  
  9. Dim arrIPAddress()
  10.  
  11. strGatewayMetric = Array(1)
  12.  
  13. strSubnetMask1 = Array("255.255.255.0")
  14. strSubnetMask2 = Array("255.255.0.0")
  15.  
  16. strSubnetMask3 = Array("255.255.255.0") 'ADDED
  17.  
  18. strGateway1 = Array("192.168.30.1")
  19. arrDNSServers1 = Array("192.168.30.21", "192.168.30.22")
  20. strWINSPrimaryServer1 = "192.168.30.21"
  21. strWINSSecondaryServer1 = "192.168.30.22"
  22.  
  23. strGateway2 = Array("172.16.17.1")
  24. arrDNSServers2 = Array("172.16.17.21", "172.16.17.22")
  25. strWINSPrimaryServer2 = "172.16.17.21"
  26. strWINSSecondaryServer2 = "172.16.17.22"
  27.  
  28. strGateway3 = Array("10.0.1.1")
  29. arrDNSServers3 = Array("10.0.1.21", "10.0.1.22")
  30. strWINSPrimaryServer3 = "10.0.1.21"
  31. strWINSSecondaryServer3 = "10.0.1.22"
  32.  
  33. strSubnetMask = Array("")
  34. strGateway = Array("")
  35. arrDNSServers = Array("", "")
  36. strWINSPrimaryServer = ""
  37. strWINSSecondaryServer = ""
  38.  
  39. For Each objNetAdapter in colNetAdapters
  40.     count = 0
  41.     ReDim arrIPAddress(0)
  42.  
  43.     strSubnet = objNetAdapter.IPSubnet(0)
  44.     If strSubnet <> "" Then
  45.         subnet = Split(strSubnet, ".")
  46.  
  47.         For Each strAddress in objNetAdapter.IPAddress
  48.             ReDim arrIPAddress(count) 'This line changed and moved
  49.             address = Split(strAddress, ".")
  50.             strNetworkId = ""
  51.             For i = LBound(address) to UBound(address)
  52.                 IPByte = address(i) And subnet(i)
  53.                 strNetworkId = strNetworkId & IPByte
  54.                 if i <> UBound(address) Then
  55.                     strNetworkId = strNetworkId & "."
  56.                 End If
  57.             Next
  58.  
  59.             strHostId = ""
  60.             networdId = Split(strNetworkId, ".")
  61.             For i = LBound(address) to UBound(address)
  62.                 IPByte = address(i) Xor networdId(i)
  63.                 strHostId = strHostId & IPByte
  64.                 if i <> UBound(address) Then
  65.                     strHostId = strHostId & "."
  66.                 End If
  67.             Next
  68.  
  69.             if strNetworkId = "128.222.82.0" Then
  70.                 strNetworkId = "128.222.92.0"
  71.                 strSubnetMask = strSubnetMask1
  72.                 strGateway = strGateway1
  73.                 arrDNSServers = arrDNSServers1
  74.                 strWINSPrimaryServer = strWINSPrimaryServer1
  75.                 strWINSSecondaryServer = strWINSSecondaryServer1
  76.             End If
  77.  
  78.             if strNetworkId = "172.16.0.0" Then
  79.                 strNetworkId = "172.17.0.0"
  80.                 strSubnetMask = strSubnetMask2
  81.                 strGateway = strGateway2
  82.                 arrDNSServers = arrDNSServers2
  83.                 strWINSPrimaryServer = strWINSPrimaryServer2
  84.                 strWINSSecondaryServer = strWINSSecondaryServer2
  85.             End If
  86.  
  87.             if strNetworkId = "10.10.0.0" Then
  88.                 strNetworkId = "10.0.0.0"
  89.                 strGateway = strGateway3
  90.                 strSubnetMask = strSubnetMask3
  91.                 arrDNSServers = arrDNSServers3
  92.                 strWINSPrimaryServer = strWINSPrimaryServer3
  93.                 strWINSSecondaryServer = strWINSSecondaryServer3
  94.             End If
  95.  
  96.             networkId = Split(strNetworkId, ".") 
  97.             hostId = Split(strHostId, ".") 
  98.             strNewIPAddress = ""
  99.             For i = LBound(networkId) to UBound(networkId)
  100.                 IPByte = networkId(i) Or hostId(i)
  101.                 strNewIPAddress = strNewIPAddress & IPByte
  102.                 if i <> UBound(networkId) Then
  103.                     strNewIPAddress = strNewIPAddress & "."
  104.                 End If
  105.             Next 
  106.  
  107.             arrIPAddress(count) = strNewIPAddress
  108.             count = count + 1
  109.  
  110.             errAddress = objNetAdapter.EnableStatic(_
  111.             arrIPAddress, strSubnetMask)
  112.             errGateways = objNetAdapter.SetGateways(_
  113.             strGateway, strGatewaymetric)
  114.             errDNS = objNetAdapter.SetDNSServerSearchOrder(_
  115.             arrDNSServers)
  116.             errWINS = objNetAdapter.SetWINSServer(_
  117.             strWINSPrimaryServer, strWINSSecondaryServer)
  118.         Next
  119.     End If
  120. Next
  121.  
  122. ' This will reboot the system.
  123. Set colOperatingSystems = objWMIService.ExecQuery _
  124. ("Select * from Win32_OperatingSystem")
  125.  
  126. For Each objOperatingSystem in colOperatingSystems
  127. ObjOperatingSystem.Reboot()
  128. Next
May 9 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.