• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2091
  • Last Modified:

VBScript to return Client DNS Servers, DHCP status and Host name

I am looking for a script to output a CSV which will return the;
input, Machine Name, DNS Server 1, DNS Server 2, DHCP Status (enabled / disabled)

The input would be a list of machines from a text file, either an IP address or a Machine name.

I need to run this against a lot of machines and therefore need some way of recording machines that cannot be Ping'ed / contacted so I can deal with them later. Perhaps just creating a list of failed machines?

Any help would be great!
1 Solution
Paste the script below into a text file with a .vbs extension.  Customize the value of the strList variable on line 5 with the location of the computer list.  Running the script will output results to a comma-delimited text file.  It will output machines which do not respond to pings to a separate text file.

Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
strList = "computerlist.txt"
strReport = "Report.csv"
strFailReport = "FailReport.txt"
On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objList = objFSO.OpenTextFile(strList, ForReading, False, TriStateUseDefault)
Set objReport = objFSO.OpenTextFile(strReport, ForWriting, True)
Set objFailReport = objFSO.OpenTextFile(strFailReport, ForWriting, True)
objReport.WriteLine "Input,Computer Name,Primary DNS,Secondary DNS,DHCP Enabled"
arrComputers = Split(objList.ReadAll, vbCrLf)
For Each strComputer in arrComputers
    If PingCheck(strComputer, 1, 500) Then
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        Set colItems = objWMIService.ExecQuery _
            ("Select * From Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
        For Each objItem In colItems
            strName = UCase(objItem.DNSHostName)
            If IsArray(objItem.DNSServerSearchOrder) Then
                arrDNSOrder = objItem.DNSServerSearchOrder
                strPrimaryDNS = arrDNSOrder(0)
                If UBound(arrDNSOrder) >= 1 Then
                    strSecondaryDNS = arrDNSOrder(1)
                End If
            End If
            strDHCP = objItem.DHCPEnabled
            If IsArray(objItem.IPAddress) Then
                strIP = objItem.IPAddress(0)
                strIP = ""
            End If
            If strIP <> "" Then
                objReport.WriteLine strComputer & "," & strName & "," & _
                    strPrimaryDNS & "," & strSecondaryDNS & "," & strDHCP
            End If
        objFailReport.WriteLine strComputer
    End If
WScript.Echo "Query complete."
Function PingCheck(strTarget, strPings, intPause)
    Const ForReading = 1
    Set objShell = CreateObject("WScript.Shell")
    strTempDir = objShell.ExpandEnvironmentStrings("%temp%")
    strTempFile = strTempDir & "\script-" & strTarget & ".txt"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objShell.Run "%comspec% /c ping -n " & strPings & " -w 100 " & _
        strTarget & ">" & strTempFile, 0, True
    WScript.Sleep intPause
    objShell.Run "%comspec% /c ping -n " & strPings & " -w 100 " & _
        strTarget & ">>" & strTempFile, 0, True
    Set objTempFile = objFSO.OpenTextFile(strTempFile, ForReading)
    strOutput = objTempFile.ReadAll
    objFSO.DeleteFile strTempFile, True
    If InStr(strOutput, "bytes=32") > 0 Then
        PingCheck = True
        PingCheck = False
    End If
End Function

Open in new window

aidebAuthor Commented:
Fantastic, works a treat. Much appreciated!


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now