Jump to content

vb script changing multiple computers ip address


Recommended Posts


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

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 If

i'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 Next

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("192.168.1.50")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.1.1")
strGatewayMetric = Array(1)


On Error Resume Next
For Each objNetAdapter in colNetAdapters
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
Next


On Error Resume Next

Link to comment
Share on other sites

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 code

strComputer = "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

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 code

strComputer = "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

i 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

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 Next
Dim arrDNSServers, colNetCards, objNetCard, objWMIService
Dim 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

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 Next
Dim arrDNSServers, colNetCards, objNetCard, objWMIService
Dim 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

about 200 computers....

Link to comment
Share on other sites

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 process

it contents in your script.

Example ComputerList.txt

Computer01
Computer02
Computer03
Computer04
Computer05
Computer06
Computer07
Computer08
Computer09
Computer10

On Error Resume Next
Dim Act, C1, Fso, Ts, Txt
Dim arrDNSServers, colNetCards, objNetCard, objWMIService
Dim 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 If

The 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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...