vbscript for DNS Changes and Windows Firewall
I've been working in an environment does not have DHCP. That has presented some challenges with migrating AD to new servers, and moving DNS servers.
Code:
strComputer = "." | |
| |
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") | |
| |
Set colNetCards = objWMIService.ExecQuery _ | |
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True and DHCPEnabled = False") | |
| |
For Each objNetCard in colNetCards | |
arrDNSServers = Array("xxx.xxx.yyy.yyy","xxx.xxx.yyy.yyy","xxx.xxx.yyy.yyy") | |
objNetCard.SetDNSServerSearchOrder(arrDNSServers) | |
Next |
This script checks to see if the interface and DHCP is disabled and then changes the DNS servers to the defined values. Not perfect but it was the best option I had. Walking to every network device didn't sound like fun.
Another useful one for changing IP addresses in non-DHCP environments:
Code:
' This code configures an IP address, subnet mask and default gateway | |
' for the specified connection. | |
' --------------------------------------------------------------- | |
' From the book "Windows Server Cookbook" by Robbie Allen | |
' ISBN: 0-596-00633-0 | |
' --------------------------------------------------------------- | |
| |
' ------ SCRIPT CONFIGURATION ------ | |
strComputer = "." | |
strConnection = "Local Area Connection" | |
strIP = Array("1xxx.xxx.xxx.yyy") | |
strMask = Array("1xxx.xxx.xxx.yyy") | |
strGatewayIP = Array("1xxx.xxx.xxx.yyy") | |
' ------ END CONFIGURATION --------- | |
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") | |
set colNA = objWMI.ExecQuery("select * " & _ | |
" from Win32_NetworkAdapter " & _ | |
" where NetConnectionID = '" & strConnection & "'" ) | |
for each objNA in colNA | |
set colNAConfig = objWMI.ExecQuery _ | |
("ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='" & _ | |
objNA.DeviceID & "'} " & _ | |
" WHERE resultClass = win32_NetworkAdapterConfiguration ") | |
for each objNAConfig in colNAConfig | |
intRC = objNAConfig.EnableStatic(strIP,strMask) | |
intRC2 = objNAConfig.SetGateways(strGatewayIP) | |
if intRC = 0 and intRC2 = 0 then | |
WScript.Echo "IP address configured for " & strConnection | |
elseif intRC = 1 or intRC2 = 1 then | |
WScript.Echo "You must reboot for the changes to take effect for " & _ | |
strConnection | |
else | |
WScript.Echo "There was an error configuring IP for " & _ | |
strconnection & ": " & intRC & " and " & intRC2 | |
end if | |
next | |
next |
The other issue I've had to overcome is the Windows firewall gets enabled by Windows updates. Sure you can configure group policy to disable it, but often the policy would take to long to apply and users would call with software issues.
Code:
Set WshNetwork = WScript.CreateObject("WScript.Network") | |
'get reference to ADSI computer object | |
Set objComputer = GetObject("WinNT://" & WshNetwork.ComputerName & ",computer") | |
| |
'get ref to a specific service | |
Set objService = objComputer.GetObject("service", "SharedAccess") | |
| |
'set starttype | |
'0 boot | |
'1 system | |
'2 automatic | |
'3 manual | |
'4 disabled | |
If objService.StartType = 2 Then | |
Set objFirewall = CreateObject("HNetCfg.FwMgr") | |
Set objPolicy = objFirewall.LocalPolicy.CurrentProfile | |
| |
objPolicy.FirewallEnabled = FALSE | |
'msgbox("firewall disabled") | |
End If | |
Set objService = Nothing |
I'm totally against disabling the Windows firewall, but certain vendors refuse to work with it.
| Print article | This entry was posted by Mikeal on 12/11/09 at 08:11:16 am . Follow any responses to this post through RSS 2.0. |