?
Solved

Vbscript to remove all current DNS records and assigen new once

Posted on 2008-11-14
17
Medium Priority
?
1,965 Views
Last Modified: 2012-05-05
Hello,
i'm looking for a VBScript to delete\remove any DNS records on a local computer and assign predetermined once (should be in the script itself). is there such a thing?
0
Comment
Question by:johnnyjonathan
  • 7
  • 7
  • 3
17 Comments
 
LVL 78

Expert Comment

by:Rob Williams
ID: 22965267
I am not a VBS guy but you can do so in a batch file:

rem  First overwrites existing
netsh interface ip set dns name = "Local Area Connection" source = static addr = 4.2.2.2
netsh interface ip add dns name = "Local Area Connection" addr = 4.2.2.3
netsh interface ip add dns name = "Local Area Connection" addr = 4.2.2.4
rem  next clears cache
IPconfig  /flushdns
0
 

Author Comment

by:johnnyjonathan
ID: 22966399
can you please explain the difference between set and add commands?

thank you.
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 22967324
Here is the VB script way.  Edit line 8 to match your DNS settings.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colNetAdapters = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
 
strDNSSettings   = Array("192.168.1.1","192.168.1.2")
 
For Each objNetAdapter in colNetAdapters
    arrDNSSetting = objNetAdapter.DNSServerSearchOrder
    errDNSSetting = objNetAdapter.SetDNSServerSearchOrder(arrDNSSetting)
    Select Case errEnable
        Case 0
            wscript.echo "Successful completion, no reboot required."
        Case 1
            wscript.echo "Successful completion, reboot required."
        Case 64
            wscript.echo "Method not supported on this platform."
        Case 65
            wscript.echo "Unknown failure."
        Case 66
            wscript.echo "Invalid subnet mask."
        Case 67
            wscript.echo "An error occurred while processing an instance that was returned."
        Case 68
            wscript.echo "Invalid input parameter."
        Case 69
            wscript.echo "More than five gateways specified."
        Case 70
            wscript.echo "Invalid IP address."
        Case 71
            wscript.echo "Invalid gateway IP address."
        Case 72
            wscript.echo "An error occurred while accessing the registry for the requested information."
        Case 73
            wscript.echo "Invalid domain name."
        Case 74
            wscript.echo "Invalid host name."
        Case 75
            wscript.echo "No primary or secondary WINS server defined."
        Case 76
            wscript.echo "Invalid file."
        Case 77
            wscript.echo "Invalid system path."
        Case 78
            wscript.echo "File copy failed."
        Case 79
            wscript.echo "Invalid security parameter."
        Case 80
            wscript.echo "Unable to configure TCP/IP service."
        Case 81
            wscript.echo "Unable to configure DHCP service."
        Case 82
            wscript.echo "Unable to renew DHCP lease."
        Case 83
            wscript.echo "Unable to release DHCP lease."
        Case 84
            wscript.echo "IP not enabled on adapter."
        Case 85
            wscript.echo "IPX not enabled on adapter."
        Case 86
            wscript.echo "Frame or network number bounds error."
        Case 87
            wscript.echo "Invalid frame type."
        Case 88
            wscript.echo "Invalid network number."
        Case 89
            wscript.echo "Duplicate network number."
        Case 90
            wscript.echo "Parameter out of bounds."
        Case 91
            wscript.echo "Access denied."
        Case 92
            wscript.echo "Out of memory."
        Case 93
            wscript.echo "Already exists."
        Case 94
            wscript.echo "Path, file, or object not found."
        Case 95
            wscript.echo "Unable to notify service."
        Case 96
            wscript.echo "Unable to notify DNS service."
        Case 97
            wscript.echo "Interface not configurable."
        Case 98
            wscript.echo "Not all DHCP leases could be released or renewed."
        Case 100
            wscript.echo "DHCP not enabled on adapter."
    end select
Next
 
wscript.echo "Done."

Open in new window

0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 78

Expert Comment

by:Rob Williams
ID: 22967385
>>"can you please explain the difference between set and add commands?"
"Set" enters the first/primary DNS server IP, "add" adds additional DNS servers. The first "add" line sets the "alternate/secondary" DNS server. You can have as many DNS servers listed as you like but, additional DNS servers are only seen in the advanced TCP/IP properties GUI or using ipconfig /all
0
 

Author Comment

by:johnnyjonathan
ID: 22968769
Thank you, i will try it later today.
is there any way i can append append dns suffixes in the vb or batch as well?
0
 
LVL 78

Expert Comment

by:Rob Williams
ID: 22968793
None that I know of with a batch file, but I bet it can be dine with VBS.
If you want to script this I assume you have a large number of computers. Wiy are you not using DHCP ? This would make the changes very easy.  Just curious.
0
 

Author Comment

by:johnnyjonathan
ID: 22969970
no problem, i am using DHCP the script to regassigen dns suffixes is for users who change it on their own and the helpdesk needs to troubleshoot and solve, i can't block in by GPO since most users do need to assigen in on their own, the others simply don't know what they are doing :)
0
 
LVL 78

Expert Comment

by:Rob Williams
ID: 22970807
Ah!  That sounds good.
--Rob
0
 

Author Comment

by:johnnyjonathan
ID: 22971810
rejoinder , thank you for the scripts, looks like it's working great (got the confirmation) but i couldn't see the DNS servers change or modify (in the TCP IP - Advanced settings - DNS tab). is there something I'm missing?

