Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vbs script to change all IP address via DNS name 1

Status
Not open for further replies.

unclegimoah

Programmer
Oct 25, 2007
3
GB
Hi Guys,

I have not used vb script for many years, we have just been taken over by a global company and we have to change all ip addresses, subnet, gateway and PDNS IP. I have a script to change the local ip settings, is it possible to change this script so that if I have a text file will IP and DNS names I can run a vbscript and it will change all ips. Please help.:

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
Next
 
Why don't you just configure all systems to use DHCP and push down your settings that way?

Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each objNetAdapter In colNetAdapters
    errEnable = objNetAdapter.EnableDHCP()
     If errEnable = 0 Then
        Wscript.Echo "DHCP has been enabled."
    Else
        Wscript.Echo "DHCP could not be enabled due to: " & errEnable
    End If
Next

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Thanks Mark,

Good idea Mark, but due to the nature of the pc's (mostly SCADA)it is essential that they all have static ip addresses.

Does anyone know the best solution for this.
 
Code:
Option Explicit 
'On Error Resume Next
dim strComputer
dim wmiNS
dim wmiQuery
dim objWMIService
dim colItems
dim objItem 
Dim strIPaddress
Dim strSubnetMask
Dim strGateway, strDNSserver
Dim strGatewayMetric
Dim errEnable, errGateways, errDNS

strComputer = "."
strIPAddress = Array("192.168.1.1")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.1.1")
strGatewayMetric = Array(1)
strDNSserver = Array("192.168.1.1")

wmiNS = "\root\cimv2"
wmiQuery = "Select * from win32_NetworkAdapterConfiguration where IPEnabled=TRUE"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
Set colItems = objWMIService.ExecQuery(wmiQuery)

WScript.Echo "starting to change the IP address ... " & Now 
For Each objItem in colItems
	errEnable = objItem.EnableStatic(strIPAddress, strSubnetMask)
  errGateways = objItem.SetGateways(strGateway, strGatewayMetric)
  errDNS = objItem.setDNSServerSearchOrder(strDNSserver)
    If errEnable = 0 Then
        WScript.Echo "The IP address has been changed. " & now
    Else
        WScript.Echo "The IP address could not be changed. " & Now 
    End If
Next

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top