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?

Viewing 1 post (of 1 total)
  • Author
    Posts

  • 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.

External Sharing and Guest User Access in Microsoft 365 and Teams

This eBook will dive into policy considerations you need to make when creating and managing guest user access to your Teams network, as well as the different layers of guest access and the common challenges that accompany a more complicated Microsoft 365 infrastructure.

You will learn:

  • Who should be allowed to be invited as a guest?
  • What type of guests should be able to access files in SharePoint and OneDrive?
  • How should guests be offboarded?
  • How should you determine who has access to sensitive information in your environment?

Sponsored by: