ReDucTor Posted June 5, 2009 Share Posted June 5, 2009 why not use a batch file for this? with netsh? Link to comment Share on other sites More sharing options...
net_user Posted June 5, 2009 Author Share Posted June 5, 2009 why not use a batch file for this? with netsh?because i want to execute my script remotely Link to comment Share on other sites More sharing options...
gunsmokingman Posted June 7, 2009 Share Posted June 7, 2009 Have you tried to add On Error Resume Next that should stop the error message.Here is a script that will ping PC1 computers using WMI, this will only work on XP and up. Dim StrComputer, Wmi Dim arrDNSServers, Col, Obj Dim strIPAddress, strSubnetMask Dim strGateway,strGatewayMetric Dim errEnable, errGateways StrComputer = "PC1" Set Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set Col = Wmi.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True") For Each Obj In Col arrDNSServers = Array("208.67.222.222", "208.67.220.220") Obj.SetDNSServerSearchOrder(arrDNSServers) Next '-> Change The IP ChangeIP() Function ChangeIP() strIPAddress = Array("172.16.1.2") strSubnetMask = Array("255.255.0.0") strGateway = Array("172.16.0.1") strGatewayMetric = Array(1) On Error Resume Next For Each Obj In Col errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric) errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask) If errEnable = 0 Then WScript.Echo "The IP address has been changed." PingComputer() Else WScript.Echo "The IP address could not be changed." End If Next End Function'-> Ping The Computer Function PingComputer() Set Wmi = GetObject("winmgmts:\\.\root\cimv2") Set Col = Wmi.ExecQuery("Select * From Win32_PingStatus where Address = '" & strIPAddress & "'") For Each Obj In Col If IsNull(Obj.StatusCode) Or Obj.StatusCode <> 0 Then WScript.Echo "Computer Offline : " & Obj Else Wscript.Echo "Computer Confirm : " & Obj End If Next End Function Link to comment Share on other sites More sharing options...
net_user Posted June 9, 2009 Author Share Posted June 9, 2009 i added the "On Error Resume Next" right above the "For Each objNetAdapter in colNetAdapters"also took out this line of code If errEnable = 0 Then WScript.Echo "The IP address has been changed." Else WScript.Echo "The IP address could not be changed." End Ifi'm on a timeline because we are changing over next weekend.so i created a custom vbs script for each computer.my final code is as follow....strComputer = "PC1"On Error Resume NextSet 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("192.168.1.50")strSubnetMask = Array("255.255.255.0")strGateway = Array("192.168.1.1")strGatewayMetric = Array(1) On Error Resume NextFor Each objNetAdapter in colNetAdapters errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric) errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)NextOn Error Resume Next Link to comment Share on other sites More sharing options...
gunsmokingman Posted June 9, 2009 Share Posted June 9, 2009 Your code is really sloppy, you only need one of these in your code.Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colNetCards = objWMIService.ExecQuery _ ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")Clean up codestrComputer = "PC1"On Error Resume Next Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colNetCards = objWMIService.ExecQuery _ ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True") strIPAddress = Array("192.168.1.50") strSubnetMask = Array("255.255.255.0") strGateway = Array("192.168.1.1") strGatewayMetric = Array(1) For Each objNetCard in colNetCards arrDNSServers = Array("208.67.222.222", "208.67.220.220") objNetCard.SetDNSServerSearchOrder(arrDNSServers) errGateways = objNetCard.SetGateways(strGateway, strGatewaymetric) errEnable = objNetCard.EnableStatic(strIPAddress, strSubnetMask) Next Link to comment Share on other sites More sharing options...
net_user Posted June 9, 2009 Author Share Posted June 9, 2009 Your code is really sloppy, you only need one of these in your code.Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colNetCards = objWMIService.ExecQuery _ ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")Clean up codestrComputer = "PC1"On Error Resume Next Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colNetCards = objWMIService.ExecQuery _ ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True") strIPAddress = Array("192.168.1.50") strSubnetMask = Array("255.255.255.0") strGateway = Array("192.168.1.1") strGatewayMetric = Array(1) For Each objNetCard in colNetCards arrDNSServers = Array("208.67.222.222", "208.67.220.220") objNetCard.SetDNSServerSearchOrder(arrDNSServers) errGateways = objNetCard.SetGateways(strGateway, strGatewaymetric) errEnable = objNetCard.EnableStatic(strIPAddress, strSubnetMask) Nexti tried that...it only changed the dns.....then it stopped...remember, i am also changing subnets as well with this script.what i have is working.thanks for the help! Link to comment Share on other sites More sharing options...
gunsmokingman Posted June 9, 2009 Share Posted June 9, 2009 How many computers are you going to be running this script on?It also a good idea to dim all your objects in a script.strComputer = "PC1"On Error Resume NextDim arrDNSServers, colNetCards, objNetCard, objWMIServiceDim strIPAddress, strSubnetMask, strGateway, strGatewayMetric 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 strIPAddress = Array("192.168.1.50") strSubnetMask = Array("255.255.255.0") strGateway = Array("192.168.1.1") strGatewayMetric = Array(1) For Each objNetCard in colNetCards errGateways = objNetCard.SetGateways(strGateway, strGatewaymetric) errEnable = objNetCard.EnableStatic(strIPAddress, strSubnetMask) Next Link to comment Share on other sites More sharing options...
net_user Posted June 9, 2009 Author Share Posted June 9, 2009 How many computers are you going to be running this script on?It also a good idea to dim all your objects in a script.strComputer = "PC1"On Error Resume NextDim arrDNSServers, colNetCards, objNetCard, objWMIServiceDim strIPAddress, strSubnetMask, strGateway, strGatewayMetric 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 strIPAddress = Array("192.168.1.50") strSubnetMask = Array("255.255.255.0") strGateway = Array("192.168.1.1") strGatewayMetric = Array(1) For Each objNetCard in colNetCards errGateways = objNetCard.SetGateways(strGateway, strGatewaymetric) errEnable = objNetCard.EnableStatic(strIPAddress, strSubnetMask) Nextabout 200 computers.... Link to comment Share on other sites More sharing options...
gunsmokingman Posted June 9, 2009 Share Posted June 9, 2009 I hope you are not going to change the computer 200 times.What you need is a script that can read a text file and processit contents in your script.Example ComputerList.txtComputer01Computer02Computer03Computer04Computer05Computer06Computer07Computer08Computer09Computer10On Error Resume NextDim Act, C1, Fso, Ts, TxtDim arrDNSServers, colNetCards, objNetCard, objWMIServiceDim strComputer, strIPAddress, strSubnetMask, strGateway, strGatewayMetric Set Act = CreateObject("Wscript.Shell") Set Fso = CreateObject("Scripting.FileSystemObject") Txt = Act.CurrentDirectory & "\ComputerList.txt" If Fso.FileExists(Txt) Then Set Ts = Fso.OpenTextFile(Txt,1,True) Do Until Ts.AtEndOfStream strComputer = Ts.ReadLine C1 = C1 + 1 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 '-> New IpAddress Starts at 192.168.1.Plus C1 Total strIPAddress = Array("192.168.1." & C1) strSubnetMask = Array("255.255.255.0") strGateway = Array("192.168.1.1") strGatewayMetric = Array(1) For Each objNetCard in colNetCards errGateways = objNetCard.SetGateways(strGateway, strGatewaymetric) errEnable = objNetCard.EnableStatic(strIPAddress, strSubnetMask) Next Loop End IfThe above scripts read each line of the text file, and uses that line for the computer name.I added a counter, this is to have a different IP address for each computer. 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