in addition, since i really must have the DNS suffixes added is there any script for that?
0
 
LVL 78

Expert Comment

by:Rob Williams
ID: 22971878
You can of course use Group Policy to assign the DNS suffix:
Configure machine | administrative template | networks | Primary DNS suffix
0
 

Author Comment

by:johnnyjonathan
ID: 22972308
i know, but it won't work on VPN\remote users
0
 
LVL 78

Expert Comment

by:Rob Williams
ID: 22972382
Not trying to argue with you but if you are stuck  for a solution:
If, and really only if the VPN users are using the Windows VPN and are members of the domain, at logon there is an check box option to connect using a dial-up  connection. If they select this, one of the dial-up options is the VPN. This allows the VPN to connect before logon completes and logon scripts and Group Polices are applied. A few other vendors such as Cisco have similar options.
If that is an option for you, there are 3 Group Policies that help to deal with the slow link that sometimes causes problems with the basic config:
Computer Configuration | Administrative Templates | System | Logon  | Always wait for the network at computer startup and login
Computer Configuration | Administrative Templates | System | Scripts | Run logon scripts synchronously
Computer Configuration | Administrative Templates | System | Group Policy | Group Policy slow link detection
0
 

Author Comment

by:johnnyjonathan
ID: 22972404
your right, and everything is implemented, however the script is for when the login script didn't work or the policy wasn't updated, it happens, and i'm looking to save the helpdesk time by giving them a one click solution.
0
 
LVL 78

Expert Comment

by:Rob Williams
ID: 22972439
Sounds very reasonable. Perhaps rejoinder will know of a way to add the suffix with VBS.
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 22973694
DNS suffix doesn't sound like it will be a problem to impliment.  I have been out all day and I am just heading off to bed now so I will look at this tomorrow.

Thank you.
0
 
LVL 14

Accepted Solution

by:
rejoinder earned 2000 total points
ID: 22980731
This version is the above with the DNS suffix included.  
Edit line 10 to include the domain names you are interested in adding.  If there are multiple names, seperate with a comma.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colNetAdapters = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
 
strDNSSettings = Array("192.168.1.1","192.168.1.2")
'The following must be a string seperated by commas
strDNSSuffix = "domain.com,sub.domain.com"
 
For Each objNetAdapter in colNetAdapters
    arrDNSSetting = objNetAdapter.DNSServerSearchOrder
    errDNSSetting = objNetAdapter.SetDNSServerSearchOrder(arrDNSSetting)
    if errDNSSetting <> 0 AND errDNSSetting <> 94 then
        wscript.echo ReturnErrorMsg(errDNSSetting)
    end if
Next
 
Set objWSHShell = WScript.CreateObject("WScript.Shell")
objWSHShell.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\SearchList", strDNSSuffix
 
wscript.echo "Done."
 
Function ReturnErrorMsg(errNumber)
    strValue = ""
    Select Case errNumber
        Case 0
            strValue = "Successful completion, no reboot required."
        Case 1
            strValue = "Successful completion, reboot required."
        Case 64
            strValue = "Method not supported on this platform."
        Case 65
            strValue = "Unknown failure."
        Case 66
            strValue = "Invalid subnet mask."
        Case 67
            strValue = "An error occurred while processing an instance that was returned."
        Case 68
            strValue = "Invalid input parameter."
        Case 69
            strValue = "More than five gateways specified."
        Case 70
            strValue = "Invalid IP address."
        Case 71
            strValue = "Invalid gateway IP address."
        Case 72
            strValue = "An error occurred while accessing the registry for the requested information."
        Case 73
            strValue = "Invalid domain name."
        Case 74
            strValue = "Invalid host name."
        Case 75
            strValue = "No primary or secondary WINS server defined."
        Case 76
            strValue = "Invalid file."
        Case 77
            strValue = "Invalid system path."
        Case 78
            strValue = "File copy failed."
        Case 79
            strValue = "Invalid security parameter."
        Case 80
            strValue = "Unable to configure TCP/IP service."
        Case 81
            strValue = "Unable to configure DHCP service."
        Case 82
            strValue = "Unable to renew DHCP lease."
        Case 83
            strValue = "Unable to release DHCP lease."
        Case 84
            strValue = "IP not enabled on adapter."
        Case 85
            strValue = "IPX not enabled on adapter."
        Case 86
            strValue = "Frame or network number bounds error."
        Case 87
            strValue = "Invalid frame type."
        Case 88
            strValue = "Invalid network number."
        Case 89
            strValue = "Duplicate network number."
        Case 90
            strValue = "Parameter out of bounds."
        Case 91
            strValue = "Access denied."
        Case 92
            strValue = "Out of memory."
        Case 93
            strValue = "Already exists."
        Case 94
            strValue = "Path, file, or object not found."
        Case 95
            strValue = "Unable to notify service."
        Case 96
            strValue = "Unable to notify DNS service."
        Case 97
            strValue = "Interface not configurable."
        Case 98
            strValue = "Not all DHCP leases could be released or renewed."
        Case 100
            strValue = "DHCP not enabled on adapter."
    end select
    ReturnErrorMsg = strValue
End Function

Open in new window

0
 

Author Closing Comment

by:johnnyjonathan
ID: 31517006
Thank you, greatly appreciated!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Resolve DNS query failed errors for Exchange
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

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