Script to enable DHCP but not on DNS

I have a VBscript that changes NIC settings to DHCP on startup.

This changes both IP, NetMask, default gateway, and DNS to use DHCP.
I am trying to make it so that DNS only is not set to DHCP.

Would love anyones help.
Thank you!

' Enable DHCP


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colNetAdapters = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
 
For Each objNetAdapter In colNetAdapters
    errEnable = objNetAdapter.EnableDHCP()
Next

Open in new window

Phase2Asked:
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.

archmukCommented:
Pl try this variation. Suppose you hv a PC with static IP 10.0.16.12. Below script changes the IP to dynamic for this PC. After testing you can run this script each subnet wise (if u hv number of subnets) or you can try appropriately modifying the 'if' clause to exclude your DNS server.
 
strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colNetAdapters = objWMIService.ExecQuery _
    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True")

For Each objNetAdapter in colNetAdapters
    For Each strAddress in objNetAdapter.IPAddress
        arrOctets = Split(strAddress, ".")
        If arrOctets(0) = "10" and arrOctets(1) = "0" and arrOctets(2) = "16" and arrOctets(3) <=

"1"Then
      errEnable = objNetAdapter.EnableDHCP()
If errEnable = 0 Then
        WScript.Echo "The IP address of your pc has been changed. Contact AM for restoring it back"
    Else
        WScript.Echo "The IP address could not be changed.Contact AM"
    End If      
End If    
    Next
Next
0
archmukCommented:
Sorry in my previous comment, the example static IP is 10.0.16.1
0
Phase2Author Commented:
only have one subnet.

trying to make it so that if a user changes the NIC to static IP it will reset to DHCP upon reboot except for DNS which will stay static
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Phase2Author Commented:
Tried the script but nothing changed....hmmm

Are yo able to get it to work on your end?
0
Phase2Author Commented:
This is the code i am using
strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colNetAdapters = objWMIService.ExecQuery _
    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True")

For Each objNetAdapter in colNetAdapters
    For Each strAddress in objNetAdapter.IPAddress
        arrOctets = Split(strAddress, ".")
        If arrOctets(0) = "10" and arrOctets(1) = "0" and arrOctets(2) = "16" and arrOctets(3) <= "1"
Then
      errEnable = objNetAdapter.EnableDHCP()
If errEnable = 0 Then
        WScript.Echo "The IP address of your pc has been changed. Contact AM for restoring it back"
    Else
        WScript.Echo "The IP address could not be changed.Contact AM"
    End If      
End If    
    Next
Next

Open in new window

0
Krzysztof PytkoSenior Active Directory EngineerCommented:
Hi,

I'm not a big expert in VB Script area but I found something interesting on a Microsoft Technet forum. Looks like you might be interested :)

http://social.technet.microsoft.com/Forums/eu/ITCG/thread/3e0f5bea-0faf-4045-a774-8ef21bc3a507

Later, I would be able to test it in my test environment, so we can try to figure out how to do that working in your scenario.

Regards,
Krzysztof
0
Phase2Author Commented:
iSiek,
Thanks I saw that earlier. He is trying to do the opposite of what I want. Don't  think it will work
0
Krzysztof PytkoSenior Active Directory EngineerCommented:
OK, maybe you are interested with normal batch file and use in that syntax netsh commad?
It's much easier to accomplish that

Krzysztof
0
archmukCommented:
Please try this: (Considering your DNS server IP is 10.0.22.21, excuding it from conversion to DHCP)

On Error Resume Next
strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colNetAdapters = objWMIService.ExecQuery _
    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True")

For Each objNetAdapter in colNetAdapters
    For Each strAddress in objNetAdapter.IPAddress
        arrOctets = Split(strAddress, ".")
        If arrOctets(0) = "10" and arrOctets(1) = "0" and arrOctets(2) = "22" and arrOctets(3) <> "21"

Then
      errEnable = objNetAdapter.EnableDHCP()
If errEnable = 0 Then
        WScript.Echo "The IP address of your PC has been changed to DHCP. Contact IT dept if you have any

problem in connectivity. Thanks"
    Else
        WScript.Echo "The IP address of your PC could not be changed.Contact IT dept for troubleshooting.

Thanks."
    End If      
End If    
    Next
Next
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
Phase2Author Commented:
Still no go :(
0
archmukCommented:
I have tested this script before uploading. It is working. I hv actually changed the static IP of our network of abt 1600+ PCs to DHCP through running similar script as startup script in domain PCs about four years back.
Did u test run the script with admin privilidges?
0
Phase2Author Commented:
Got it working. Was an issue with startup scripts
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.