How do I enumerate all computers and their description field?

Home Forums Scripting Windows Script Host How do I enumerate all computers and their description field?

This topic contains 0 replies, has 1 voice, and was last updated by Avatar JDMils 8 years, 4 months ago.

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Avatar
    JDMils
    Member
    #154192

    I have the following script which needs to list the computer name and description. It works until it loops to one particular computer, I suppose the first one it finds with a description set, and crashes.

    Here’s the code:
    ‘ On Error Resume Next

    Dim PCQuery, objConnection, objCommand, objRecordSet
    Dim oRootDSE, strDNC

    ‘First get domain information
    Set oRootDSE = GetObject(“LDAP://rootDSE”)
    strDNC = oRootDSE.get(“defaultNamingContext”)
    ‘ other categories = computer, user, printqueue, group
    PCQuery = “;(objectCategory=computer)” & _
    “;distinguishedName,name,description,location;subtree”

    Set objConnection = CreateObject(“ADODB.Connection”)
    Set objCommand = CreateObject(“ADODB.Command”)
    objConnection.Open “Provider=ADsDSOObject;”
    objCommand.ActiveConnection = objConnection
    objCommand.CommandText = PCQuery
    Set objRecordSet = objCommand.Execute

    Dim strComputer
    Dim strComputerDN
    Dim strComputerDesc
    ‘ Dim strComputerLocation
    Dim Counter

    objRecordSet.movefirst
    Do Until objRecordSet.EOF
    Counter = Counter + 1
    ‘assign the computer name and distinguished path to variables
    strComputer = objRecordSet.Fields(“name”)
    strComputerDN = objRecordSet.Fields(“distinguishedName”)
    strComputerDesc = objRecordSet.fields(“Description”)
    ‘ strComputerLocation = objRecordSet.fields(“Location”)
    ‘Put the worker process of your code in here
    ‘*******************************************

    If strComputerDesc <> “” Then
    ‘ If strComputerDesc <> “” Or strComputerLocation <> “” Then
    WScript.Echo “*******************************************”
    wscript.Echo strComputer
    wscript.Echo strComputerDN
    wscript.Echo strComputerDesc
    ‘ wscript.Echo strComputerLocation
    WScript.Echo “*******************************************”
    End If

    ‘*******************************************
    strComputer=””
    strComputerDN =””
    strComputerDesc =””
    ‘ strComputerLocation=””
    objrecordset.MoveNext
    Loop

    objConnection.Close

    WScript.Echo “Script Finished”
    [/CODE]

    It crashes on the line:

    If strComputerDesc <> “” Then

    With the error:

    Exception: Type mismatch

    Looking at the variable strComputerDesc, I can see the following:

    [QUOTE]?strComputerDesc
    {…}
    (0): “VM MACHINE. DO NOT MOVE”[/QUOTE]

    So, it looks like it’s an array of values. So how can I get the variable strComputerDesc to contain all values of the objRecordSet.fields(“Description”) property as one big string?[CODE]’ On Error Resume Next

    Dim PCQuery, objConnection, objCommand, objRecordSet
    Dim oRootDSE, strDNC

    ‘First get domain information
    Set oRootDSE = GetObject(“LDAP://rootDSE”)
    strDNC = oRootDSE.get(“defaultNamingContext”)
    ‘ other categories = computer, user, printqueue, group
    PCQuery = “
    “>;(objectCategory=computer)” & _
    “;distinguishedName,name,description,location;subtree”

    Set objConnection = CreateObject(“ADODB.Connection”)
    Set objCommand = CreateObject(“ADODB.Command”)
    objConnection.Open “Provider=ADsDSOObject;”
    objCommand.ActiveConnection = objConnection
    objCommand.CommandText = PCQuery
    Set objRecordSet = objCommand.Execute

    Dim strComputer
    Dim strComputerDN
    Dim strComputerDesc
    ‘ Dim strComputerLocation
    Dim Counter

    objRecordSet.movefirst
    Do Until objRecordSet.EOF
    Counter = Counter + 1
    ‘assign the computer name and distinguished path to variables
    strComputer = objRecordSet.Fields(“name”)
    strComputerDN = objRecordSet.Fields(“distinguishedName”)
    strComputerDesc = objRecordSet.fields(“Description”)
    ‘ strComputerLocation = objRecordSet.fields(“Location”)
    ‘Put the worker process of your code in here
    ‘*******************************************

    If strComputerDesc <> “” Then
    ‘ If strComputerDesc <> “” Or strComputerLocation <> “” Then
    WScript.Echo “*******************************************”
    wscript.Echo strComputer
    wscript.Echo strComputerDN
    wscript.Echo strComputerDesc
    ‘ wscript.Echo strComputerLocation
    WScript.Echo “*******************************************”
    End If

    ‘*******************************************
    strComputer=””
    strComputerDN =””
    strComputerDesc =””
    ‘ strComputerLocation=””
    objrecordset.MoveNext
    Loop

    objConnection.Close

    WScript.Echo “Script Finished”
    [/CODE]

    It crashes on the line:

    If strComputerDesc <> “” Then

    With the error:

    Exception: Type mismatch

    Looking at the variable strComputerDesc, I can see the following:

    Quote:
    ?strComputerDesc
    {…}
    (0): “VM MACHINE. DO NOT MOVE”

    So, it looks like it’s an array of values. So how can I get the variable strComputerDesc to contain all values of the objRecordSet.fields(“Description”) property as one big string?

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

Register for this Petri Webinar!

Want to Make Your Backup Storage Unlimited & Ready for the Cloud? – Free Thurrott Premium Account with Webinar Registration!

Tuesday, August 27, 2019 @ 1:00 pm EDT

A Scale-Out Backup storage infrastructure is a must-have technology for your backups. In this webinar, join expert Rick Vanover for a look on what real-world problems are solved by the Scale-Out Backup Repository.

Register Now

Sponsored By