?
Solved

Script to enable DHCP but not on DNS

Posted on 2011-10-02
12
Medium Priority
?
584 Views
Last Modified: 2012-06-21
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

0
Comment
Question by:Phase2
  • 6
  • 4
  • 2
12 Comments
 
LVL 1

Expert Comment

by:archmuk
ID: 36901472
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
 
LVL 1

Expert Comment

by:archmuk
ID: 36901474
Sorry in my previous comment, the example static IP is 10.0.16.1
0
 

Author Comment

by:Phase2
ID: 36901705
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Phase2
ID: 36901736
Tried the script but nothing changed....hmmm

Are yo able to get it to work on your end?
0
 

Author Comment

by:Phase2
ID: 36901784
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
 
LVL 39

Expert Comment

by:Krzysztof Pytko
ID: 36901789
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
 

Author Comment

by:Phase2
ID: 36901861
iSiek,
Thanks I saw that earlier. He is trying to do the opposite of what I want. Don't  think it will work
0
 
LVL 39

Expert Comment

by:Krzysztof Pytko
ID: 36901870
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
 
LVL 1

Accepted Solution

by:
archmuk earned 2000 total points
ID: 36901871
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
 

Author Comment

by:Phase2
ID: 36901940
Still no go :(
0
 
LVL 1

Expert Comment

by:archmuk
ID: 36901995
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
 

Author Closing Comment

by:Phase2
ID: 36902502
Got it working. Was an issue with startup scripts
0

Featured Post

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

Question has a verified solution.

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

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
Suggested Courses

809 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