Solved

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

Posted on 2010-08-19
6
2,634 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

705 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now