Use of BES and Wiki for Win DNS configuration management using VBS

(imported topic written by ononcg91)

Overview of Win DNS configuration management using VBScript and Wiki as revision control.

Win DNS update is performed by VBScript “update-dns.vbs”:

Set WshArgs = WScript.Arguments

bDebug = False

aNewDNSServersAMER = Array (“10.160.29.90”, “10.168.29.87”)

aNewDNSServersAPAC = Array (“10.128.2.87”, “10.160.29.90”)

aNewDNSServersEMEA = Array (“10.0.0.84”, “10.160.29.90”)

If WshArgs.Count = 0 Then

WScript.Echo “Must specify region.”

WScript.Quit (1)

Else

sRegion = WshArgs (0)

If StrComp (sRegion, “AMER”, vbTextCompare) = 0 Then

aNewDNSServers = aNewDNSServersAMER

ElseIf StrComp (sRegion, “EMEA”, vbTextCompare) = 0 Then

aNewDNSServers = aNewDNSServersEMEA

ElseIf StrComp (sRegion, “APAC”, vbTextCompare) = 0 Then

aNewDNSServers = aNewDNSServersAPAC

Else

WScript.Echo “Invalid region specified.”

WScript.Quit (1)

End If

aComputers = Array (“.”)

End If

If WshArgs.Count > 1 Then

sListFile = WshArgs (1)

beaDebugPrint "File: " & sListFile

aComputers = FileToArray (sListFile)

End If

nErrorLevel = 0

For Each sComputer In aComputers

nSuccess = beaNetworkUpdateDNS (sComputer, aNewDNSServers)

If nSuccess = 0 Then

beaDebugPrint "Successfully updated DNS on " & sComputer

Else

beaDebugPrint "Failed updating DNS on " & sComputer

nErrorLevel = 1

End If

Next

WScript.Quit (nErrorLevel)

Function beaNetworkUpdateDNS (sComputer, aDNSServers)

sWMINicSearch = “SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True AND DHCPEnabled = False”

nReturn = 0

On Error Resume Next

beaDebugPrint "Computer: " & sComputer

Set oWMIService = GetObject (“winmgmts:{impersonationLevel=impersonate}!\” & sComputer & “\root\cimv2”)

If Err = 0 Then

Set colNicConfigs = oWMIService.ExecQuery (sWMINicSearch)

For Each oNicConfig In colNicConfigs

If Not IsNull (oNicConfig.IPAddress) Then

beaDebugPrint "Network Adapter " & oNicConfig.Index & " - " & oNicConfig.Caption

beaDebugPrint " IPEnabled: " & oNicConfig.IPEnabled

beaDebugPrint " DHCPEnabled: " & oNicConfig.DHCPEnabled

bSkipNic = False

For i = LBound (oNicConfig.IPAddress) To UBound (oNicConfig.IPAddress)

beaDebugPrint " IPAddress: " & oNicConfig.IPAddress(i)

nCompare = StrComp (oNicConfig.IPAddress(i), “192.168.234.235”, vbTextCompare)

If nCompare = 0 Then

bSkipNic = True

End If

Next

If bSkipNic = False Then

beaDebugPrint vbTab & “DNS Server Search Order - Before:”

If Not IsNull (oNicConfig.DNSServerSearchOrder) Then

For Each sDNSServer In oNicConfig.DNSServerSearchOrder

beaDebugPrint vbTab & sDNSServer

Next

End If

nSetDNSServers = oNicConfig.SetDNSServerSearchOrder (aDNSServers)

If nSetDNSServers = 0 Then

beaDebugPrint vbTab & “Replaced DNS server search order list.”

nReturn = 0

Else

beaDebugPrint vbTab & “Unable to replace DNS server search order list.”

nReturn = 1

End If

Else

beaDebugPrint vbTab & “Skipping Nic.”

End If

End If

Next

Else

beaDebugPrint “Unable to connect to computer.”

nReturn = 1

End If

beaNetworkUpdateDNS = nReturn

End Function

Sub beaDebugPrint (sOutput)

If bDebug = False Then

Exit Sub

End If

WScript.Echo Now & " " & sOutput

End Sub

Function FileToArray (ByVal sFilename)

Set oFSO = CreateObject (“Scripting.FileSystemObject”)

Const ForReading = 1

FileToArray = Split (“”)

If oFSO.FileExists (sFilename) Then

beaDebugPrint "File exists: " & sFilename

On Error Resume Next

Set oTS = oFSO.OpenTextFile (sFilename, ForReading)

If Err = 0 Then

sContents = oTS.ReadAll

oTS.Close

FileToArray = Split (sContents, vbNewLine)

Else

WScript.Echo "Error opening: " & Err.Number

WScript.Quit (1)

End If

Else

WScript.Echo "File does not exist: " & sFilename

WScript.Quit (1)

End If

End Function

“update-dns.vbs” requires DNS region provided as argument e.g. “AMER”, “APAC” or “EMEA”.

This can be run independently of BES deployment either standalone on a system or run via

WMI on list of systems contained in text file provided as argument.

VBScript is uploaded to Wiki as “update-dns.vbs.txt”.

Each DNS region has its own BigFix task but all use the same VBScript.

Excerpts of “Win AMER DNS update” BigTask task.

Relevance:

name of operating system as lowercase starts with “win” AND not (exists addresses whose (it as string = “10.160.29.90”) of dns servers of network and exists addresses whose (it as string = “10.168.29.87”) of dns servers of network)

Action:

download as update-dns.vbs.txt http://mywiki/download/attachments/1212747/update-dns.vbs.txt?version=1

continue if {(size of it = 4581 and sha1 of it = “35f8ad17f5eecc4de922aec9184e4d37269289fb”) of file “update-dns.vbs.txt” of folder “__Download”}

delete __Download\update-dns.vbs

move __Download\update-dns.vbs.txt __Download\update-dns.vbs

run wscript.exe __Download\update-dns.vbs AMER

The action script size and sha1 are obtained by using the the BigFix SHA1 utility:

sha1 -r update-dns.vbs.txt

continue if {(size of it = 4581 and sha1 of it = “35f8ad17f5eecc4de922aec9184e4d37269289fb”) of file “update-dns.vbs.txt” of folder “__Download”}

Confluence is the Wiki used for this implementation. The use of query string to specify version is a very nice

feature.