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!

Change DNS Server using VBS

Status
Not open for further replies.

kumar3698

IS-IT--Management
May 19, 2013
7
IN
Hello friends

want to change a ip part by VB script and I found a script which will get the user input and apply in the IP configuration but that script doesn't altering the DNS part.

The script is

Option Explicit 'Enforce strict naming
Dim WSH, FSO
Dim objComputer, objWMIService, objOperatingSystem, objNetAdapter
Dim strTitle, strVersion, strThisPC, strComputername, strCompNameRegPath, strWorkgroup
Dim strCCS, strTcpipParamsRegPath, strIPAddress, strSubnetMask, strGateway, strGatewayMetric
Dim colMEM, colNetAdapters, colOperatingSystems
Dim errEnable, errGateways

'Setup scripting environment
Set WSH = CreateObject ("WSCript.shell")
Set FSO = CreateObject("Scripting.FileSystemObject")

'Set Version info
strVersion = "v04"

'Set target PC for WMI
strThisPC = "."

'Set the Title for any dialogs
strTitle = "XP Clone Changer " & strVersion

'------------------------------------------------------------------------
'Section 1 - Use WMI to change IP address, subnet mask and gateway
WSH.Popup "Preparing to change IP address, subnet mask and gateway... Please wait.",2, strTitle 'Inform user

'Setup WMI
Set objWMIService = GetObject("winmgmts:\\" & strThisPC & "\root\cimv2")

'Set specific WMI query so 1394 adapter is excluded
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

'Get user input into an array
strIPAddress = Array(InputBox("Enter the new IP address"))
strSubnetMask = Array(InputBox("Enter the new Subnet mask"))
strGateway = Array(InputBox("Enter the new Gateway address"))

'Inform user
WSH.Popup "Please be patient whilst changes are made. This could take 10 seconds or more.",3, strTitle 'Inform user
strGatewayMetric = Array(1)

'Make the changes
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
If errEnable = 0 Then
WSH.Popup "The IP address has been changed.",2,strTitle 'Inform user of success
Else
WSH.Popup "The IP address could not be changed automatically." & vbCrLf & vbCrLf &_
"This may be because the PC/Laptop is not connected to an active network." & vbCrLf & vbCrLf &_
"Please check the IP settings manually afterwards.",2,strTitle 'Inform user of failure
End If
Next

Set WSH = Nothing
Set FSO = Nothing
Set objWMIService = Nothing
Set colNetAdapters = Nothing
Set colMEM = Nothing
WScript.Quit
 
i made changes like this


Option Explicit 'Enforce strict naming
Dim WSH, FSO
Dim objComputer, objWMIService, objOperatingSystem, objNetAdapter
Dim strTitle, strVersion, strThisPC, strComputername, strCompNameRegPath, strWorkgroup
Dim strCCS, strTcpipParamsRegPath, strIPAddress, strSubnetMask, strGateway, strGatewayMetric, strDNSServers
Dim colMEM, colNetAdapters, colOperatingSystems
Dim errEnable, errGateways, errDNS

'Setup scripting environment
Set WSH = CreateObject ("WSCript.shell")
Set FSO = CreateObject("Scripting.FileSystemObject")

'Set Version info
strVersion = "v04"

'Set target PC for WMI
strThisPC = "."

'Set the Title for any dialogs
strTitle = "XP Clone Changer " & strVersion

'------------------------------------------------------------------------
'Section 1 - Use WMI to change IP address, subnet mask and gateway
WSH.Popup "Preparing to change IP address, subnet mask and gateway... Please wait.",2, strTitle 'Inform user

'Setup WMI
Set objWMIService = GetObject("winmgmts:\\" & strThisPC & "\root\cimv2")

'Set specific WMI query so 1394 adapter is excluded
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

'Get user input into an array
strIPAddress = Array(InputBox("Enter the new IP address"))
strSubnetMask = Array(InputBox("Enter the new Subnet mask"))
strGateway = Array(InputBox("Enter the new Gateway address"))
strDNSServers = Array(InputBox("Enter the name servers (separated by spaces)"))

'Inform user
WSH.Popup "Please be patient whilst changes are made. This could take 10 seconds or more.",3, strTitle 'Inform user
strGatewayMetric = Array(1)

'Make the changes
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
errDNS = objNetAdapter.SetDNSServerSearchOrder(strDNSServers)

If errEnable = 0 Then
WSH.Popup "The IP address has been changed.",2,strTitle 'Inform user of success
Else
WSH.Popup "The IP address could not be changed automatically." & vbCrLf & vbCrLf &_
"This may be because the PC/Laptop is not connected to an active network." & vbCrLf & vbCrLf &_
"Please check the IP settings manually afterwards.",2,strTitle 'Inform user of failure
End If
Next

Set WSH = Nothing
Set FSO = Nothing
Set objWMIService = Nothing
Set colNetAdapters = Nothing
Set colMEM = Nothing
WScript.Quit
 
.SetDNSServerSearchOrder accepts an array of IPs

all you need is (generally)

Code:
strThisPC = "."
Set objWMIService = GetObject("winmgmts:\\" & strThisPC & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

arrServers = split(inputbox("Enter the IP servers (separated by [b]a comma[/b])"), ",")
for each objNIC in colNetAdapters
   objNIC.SetDNSServerSearchOrder(arrServers)
next

-Geates

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top