Vbscript to remove all current DNS records and assigen new once

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?
johnnyjonathanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rob WilliamsCommented:
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
johnnyjonathanAuthor Commented:
can you please explain the difference between set and add commands?

thank you.
0
rejoinderCommented:
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
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

Rob WilliamsCommented:
>>"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
johnnyjonathanAuthor Commented:
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
Rob WilliamsCommented:
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
johnnyjonathanAuthor Commented:
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
Rob WilliamsCommented:
Ah!  That sounds good.
--Rob
0
johnnyjonathanAuthor Commented:
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
Rob WilliamsCommented:
You can of course use Group Policy to assign the DNS suffix:
Configure machine | administrative template | networks | Primary DNS suffix
0
johnnyjonathanAuthor Commented:
i know, but it won't work on VPN\remote users
0
Rob WilliamsCommented:
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
johnnyjonathanAuthor Commented:
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
Rob WilliamsCommented:
Sounds very reasonable. Perhaps rejoinder will know of a way to add the suffix with VBS.
0
rejoinderCommented:
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
rejoinderCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
johnnyjonathanAuthor Commented:
Thank you, greatly appreciated!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.