Avatar of Phase2
Phase2Flag for United States of America

asked on 

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

VB ScriptWindows Server 2008DHCP

Avatar of undefined
Last Comment
Phase2
Avatar of archmuk
archmuk

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
Avatar of archmuk
archmuk

Sorry in my previous comment, the example static IP is 10.0.16.1
Avatar of Phase2
Phase2
Flag of United States of America image

ASKER

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
Avatar of Phase2
Phase2
Flag of United States of America image

ASKER

Tried the script but nothing changed....hmmm

Are yo able to get it to work on your end?
Avatar of Phase2
Phase2
Flag of United States of America image

ASKER

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

Avatar of Krzysztof Pytko
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
Avatar of Phase2
Phase2
Flag of United States of America image

ASKER

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

Krzysztof
ASKER CERTIFIED SOLUTION
Avatar of archmuk
archmuk

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Phase2
Phase2
Flag of United States of America image

ASKER

Still no go :(
Avatar of archmuk
archmuk

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?
Avatar of Phase2
Phase2
Flag of United States of America image

ASKER

Got it working. Was an issue with startup scripts
Windows Server 2008
Windows Server 2008

Windows Server 2008 and Windows Server 2008 R2, based on the Microsoft Vista codebase, is the last 32-bit server operating system released by Microsoft. It has a number of versions, including including Foundation, Standard, Enterprise, Datacenter, Web, HPC Server, Itanium and Storage; new features included server core installation and Hyper-V.

86K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo