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!
LVL 2
aidebAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shift-3Commented:
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)
objList.Close
 
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)
            Else
                strIP = ""
            End If
 
            If strIP <> "0.0.0.0" Then
                objReport.WriteLine strComputer & "," & strName & "," & _
                    strPrimaryDNS & "," & strSecondaryDNS & "," & strDHCP
            End If
        Next
    Else
        objFailReport.WriteLine strComputer
    End If
Next
 
objReport.Close
objFailReport.Close
 
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
    objTempFile.Close
    objFSO.DeleteFile strTempFile, True
 
    If InStr(strOutput, "bytes=32") > 0 Then
        PingCheck = True
    Else
        PingCheck = False
    End If
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aidebAuthor Commented:
Fantastic, works a treat. Much appreciated!

Cheers

Aidan
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.