Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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 …

749 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