views:

947

answers:

4

I have a type mismatch in my VBScript script. I know that the value is correct, but not sure why it's coming up.

This is the line where the script terminates:

WScript.Echo "DNS Server Search Order: " & objNicItem.DNSServerSearchOrder

The script requires a file named servers.txt (which has a list of servers in it, I am tesing using my own workstation name).

If I do an error on resume next the script works. However if the servers list is populated. it displays the same information as the fisrt which works again for each host, so it's not correct.

'Input from the command line
If Wscript.Arguments.Count = 0 Then
    Wscript.Echo "Usage: DNSAudit.vbs servers.txt "
    Wscript.Quit
End If

'Input from txt file
Const ForReading = 1
Set objArgs = WScript.Arguments
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(ObjArgs(0), ForReading)
i = 0
Do Until objTextFile.AtEndOfStream
    StrNextLine = objTextFile.ReadLine
    objDictionary.Add i, StrNextLine
    i = i + 1
Loop
For Each objItem In objDictionary
    Computer = objDictionary.Item(objItem)

    'For Each Computer In Wscript.Arguments

    'remote connection to another computer other than using variables
    'Set ObjWMIService = GetObject("Winmgmts://COMPUTERNAME") for remote connection

    Set ObjWMIService = GetObject("Winmgmts:{impersonationLevel=impersonate}!\\" & Computer & "\root\cimv2")
    'If Err.Number <> 0 Then
    'WScript.Echo "**************************************************************"  & vbCrLf
    'WScript.Echo "Connection Errors"  & vbCrLf
    'WScript.Echo Computer & " " & Err.Description
    'WScript.Echo Computer & " " & Err.Description & " investigation required!"
    'WScript.Echo "**************************************************************"  & vbCrLf
    ''*********************************************************************************************************
    'End if
    WScript.Echo "**************************************************************"  & vbCrLf
    WScript.Echo "Starting Audit on " & computer
    WScript.Echo "**************************************************************"  & vbCrLf
    WScript.Echo "**************************************************************"  & vbCrLf
    wscript.echo "List DHCP, DNS, WINS Adapter Parameters"
    wscript.Echo "**************************************************************"  & vbCrLf

    Set colItems = objWMIService.ExecQuery _
        ("Select * from Win32_NetworkAdapterConfiguration")

    For Each objNicItem in colItems
        Wscript.Echo "DHCP Enabled: " & objNicItem.DHCPEnabled
        Wscript.Echo "DHCP Lease Expires: " & objNicItem.DHCPLeaseExpires
        Wscript.Echo "DHCP Lease Obtained: " & objNicItem.DHCPLeaseObtained
        Wscript.Echo "DHCP Server: " & objNicItem.DHCPServer
        Wscript.Echo "DNS Domain: " & objNicItem.DNSDomain
        Wscript.Echo "DNS Domain Suffix Search Order: " & _
            objNicItem.DNSDomainSuffixSearchOrder
        Wscript.Echo "DNS Enabled For WINS Resolution: " & _
            objNicItem.DNSEnabledForWINSResolution
        Wscript.Echo "DNS Host Name: " & objNicItem.DNSHostName
        WScript.Echo "DNS Server Search Order: " & objNicItem.DNSServerSearchOrder
    WScript.Echo "Domain DNS Registration Enabled: " & objNicItem.DomainDNSRegistrationEnabled
        Wscript.Echo "Full DNS Registration Enabled: " & _
            objNicItem.FullDNSRegistrationEnabled
       Wscript.Echo "IP Address: " & objNicItem.IPAddress
       Wscript.Echo "MAC Address: " & objNicItem.MACAddress
        Wscript.Echo "TCP Maximum Data Retransmissions: " & _
            objNicItem.TcpMaxDataRetransmissions
        Wscript.Echo "TCP NumC onnections: " & objNicItem.TcpNumConnections
        Wscript.Echo "WINS Host Lookup File: " & objNicItem.WINSHostLookupFile
        Wscript.Echo "WINS Primary Server: " & objNicItem.WINSPrimaryServer
        Wscript.Echo "WINS Secondary Server: " & objNicItem.WINSSecondaryServer & vbCrLf
        Wscript.Echo "Adaptor: "  & objNicItem.Description & vbCrLf
    Next
Next
+3  A: 

The Win32_NetworkAdapterConfiguration object's DNSServerSearchOrder property is an array, not a value that can be (implicitly) converted to a string.

See MSDN on the Win32_NetworkAdapterConfiguration Class

Gary McGill
Try escaping the parenthesis with the backslash: `\(` and `\)`.
Helen
+1 - Link fixed. <hint>The "Hyperlink" button in the editor does a good job escaping links to an SO-compatible format.</hint> ;-)
Tomalak
@Helen: This would break it even more. Not everything can be escaped with backslashes. :-)
Tomalak
@Tomalak: thanks.
Gary McGill
@Tomalak: Backslash is a Markdown escape character for special characters, including parenthesis in link addresses within the `[...](...)` constructs (see http://daringfireball.net/projects/markdown/syntax#backslash). So the `\(` and `\)` escapes are turned into literal parenthesis in the resulting link address. Worked just fine with all MSDN links I've posted on SO so far. :)
Helen
@Helen: Thanks for the hint! :) I didn't know, obviously. Shame on me.
Tomalak
@Tomalak: please refund me my thanks; @Helen: please accept this slightly-used thanks :-) [Seriously, thankyou both]
Gary McGill
@Gary McGill: accepted ;-)
Helen
+4  A: 

The problem is that DNSServerSearchOrder is a list of key/value pairs so the Echo command will fail. You will need to enumerate through all the values like this:

If Not IsNull(objNicItem.DNSServerSearchOrder) Then
  For x = 0 To UBound(objNicItem.DNSServerSearchOrder)
     WScript.Echo "      " & objNicItem.DNSServerSearchOrder(x)
  Next
End If
Frozenskys
Helen
A: 

Found this and it's worked now the script is hanging up on the IPaddress part

WScript.Echo "DNS Server Search Order: " & objNicItem.DNSServerSearchOrder(0) & vbtab & objNicItem.DNSServerSearchOrder(1)

Nasa
A: 

It's fully working now with the following code:

Wscript.Echo "IP Address: " & objNicItem.IPAddress(0)

Thanks Everyone

Nasa