Solved

Change the IP, DNS, Gateway and wins on multiple computers using WMI

Posted on 2010-08-19
6
2,685 Views
Last Modified: 2013-11-08
I found this wmi script that works for changing the IP, netmask and gateway for one PC. I would like to modify it to change the IP, DNS1, DNS2, Subnet Mask, Gateway, and WINS for an array of PCs that already have static IP addresses.

Here is the Code I found

strComputer = "."
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.141")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.1.100")
strGatewayMetric = Array(1)
 
For Each objNetAdapter in colNetAdapters
    errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
    errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
    If errEnable = 0 Then
        WScript.Echo "The IP address has been changed."
    Else
        WScript.Echo "The IP address could not be changed."
    End If


The DNS, Subnet Mask, Gateway and WINS will be the same for all PCs just the IP address will be different. I would also like it to echo or save to a file what computers were changed.
0
Comment
Question by:khfinlay
  • 3
6 Comments
 
LVL 29

Expert Comment

by:matrixnz
ID: 33484252
How exactly will it be distributed?  Because if you're connecting remotely you'll lose the connection, once the IP is changed?  You could also just use NETSH to change all four.

Cheers
0
 

Author Comment

by:khfinlay
ID: 33485993
Matrixnz,
I can distribute however way I choose as I am system admin. What do you recommend?  I was thinking of rebooting all the PCs as well. How can I automate it withh NETSH
0
 
LVL 29

Accepted Solution

by:
matrixnz earned 500 total points
ID: 33489469
khfinlay

Basically you'd want to run the script locally on the machine, either have the script copied to the Temp Directory for example executed and then deleted, then it's not relying off the network at all.

For netsh
NETSH INT IP SET ADDRESS "Local Area Connection" STATIC 192.168.1.141 255.255.255.0 192.168.1.100 GWMETRIC=1
NETSH INT IP SET DNS "Local Area Connection" STATIC 192.168.1.100 ; Or what ever your Primary DNS is
NETSH INT IP SET DNS "Local Area Connection" 192.168.1.101 ; Or what ever you Secondary DNS is
NETSH INT IP SET WINS "Local Area Connection" STATIC 192.168.1.102 ; Or what ever your WINS Address is

0
 
LVL 29

Assisted Solution

by:matrixnz
matrixnz earned 500 total points
ID: 33489573
Another option, which will use the VB you posted above is to use AutoIT
Download and Install AutoIT from www.autoitscript.com

Open Scite Editor and paste the code below into it Save and then click Tools - Compile

Create an INI File as mentioned in the code below keep it alongside your compiled script
[Computer Information]
Computer1=192.168.1.1
Computer2=192.168.1.2
etc...

Run the compiled script it will go through check to find the computername = the computer in the ini file and then change the IP Address

Cheers
;External File called IPConfig.ini
;Must be saved relative to the main script
;[Computer Information]
;Computer1=192.168.1.1
;Computer2=192.168.1.2
;etc...


; The following command will create a 2D Array of IPConfig.ini
;$INI_LISTCOMP[$x][0] = Computer Name
;$INI_LISTCOMP[$x][1] = IP Address
Global $strSubnetMask = "255.255.255.0"
Global $strGateway = "192.168.1.100"
Global $strGatewayMetric = 1

$INI_LISTCOMP = IniReadSection('IPConfig.ini', 'Computer Information')
If @error = 1 Then MsgBox(16, 'Error', 'Unable to open ' & @ScriptDir & '\IPConfig.ini or [Computer Information] Section does not exist')

For $x = 1 To $INI_LISTCOMP[0][0]
	If $INI_LISTCOMP = @ComputerName Then
		$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $INI_LISTCOMP[$x][0] & "\root\cimv2")
		$colNetAdapters = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
		For $objNetAdapter in $colNetAdapters
			$errEnable = $objNetAdapter.EnableStatic($INI_LISTCOMP[$x][1], $strSubnetMask)
			$errGateways = $objNetAdapter.SetGateways($strGateway, $strGatewaymetric)
			If $errEnable = 0 Then
				MsgBox(64, 'Success', "The IP address has been changed.")
			Else
				MsgBox(16, 'Error', "The IP address could not be changed.")
			EndIf
		Next
	EndIf
Next

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34986394
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question