Jump to content
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble

MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, register and become a site sponsor/subscriber and ads will be disabled automatically. 


net_user

vb script changing multiple computers ip address

Recommended Posts

i need to change several computers on my network that are using static ip address.

i've created a vbs script to change the ip address.

the problem is pc1 will change...but the script will not continue to pc2

here is my code.

what am i missing to make the script continue to pc2?

thanks

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

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

For Each objNetCard in colNetCards
arrDNSServers = Array("192.168.1.1", "192.168.1.1")
objNetCard.SetDNSServerSearchOrder(arrDNSServers)
Next

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

Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

strIPAddress = Array("192.168.1.100")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.1.1")
strGatewayMetric = Array(1)

For Each objNetAdapter in colNetAdapters
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
If errEnable = 0 Then
WScript.Echo "The IP address has been changed."
Else
WScript.Echo "The IP address could not be changed."
End If
Next

On Error Resume Next






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

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

For Each objNetCard in colNetCards
arrDNSServers = Array("192.168.1.1", "192.168.1.1")
objNetCard.SetDNSServerSearchOrder(arrDNSServers)
Next

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

Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

strIPAddress = Array("192.168.1.101")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.1.1")
strGatewayMetric = Array(1)

For Each objNetAdapter in colNetAdapters
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
If errEnable = 0 Then
WScript.Echo "The IP address has been changed."
Else
WScript.Echo "The IP address could not be changed."
End If
Next


On Error Resume Next

Share this post


Link to post
Share on other sites

Maybe try making a array that holds the computers names. Here is a rewrite of your script.I have not tested it.

 Dim C1, Col, Computer, ObjCard, StrCard, Wmi
Dim strGateway, strIPAddress,strGatewayMetric,strSubnetMask
'-> Array For Computers Names
Computer = Array("pc1","pc2")
C1 = 0
'-> Process Computers Array
For Each Col In Computer

Set Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & Computer & "\root\cimv2")

Set ObjCard = Wmi.ExecQuery ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

strIPAddress = Array("192.168.1.10" & C1)
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.1.1")
strGatewayMetric = Array(1)

For Each StrCard In ObjCard

errGateways = StrCard.SetGateways(strGateway, strGatewaymetric)
errEnable = StrCard.EnableStatic(strIPAddress, strSubnetMask)

If errEnable = 0 Then
WScript.Echo "The IP address has been changed."
Else
WScript.Echo "The IP address could not be changed."
End If

Next

C1 = C1 + 1
Next

Share this post


Link to post
Share on other sites

i would like to stick to my original way if possible...if not i would understand...

i got the script from here

link

i'm actually changing the subnet also on these computers...

going from a 192.168.1.x to a 172.16.1.x

255.255.255.0 to 255.255.0.0

the original script will report back that the ip has changed.

i can do without this function. (actually i think this is what is "bombing the script")

so what section of the script towards the end do i take out for the "reporting".....and have the script continue to the next computer?

Edited by net_user

Share this post


Link to post
Share on other sites
Where does it error at if you remove the On Error Resume Next line?

pc1 will change...but pc2 will not change

Share this post


Link to post
Share on other sites
Where does it error at if you remove the On Error Resume Next line?

pc1 will change...but pc2 will not change

But does it give an error somewhere when it reaches that half of the script?

Share this post


Link to post
Share on other sites
Where does it error at if you remove the On Error Resume Next line?

pc1 will change...but pc2 will not change

But does it give an error somewhere when it reaches that half of the script?

no it does not.

it's like the script doesn't reach the 2nd half to change pc2

Share this post


Link to post
Share on other sites

Are both PC1 and PC2 remote machines? Your not running and testing the script from PC1.

If you run just the second half for PC2 and don't do PC1 does it work?

Share this post


Link to post
Share on other sites
Are both PC1 and PC2 remote machines? Your not running and testing the script from PC1.

If you run just the second half for PC2 and don't do PC1 does it work?

yes they are both remote machines.

I ran just the 2nd half for pc2 and it works.

Share this post


Link to post
Share on other sites
i would like to stick to my original way if possible...if not i would understand...

i got the script from here

link

What you actually have is two scripts mashed together (both of which can actually stand on their own) and then repeated. This can make debugging a bit more difficult. One of the reasons Gunsmokingman provided the script earlier.

Script1:

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

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

For Each objNetCard in colNetCards
arrDNSServers = Array("192.168.1.1", "192.168.1.1")
objNetCard.SetDNSServerSearchOrder(arrDNSServers)
Next

Script2:

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

Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

strIPAddress = Array("192.168.1.101")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.1.1")
strGatewayMetric = Array(1)

For Each objNetAdapter in colNetAdapters
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
If errEnable = 0 Then
WScript.Echo "The IP address has been changed."
Else
WScript.Echo "The IP address could not be changed."
End If
Next

The first three lines of which do exactly the same thing, the only difference is the variable colNetAdapters and colNetCards, but they contain the same data once the query finishes:

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

Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

The following lines since they are not changing can also be set once and left alone rather than reset for each pass:

	arrDNSServers = Array("192.168.1.1", "192.168.1.1")
<snip>
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.1.1")
strGatewayMetric = Array(1)

So if we change colNetadapters to colNetCards and move the static variables to the beginning we can get this and do the same thing:

arrDNSServers = Array("192.168.1.1", "192.168.1.1")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.1.1")
strGatewayMetric = Array(1)

StrComputer = "pc2"
StrIPAddress = Array("192.168.1.101")

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

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

For Each objNetCard in colNetCards
objNetCard.SetDNSServerSearchOrder(arrDNSServers)
errGateways = objNetCard.SetGateways(strGateway, strGatewaymetric)
errEnable = objNetCard.EnableStatic(strIPAddress, strSubnetMask)
If errEnable = 0 Then
WScript.Echo "The IP address has been changed."
Else
WScript.Echo "The IP address could not be changed."
End If
Next

Now since we are reusing the last 2/3 of the script for each machine we could either put it in a loop or make it a function. Gunsmokingman modified it and put it in a loop, I'll do a function. So we first set the variables that don't change.

Then set up the computer name variable and it's IP address, then call the function, Repeat these three lines as needed for each machine.

strComputer = "pc1"
strIPAddress = Array("192.168.1.100")
ChangeNetCard

The only additional thing I have added is to destroy the variables used for the WMI query at the end of the function, while not required I have seen occasional issues when they are not destroyed and you are running scripts against multiple computers:

arrDNSServers = Array("192.168.1.1", "192.168.1.1")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.1.1")
strGatewayMetric = Array(1)

strComputer = "pc1"
strIPAddress = Array("192.168.1.100")
ChangeNetCard

strComputer = "pc2"
strIPAddress = Array("192.168.1.101")
ChangeNetCard

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

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

For Each objNetCard in colNetCards
objNetCard.SetDNSServerSearchOrder(arrDNSServers)
errGateways = objNetCard.SetGateways(strGateway, strGatewaymetric)
errEnable = objNetCard.EnableStatic(strIPAddress, strSubnetMask)
If errEnable = 0 Then
WScript.Echo "The IP address has been changed."
Else
WScript.Echo "The IP address could not be changed."
End If
Next
set objWMIService = Nothing
Set colNetcards = Nothing
end Function

See if you can get any further with this.

Share this post


Link to post
Share on other sites

same thing...the first computer will change...but that's it...

o...i do get a pop up that i have to click ok on....

not just from your modified script....but from my original script as well.

---------------------------

Windows Script Host

---------------------------

Script: C:\change.vbs

Line: 27

Char: 5

Error: The remote procedure call failed.

Code: 800706BE

Source: SWbemObjectEx

---------------------------

OK

---------------------------

i think because after i change the ip address....i can no longer communicate with the computer...ping it...ect

if i can get this error out of my script i think i will be ok.

Edited by IcemanND
No need to quote the previous post, especially when a mile long

Share this post


Link to post
Share on other sites

That would be the error message I was asking for earlier.

Which script is the posted error from and what line is 27?

Share this post


Link to post
Share on other sites
That would be the error message I was asking for earlier.

Which script is the posted error from and what line is 27?

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

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

For Each objNetCard in colNetCards
arrDNSServers = Array("208.67.222.222", "208.67.220.220")
objNetCard.SetDNSServerSearchOrder(arrDNSServers)
Next

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

Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

strIPAddress = Array("172.16.1.2")
strSubnetMask = Array("255.255.0.0")
strGateway = Array("172.16.0.1")
strGatewayMetric = Array(1)

For Each objNetAdapter in colNetAdapters
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
line 27-----errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
If errEnable = 0 Then
WScript.Echo "The IP address has been changed."
Else
WScript.Echo "The IP address could not be changed."
End If
Next

Edited by net_user

Share this post


Link to post
Share on other sites

Don't really have time to go over it or to try it out, but I figured I'd let you know you can actually debug scripts, by adding //X as a parameter i.e. "yourscriptnamehere.vbs //X" (w/o the quotes...). If you don't have visual studio (express eds are free BTW) or another suitable debugger (many good tools out there), then there's the old script debugger from here. Notice I didn't say to try to run the script inside the debugger using the //X parameter -- you run the script (from the run box, cmd prompt or whatever) with that parameter, then your script debugger (whatever you chose to use) will popup.

There's a large number of people who do a lot of scripting that don't even know about that one, so I'd figure it would probably also benefit others.

The RPC error is likely due to the TCP/IP connection being broken as the adapter changes its IP.

Share this post


Link to post
Share on other sites
The RPC error is likely due to the TCP/IP connection being broken as the adapter changes its IP.

Thats what i think...after the remote pc changes it's ip i can no longer communicate with it.

How do i edit my script for no errors?

so it can continue on?

I'm using the Microsoft script editor, and i'm getting the same error.

Edited by net_user

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...