none
VBScript - finding out what attributes does the object support?

    Question


  • Can anyone help me trying to understand something basic about VBScript,

    Why is it finding out the attributes of a class are such a big mystery?!?!?!!!!!!

    Only for example; this piece of script pulls back a domain group from Active Directory, it then goes through checking each member of the group to see if that member is actually a group nested inside or not, if its a group it displays the message 'This is a group'

    What I would like to do say with the Else statement is display the "name" of the object that turned out not to be a group using .... ??? .... ???? I really don't know???

    strTargetGroupDN = "LDAP://<group>"
    Set objGroup = GetObject (strTargetGroupDN)

         For Each objMember In objGroup.Members  
             If (LCase(objMember.Class) = "group") Then
                 WScript.Echo "This is a group"
             Else
                 WScript.Echo objMember.????
             End If
         Next

    The real problem is not that I actually want to display the 'name', the problem is that the variable objMember has clearly got more classes assigned to the object but I have no way of finding out what classes they are! and I seem to run into this exact same problem time and time again with other scripts!

    I've been searching the internet trying to find a tool which pulls back a list of classes that an object supports and my PC is littered full of trial programs as a result of trying, the search engines are failing me too, I've spent hours and for some reason I can't get the search terms right.

    Please could someone tell me how the pro's figure it out, its not important to me to get the code example working, what I need to know is how to analyse any object on the fly to pull back what attributes it supports.

    Thanks for any help

    Thursday, April 05, 2012 1:22 PM

Answers

  • You asked for it.

    Here is the method for listing all of the properties and values of an object for any ADSI object.

    strTargetGroupDN = "LDAP://CN=Mytest,OU=TestOU,DC=sec,DC=local"
    Set objGroup = GetObject (strTargetGroupDN)
    For Each objMember In objGroup.Members
         WScript.Echo objMember.Class, objMember.CN 
         objMember.GetInfo()
         Set objClass = GetObject(objMember.Schema)
         'Mandatory Properties
         For Each strProperty In objClass.MandatoryProperties
            value=" = n/a"
            On Error Resume Next
            value = " = " & objMember.Get(strProperty)
            Wscript.Echo vbTab & "(M) " & strProperty & Value
            On Error GoTo 0
         Next
         'Optional Properties
         For Each strProperty In objClass.OptionalProperties 
            value=" = n/a"
            On Error Resume Next
            value = " = " & objMember.Get(strProperty)
            Wscript.Echo vbTab & "(O) " & strProperty & value
            On Error GoTo 0
         Next
         Wscript.Echo "Hit <Enter> to continue..."
         x=WScript.Stdin.ReadLine()
    Next

    All automation objects have discoverable interfaces if they have a type library either external or embedded.  With ADSI we also have a schema stored in AD.  The above technique references the objects schema (it is attached to every AD class instance).  The schema is enumerable and contains to lists of properties; one lists the mandatory properties of the object, the other lists the optional properties.  Properties that are optional may or may not exist on the object.  Properties that have no value assigned may be null.

    The above technique has one weakness.  It will skip properties that are objects or collections because it does not detect these situations.  The code will error on these objects and assign the value as 'n/a'.  This can be fixed,


    ¯\_(ツ)_/¯


    Thursday, April 05, 2012 2:59 PM

