Can't add computer to ad group

Welcome Forums General PowerShell Q&A Can't add computer to ad group

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
3 months ago.

  • Author
    Posts
  • #161735

    Participant
    Topics: 25
    Replies: 26
    Points: 49
    Rank: Member

    Hi, I have been using the following script to add a computer to AD groups but I'm having trouble with this certain group.  I believe the problem is the parentheses but I'm not sure.

    $computername = '705pqg2'

    $dns = get-adcomputer $computername

    $b = $dns.distinguishedname

    Add-ADPrincipalGroupMembership $b (Get-ADGroup -Filter * -Server 'test' | where { $_.name -match 'Apps – Java JRE (test)_Dom1' })

    Any help would be greatly appreciated.

    Thanks

  • #161759

    Participant
    Topics: 1
    Replies: 1529
    Points: 2,587
    Helping Hand
    Rank: Community Hero

    .... but I'm having trouble with this certain group.

    What kind of trouble?

    ... I believe the problem is the parentheses but I'm not sure.

    Why do you think that?
    Don't you know the exact name of the group? ... or why do you use a where filter with a match comparison?

    If it's a single computer and a single group you could use

    Add-ADGroupMember -Identity 'Apps – Java JRE (test)_Dom1' -Members '705pqg2'
  • #161771

    Participant
    Topics: 25
    Replies: 26
    Points: 49
    Rank: Member

    Hi  yes that would work and thank you.  That is the exact name.  I typically add multiple groups using a similar script.  I guess I was curious on why no groups with the parentheses worked.  This works also:

    $computername = '705pqg2'

    $dns = get-adcomputer $computername

    $b = $dns.distinguishedname

    Add-ADPrincipalGroupMembership $b 'Apps – Java JRE (test)_Dom1'

  • #161819

    Participant
    Topics: 1
    Replies: 1529
    Points: 2,587
    Helping Hand
    Rank: Community Hero

    Without you showing the error you get if you get one it's hard to say what problem you have. 😉 If you use the -match operator you should be aware of that it's using regular expressions for comparison. Therefor charachters might not have the meaning you expect. You might read some more information about that topic – regular expressions.

    BTW: When you post code please format it as code using the code tag button (pre) on the edit bar of the post editor. Thanks.

The topic ‘Can't add computer to ad group’ is closed to new replies.