net_user Posted June 3, 2009 Share Posted June 3, 2009 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 pc2here is my code.what am i missing to make the script continue to pc2?thanksstrComputer = "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)NextstrComputer = "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 IfNextOn Error Resume NextstrComputer = "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)NextstrComputer = "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 IfNextOn Error Resume Next Link to comment Share on other sites More sharing options...
gunsmokingman Posted June 3, 2009 Share Posted June 3, 2009 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 Link to comment Share on other sites More sharing options...
IcemanND Posted June 3, 2009 Share Posted June 3, 2009 Where does it error at if you remove the On Error Resume Next line? Link to comment Share on other sites More sharing options...
net_user Posted June 3, 2009 Author Share Posted June 3, 2009 (edited) i would like to stick to my original way if possible...if not i would understand...i got the script from herelinki'm actually changing the subnet also on these computers...going from a 192.168.1.x to a 172.16.1.x255.255.255.0 to 255.255.0.0the 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 June 3, 2009 by net_user Link to comment Share on other sites More sharing options...
net_user Posted June 3, 2009 Author Share Posted June 3, 2009 Where does it error at if you remove the On Error Resume Next line?pc1 will change...but pc2 will not change Link to comment Share on other sites More sharing options...
IcemanND Posted June 3, 2009 Share Posted June 3, 2009 Where does it error at if you remove the On Error Resume Next line?pc1 will change...but pc2 will not changeBut does it give an error somewhere when it reaches that half of the script? Link to comment Share on other sites More sharing options...
net_user Posted June 3, 2009 Author Share Posted June 3, 2009 Where does it error at if you remove the On Error Resume Next line?pc1 will change...but pc2 will not changeBut 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 Link to comment Share on other sites More sharing options...
IcemanND Posted June 3, 2009 Share Posted June 3, 2009 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? Link to comment Share on other sites More sharing options...
net_user Posted June 3, 2009 Author Share Posted June 3, 2009 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. Link to comment Share on other sites More sharing options...
IcemanND Posted June 4, 2009 Share Posted June 4, 2009 i would like to stick to my original way if possible...if not i would understand...i got the script from herelinkWhat 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)NextScript2: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 IfNextThe 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 IfNextNow 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")ChangeNetCardstrComputer = "pc2"strIPAddress = Array("192.168.1.101")ChangeNetCardFunction 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 = Nothingend FunctionSee if you can get any further with this. Link to comment Share on other sites More sharing options...
net_user Posted June 4, 2009 Author Share Posted June 4, 2009 (edited) 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.vbsLine: 27Char: 5Error: The remote procedure call failed. Code: 800706BESource: SWbemObjectEx---------------------------OK ---------------------------i think because after i change the ip address....i can no longer communicate with the computer...ping it...ectif i can get this error out of my script i think i will be ok. Edited June 4, 2009 by IcemanND No need to quote the previous post, especially when a mile long Link to comment Share on other sites More sharing options...
IcemanND Posted June 4, 2009 Share Posted June 4, 2009 That would be the error message I was asking for earlier.Which script is the posted error from and what line is 27? Link to comment Share on other sites More sharing options...
net_user Posted June 4, 2009 Author Share Posted June 4, 2009 (edited) 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)NextstrComputer = "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 IfNext Edited June 4, 2009 by net_user Link to comment Share on other sites More sharing options...
CoffeeFiend Posted June 4, 2009 Share Posted June 4, 2009 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. Link to comment Share on other sites More sharing options...
net_user Posted June 4, 2009 Author Share Posted June 4, 2009 (edited) 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 June 4, 2009 by net_user Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now