All replies

  • This is a huge issue with VBS, as far as I'm concerned, and why many people are switching to Powershell.  It is MUCH more discoverable.  You don't need reference books, or to keep searching on the web for every thing you want to do.  VBS is a pain!

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Thursday, April 05, 2012 1:46 PM
  • Hi,

    These classes are all "late bound" in VBScript. This means that VBScript doesn't perform any validation on what attribute (property) you access (or a method you try to call) until you actually run the script. On MSDN, these are documented in the ADSI section on Persistent Object Interfaces.

    Bill

    Thursday, April 05, 2012 1:48 PM
  • Isn't this one of the premiere reasons Script-o-matic 2.0 exists?

    http://www.microsoft.com/download/en/details.aspx?DisplayLang=en&id=12028

    DAS

    Thursday, April 05, 2012 2:44 PM
  • You asked for it.

    Here is the method for listing all of the properties and values of an object for any ADSI object.

    strTargetGroupDN = "LDAP://CN=Mytest,OU=TestOU,DC=sec,DC=local"
    Set objGroup = GetObject (strTargetGroupDN)
    For Each objMember In objGroup.Members
         WScript.Echo objMember.Class, objMember.CN 
         objMember.GetInfo()
         Set objClass = GetObject(objMember.Schema)
         'Mandatory Properties
         For Each strProperty In objClass.MandatoryProperties
            value=" = n/a"
            On Error Resume Next
            value = " = " & objMember.Get(strProperty)
            Wscript.Echo vbTab & "(M) " & strProperty & Value
            On Error GoTo 0
         Next
         'Optional Properties
         For Each strProperty In objClass.OptionalProperties 
            value=" = n/a"
            On Error Resume Next
            value = " = " & objMember.Get(strProperty)
            Wscript.Echo vbTab & "(O) " & strProperty & value
            On Error GoTo 0
         Next
         Wscript.Echo "Hit <Enter> to continue..."
         x=WScript.Stdin.ReadLine()
    Next

    All automation objects have discoverable interfaces if they have a type library either external or embedded.  With ADSI we also have a schema stored in AD.  The above technique references the objects schema (it is attached to every AD class instance).  The schema is enumerable and contains to lists of properties; one lists the mandatory properties of the object, the other lists the optional properties.  Properties that are optional may or may not exist on the object.  Properties that have no value assigned may be null.

    The above technique has one weakness.  It will skip properties that are objects or collections because it does not detect these situations.  The code will error on these objects and assign the value as 'n/a'.  This can be fixed,


    ¯\_(ツ)_/¯


    Thursday, April 05, 2012 2:59 PM
  • I have example VBScript program to document all attributes of any AD object linked here:

    http://www.rlmueller.net/List%20Object%20Attributes.htm

    In addition, I have a VBScript program to document the values of all attributes of any AD object linked here:

    http://www.rlmueller.net/Document%20Attributes.htm


    Richard Mueller - MVP Directory Services

    Thursday, April 05, 2012 3:07 PM
  • Isn't this one of the premiere reasons Script-o-matic 2.0 exists?

    http://www.microsoft.com/download/en/details.aspx?DisplayLang=en&id=12028

    DAS


    Scriptomatic is a great tool, but it is only for WMI scripting.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Thursday, April 05, 2012 3:21 PM
  • Isn't this one of the premiere reasons Script-o-matic 2.0 exists?

    http://www.microsoft.com/download/en/details.aspx?DisplayLang=en&id=12028

    DAS


    Scriptomatic is a great tool, but it is only for WMI scripting.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Has it been fixed for Win7.  I downloaded it to Win7 and had to do some fixes.

    ¯\_(ツ)_/¯

    Thursday, April 05, 2012 3:33 PM
  • Isn't this one of the premiere reasons Script-o-matic 2.0 exists?

    http://www.microsoft.com/download/en/details.aspx?DisplayLang=en&id=12028

    DAS


    Scriptomatic is a great tool, but it is only for WMI scripting.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Really?  Look here: ADSIScriptoMatic

    It is in the script center and has been for a very long time.


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, April 05, 2012 3:50 PM
    Thursday, April 05, 2012 3:49 PM
  • Thanks for all the useful replies a way of querying each object for its supported methods is exactly what I was looking for and from jrv's scripting example I was able to discover much more about what I was working with and so produce a script which functions that I did not believe was possible

    ADSIScriptoMatic is a great recommendation also thanks, I have used it often over the years but found it was too linear for the project I have in hand.

    Thanks for all the useful replies a way of querying each object for its supported methods is exactly what I was looking for and from jrv's scripting example I was able to discover much more about what I was working with and so produce a script with functions that I did not believe was possible.

    ADSIScriptoMatic is a great recommendation also thanks, I have used it often over the years but found it was too linear for the project I have in hand.

    I really started to question the scripting language as Bigteddy mentions.

    Though I like the flexibility of VBScript (HTA's, macro's, webpage integration, standalone, etc ..) I've found it’s become markedly more difficult over the years to use search engines to find the answers to the bloated functionality VBScript supports and I’ve shifted through loads of bad and none relevant information until I got to what I needed, I'm sure that a few years ago I put similar scripts together much quicker!

    I’m not sure if that a criticism of VBScript and my skills or a citation of search engines themselves!

    Either way as a dextrous administrator of no particular discipline this reminds me of when I moved from batch files to VBScript and I it feels like it’s time to start increasing my skills with Powershell and exploring how that can be used.

    Friday, April 13, 2012 10:47 AM