[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2010-08-19
6
Medium Priority
?
2,864 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
5 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 2000 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 2000 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 143

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses
Course of the Month19 days, 12 hours left to enroll

873 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