-
April 28th, 2005, 08:22 PM
#1
Enable DHCP script (code inside)
Hey everyone. I'm not sure if this was the right place to put this but it seemed like the most relevant forum to do it in. I recently completed this script for my company to switch all users from static IP addresses to DHCP. Basically this script enables DHCP, obtain DNS automatically (via DHCP), appends the DNS suffix list [im editing out my company's info], sets the DNS suffix for the location and sets it so that NetBIOS setting is sent from DHCP server. If you don't need something done simply comment it out. For those who aren't familiar with WSL (Windows Scripting Language) simply copy/paste this code to a text file and save the file with an extension of .vbs [example: testscript.vbs]
Hopefully this will be of some use to some administrators out there.
Code:
'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
'Set DNS back to DHCP / Set DNS server addresses to blank (redundancy)
'--------------------------------------------------------------------------
'The Chr(34) is used to create the " character so the string will pass correctly
'NOTE: I have it run through 3 different LAC's just to cover all bases
set objshell = wscript.createobject("wscript.shell")
filenamea = "Netsh inter ip set dns name="& Chr(34) &"Local Area Connection"& Chr(34) &" source=dhcp"
objshell.run ("%comspec% /c " & filenamea) ,0,true
set objshell = wscript.createobject("wscript.shell")
filenameb="Netsh inter ip set dns name="& Chr(34) &"Local Area Connection 2"& Chr(34) & " source=dhcp"
objshell.run ("%comspec% /c " & filenameb) ,0,true
set objshell = wscript.createobject("wscript.shell")
filenamec="Netsh inter ip set dns name="& Chr(34) &"Local Area Connection 3"& Chr(34) & " source=dhcp"
objshell.run ("%comspec% /c " & filenamec) ,0,true
'Sets WINS on all network adapters
'--------------------------------------------------------------------------
On Error Resume Next
Const DNS_ENABLED_FOR_WINS_RESOLUTION = True
Const USE_LMHOST_FILE = False
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objNetworkSettings = objWMIService.Get("Win32_NetworkAdapterConfiguration")
errResult = objNetworkSettings.EnableWINS(DNS_ENABLED_FOR_WINS_RESOLUTION,_
USE_LMHOST_FILE)
'Sets the Dynamic DNS
'--------------------------------------------------------------------------
Const FULL_DNS_REGISTRATION = True
Const DOMAIN_DNS_REGISTRATION = True
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNetCards = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each objNetCard in colNetCards
objNetCard.DomainDNSRegistrationEnabled
objNetCard.SetDynamicDNSRegistration FULL_DNS_REGISTRATION, DOMAIN_DNS_REGISTRATION
Next
'Modify NETBIOS use on a network adapter
'--------------------------------------------------------------------------
On Error Resume Next
Set colNetCards2 = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each objNetCard in colNetCards2
objNetCard.SetTCPIPNetBIOS(0)
Next
'Set DNS Suffix Search Order
'--------------------------------------------------------------------------
Set objNetworkSettings = objWMIService.Get("Win32_NetworkAdapterConfiguration")
arrDNSSuffixes = Array("ad.company.net", "company.net")
objNetworkSettings.SetDNSSuffixSearchOrder(arrDNSSuffixes)
'Set DNS Domain
'--------------------------------------------------------------------------
Set objNetworkSettings = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNetSettings = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each objNetworkSetting in colNetSettings
Set objNetworkSetting = objWMIService.Get("Win32_NetworkAdapterConfiguration")
strDNSDomain = "ad.company.net"
objNetworkSetting.SetDNSDomain(strDNSDomain)
Next
'Sets WINS Servers to blank (defaulting to DHCP)
'--------------------------------------------------------------------------
Set colNetCards3 = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each objNetCard in colNetCards3
strPrimaryServer = ""
strSecondaryServer = ""
objNetCard.SetWINSServer strPrimaryServer, strSecondaryServer
Next
'Release and renew to refresh everything in lieu of restart
'--------------------------------------------------------------------------
set objshell = wscript.createobject("wscript.shell")
releasecomm = "ipconfig/release"
objshell.run ("%comspec% /c " & releasecomm) ,0,true
set objshell = wscript.createobject("wscript.shell")
renewcomm = "ipconfig/renew"
objshell.run ("%comspec% /c " & renewcomm) ,0,true
'Reboot system for settings to take effect
'--------------------------------------------------------------------------
'Set objWMIService = GetObject("winmgmts:" _
' & "{impersonationLevel=impersonate,(Shutdown)}!\\" & _
' strComputer & "\root\cimv2")
'Set colOperatingSystems = objWMIService.ExecQuery _
' ("SELECT * FROM Win32_OperatingSystem")
'For Each objOperatingSystem in colOperatingSystems
' objOperatingSystem.Reboot()
'Next
Oh and the last two sections are like that because we decided that doing release/renew was good enough and the system restart was taken care of via ZenWorks. The code for the restarting via script was left in just in case we wanted to utilize it but for reasons stated above I commented it out.
And so at last the beast fell and the unbelievers rejoiced. But all was not lost, for from the ash rose a great bird. The bird gazed down upon the unbelievers and cast fire and thunder upon them. For the beast had been reborn with its strength renewed, and the followers of Mammon cowered in horror. -from The Book of Mozilla, 7:15
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|