Thread Closed 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Powershell: Error HRESULT: 0x8007200B when adding a member to a group in AD (SOLVED)
17-12-2010, 02:07 PM (This post was last modified: 17-12-2010 02:08 PM by Fail Whale.)
Post: #1
Powershell: Error HRESULT: 0x8007200B when adding a member to a group in AD (SOLVED)
Hi

I am having a very weird problem with a script I am writing to create new users. Currently the user selects a group name from a drop-down list which is populated from AD. The cn of the group and the Distinguished Name of the newly created user are passed to a function to add the group membership.

This works perfectly for most groups unless the name contains a hypen or underscore without spaces ie.
GroupName = Works
Group - Name = works
Group-Name = Fails
Group _ Name = Works
Group_Name = Fails
Group Name_2 = Fails
Group Name-2 = Fails

The code for the function in question is:
Code:
FUNCTION AddGroups ($UserDN, $Group)
{
#Create Group DN
Write-Host $Group
$GroupResults = SearchAD "group" "LDAP://OU=London,DC=MYCOMPANY,DC=LOCAL"
FOREACH ($objResult in $GroupResults)
    {
    $objItem = $objResult.Properties
    
    IF ($objItem.cn -ccontains $Group)
        {
        $GroupDN = $objItem.distinguishedname
        }
    }
#Add User to Group
$UserAD = [ADSI]"LDAP://$UserDN"
$Member = $UserAD.distinguishedName
Write-Host $Member
$GroupAD = [ADSI]"LDAP://$GroupDN"
Write-Host $GroupAD.distinguishedName
$GroupAD.member.add("$Member")
$GroupAD.setinfo()
}

The problem seems to be is when one of the problem group names is selected it stops the $UserDN variable being passed correctly so If I insert a couple of write-hosts for trouble shooting I am able to retrieve
$GroupAD.distinguishedName but not $UserAD.distinguishedName. Anyone got any Ideas what might cause this as I am completely stumped now.

Follow me on Twitter
My Blog
Follow me on mFlow
Gamertag - Davotronic 5000
Send this user an email Visit this user's website Find all posts by this user
17-12-2010, 02:17 PM
Post: #2
Powershell: Error HRESULT: 0x8007200B when adding a member to a group in AD (SOLVED)
Any use to you?

Quote:To create a variable name that includes a hyphen, enclose the
variable name in braces. The following command creates a variable
named "save-items".

C:\PS> ${save-items} = "a", "b", "c"
C:\PS>${save-items}
a
b
c

http://www.colorconsole.de/PS_Windows/en...iables.htm

- Techmonkey
================
Enterprise Business IT Support | SME IT Support | Home Support Available
Send this user an email Visit this user's website Find all posts by this user
17-12-2010, 02:37 PM (This post was last modified: 17-12-2010 02:41 PM by Fail Whale.)
Post: #3
Powershell: Error HRESULT: 0x8007200B when adding a member to a group in AD (SOLVED)
(17-12-2010 02:17 PM)Techmonkey Wrote:  Any use to you?

Quote:To create a variable name that includes a hyphen, enclose the
variable name in braces. The following command creates a variable
named "save-items".

C:\PS> ${save-items} = "a", "b", "c"
C:\PS>${save-items}
a
b
c

http://www.colorconsole.de/PS_Windows/en...iables.htm

Unfortunately not, it is only the content of the variable which contain hyphens not the variable names.

I have tried the same code outside of the function on the problem groups and it does work. The issue seems to be with passing the variables to the function.

If it helps this is the code which loads the Function, but there is not much to it
Code:
#add the new user to each group selected on the form
FOREACH ($selection in $clbAdGroups.SelectedItems)
    {
    AddGroups $UserDN $selection
    }

Follow me on Twitter
My Blog
Follow me on mFlow
Gamertag - Davotronic 5000
Send this user an email Visit this user's website Find all posts by this user
17-12-2010, 02:57 PM
Post: #4
Powershell: Error HRESULT: 0x8007200B when adding a member to a group in AD (SOLVED)
I think this time we may have a winner Smile or at least a step in the right direction:

Quote:There is one small caveat when using query variables to provide tab completion:

- Many LDAP attribute names defined in AD schema have a hyphen ‘-’ in them. Since .NET property names cannot have a hyphen, I have modified the script to convert hypens ‘-’into underscores ‘_’. Thus, for writing a filter using attribute “msDS-LastKnownRDN”, you would have to use $xADUser.msDS_LastKnownRDN.

Get-ADObject -filter { $xADObject.msDS_LastKnownRDN -like "*" } -IncludeDeletedObjects

Enjoy!

http://blogs.msdn.com/b/adpowershell/arc...lters.aspx

- Techmonkey
================
Enterprise Business IT Support | SME IT Support | Home Support Available
Send this user an email Visit this user's website Find all posts by this user
17-12-2010, 03:13 PM
Post: #5
Powershell: Error HRESULT: 0x8007200B when adding a member to a group in AD (SOLVED)
Unfortunately again no luck. In the problem I am having underscores are causing the same problem as hyphens and only when they are not enclosed by spaces.

I think I need to wrap the variable content in some sort of brace or quote marks but cant find anything especially as hyphens and quotes should not be escape characters.

Follow me on Twitter
My Blog
Follow me on mFlow
Gamertag - Davotronic 5000
Send this user an email Visit this user's website Find all posts by this user
17-12-2010, 05:48 PM
Post: #6
Powershell: Error HRESULT: 0x8007200B when adding a member to a group in AD (SOLVED)
Finally got this solved. The Function the data was originally coming from was returning too much data to the variable, which I have cleaned up now. Was just manifesting in a very strange way.

Follow me on Twitter
My Blog
Follow me on mFlow
Gamertag - Davotronic 5000
Send this user an email Visit this user's website Find all posts by this user
17-12-2010, 07:29 PM
Post: #7
RE: Powershell: Error HRESULT: 0x8007200B when adding a member to a group in AD (SOLVED)
hmmm that explains why I could find no mention of the _ underscore issue lol nice find Smile

- Techmonkey
================
Enterprise Business IT Support | SME IT Support | Home Support Available
Send this user an email Visit this user's website Find all posts by this user
Thread Closed 


Forum Jump: