Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Computer Groups Help 1

Status
Not open for further replies.

twooly

MIS
Feb 22, 2004
122
US
I'm trying to retrieve what groups the computer object is a part of but I am having a issues. One when I run the script below I am getting this error:

"The directory property cannont be found in the cache." on this line 'strGroups = objComputer.GetEx("memberOf ")'


Second how can I run this remotely against a computer to get the information without having to have the script actually running on the box?

Code:
Dim WSHShell, objNET, objSysInfo, objComputer, strComputerDN, strGroups, Group, GroupName
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set objNET = WScript.CreateObject("WScript.Network")
Set objSysInfo = WScript.CreateObject("ADSystemInfo")
strComputerDN = objSysInfo.COMPUTERNAME
Set objComputer = GetObject("LDAP://" & strComputerDN) 'Binds the objComputer to the Distiguished Name of the Computer in reference   
  
strGroups = objComputer.GetEx("memberOf ")   
For Each Group in strGroups
Set ThisGroup = GetObject("LDAP://" & Group)
GroupName = ThisGroup.CN
WScript.Echo GroupName
Next
 
Code:
strGroups = objComputer.GetEx([red]"memberOf "[/red]) 

strGroups = objComputer.GetEx([red]"memberOf"[/red])

To run remotely you need to simply bind to the machine account. Take a look at the handy functions posted by K0b3 in the FAQs of this forum for code that can take the netbios name and return the distinguished name for use in your script.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
and for the other piece. I am using this, which works great for usernames but doesn't return anything for computernames

Code:
wscript.echo SearchDistinguishedName("barf")

Public Function SearchDistinguishedName(ByVal vSAN)
    ' Function:     SearchDistinguishedName
    ' Description:  Searches the DistinguishedName for a given SamAccountName
    ' Parameters:   ByVal vSAN - The SamAccountName to search
    ' Returns:      The DistinguishedName Name
    Dim oRootDSE, oConnection, oCommand, oRecordSet

    Set oRootDSE = GetObject("LDAP://rootDSE")
    Set oConnection = CreateObject("ADODB.Connection")
    oConnection.Open "Provider=ADsDSOObject;"
    Set oCommand = CreateObject("ADODB.Command")
    oCommand.ActiveConnection = oConnection
    oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & _
        ">;(&(objectCategory=User)(samAccountName=" & vSAN & "));distinguishedName;subtree"
    Set oRecordSet = oCommand.Execute
    On Error Resume Next
    SearchDistinguishedName = oRecordSet.Fields("DistinguishedName")
    On Error GoTo 0
    oConnection.Close
    Set oRecordSet = Nothing
    Set oCommand = Nothing
    Set oConnection = Nothing
    Set oRootDSE = Nothing
End Function
 
Ok I figured out my first error but still having trouble geting the DN name remotely of the computer object.
 
This is the script I have having trouble with, geting the DistinguishedName of the computer object. It doesn't return anything.

Code:
wscript.echo SearchDistinguishedName("barf")

Public Function SearchDistinguishedName(ByVal vSAN)
    ' Function:     SearchDistinguishedName
    ' Description:  Searches the DistinguishedName for a given SamAccountName
    ' Parameters:   ByVal vSAN - The SamAccountName to search
    ' Returns:      The DistinguishedName Name
    Dim oRootDSE, oConnection, oCommand, oRecordSet

    Set oRootDSE = GetObject("LDAP://rootDSE")
    Set oConnection = CreateObject("ADODB.Connection")
    oConnection.Open "Provider=ADsDSOObject;"
    Set oCommand = CreateObject("ADODB.Command")
    oCommand.ActiveConnection = oConnection
    oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & _
        ">;(&(objectCategory=computer)(samAccountName=" & vSAN & "));distinguishedName;subtree"
    Set oRecordSet = oCommand.Execute
    On Error Resume Next
    SearchDistinguishedName = oRecordSet.Fields("DistinguishedName")
    On Error GoTo 0
    oConnection.Close
    Set oRecordSet = Nothing
    Set oCommand = Nothing
    Set oConnection = Nothing
    Set oRootDSE = Nothing
End Function
 
Got it all figured out. The script below will give the groups the computer account is part of by simply changing the computer name assigned to strcomputer

Thanks for the help

Code:
On Error Resume Next

Const E_ADS_PROPERTY_NOT_FOUND  = &h8000500D

strComputer = "ComputerName"



Set objSysInfo = WScript.CreateObject("ADSystemInfo")

strComputerDN = SearchDistinguishedName(strComputer)

Set objComputer = GetObject("LDAP://" & strComputerDN) 

  

strGroups = objComputer.GetEx("memberOf")

If Err.Number = E_ADS_PROPERTY_NOT_FOUND Then

    WScript.Echo "The memberOf attribute is not set."

Else

   

For Each Group in strGroups

Set ThisGroup = GetObject("LDAP://" & Group)

GroupName = ThisGroup.CN

WScript.Echo GroupName

Next 

End If







Public Function SearchDistinguishedName(ByVal vSAN)

    ' Function:     SearchDistinguishedName

    ' Description:  Searches the DistinguishedName for a given SamAccountName

    ' Parameters:   ByVal vSAN - The SamAccountName to search

    ' Returns:      The DistinguishedName Name

    Dim oRootDSE, oConnection, oCommand, oRecordSet



    Set oRootDSE = GetObject("LDAP://rootDSE")

    Set oConnection = CreateObject("ADODB.Connection")

    oConnection.Open "Provider=ADsDSOObject;"

    Set oCommand = CreateObject("ADODB.Command")

    oCommand.ActiveConnection = oConnection

    oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & ">;(&(objectClass=computer)(cn=" & vSAN & "));cn,distinguishedName;subtree"

    Set oRecordSet = oCommand.Execute

    On Error Resume Next

    SearchDistinguishedName = oRecordSet.Fields("DistinguishedName")

    On Error GoTo 0

    oConnection.Close

    Set oRecordSet = Nothing

    Set oCommand = Nothing

    Set oConnection = Nothing

    Set oRootDSE = Nothing

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top