using a pipeline to pass a member into Add-ADGroupMember

Welcome Forums General PowerShell Q&A using a pipeline to pass a member into Add-ADGroupMember

This topic contains 5 replies, has 4 voices, and was last updated by

 
Participant
1 week, 2 days ago.

  • Author
    Posts
  • #175834

    Participant
    Topics: 5
    Replies: 5
    Points: 54
    Rank: Member

    Command Example

    Get-ADGroup "GROUP1" | Add-ADGroupMember -Identity "GROUP2" -Members $_.ObjectGUID

    Error:

    Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Provide an argument that is not null
    or empty, and then try the command again.

     

    So unfortunately I have written like 400 lines of these commands and I know I can change the command to this but that means modifying roughly 400 lines of code.  Any idea what I'm doing wrong?  I have tried multiple ways of entering the pipeline data without success.

    I could probably modify the code to do a foreach down to about 100 lines of code but still modifying a ton of code.  For me easier to just find a way to do a find and replace if it is possible.

  • #175846

    Participant
    Topics: 0
    Replies: 100
    Points: 363
    Helping Hand
    Rank: Contributor

    Lawson,

    Any reason you can't do this instead?

    Add-ADGroupMember -Identity "GROUP2" -Members (Get-ADGroup "Group1").DistinguishedName

    I would also look at adding error handling to your script i.e.

    if ($null -ne $Variable){
        Complete the action
    }
  • #175858
    Jon

    Participant
    Topics: 13
    Replies: 221
    Points: 105
    Rank: Participant

    Also try this:

     

    Get-ADGroupMember -Identity GROUP-A | Add-ADPrincipalGroupMembership -MemberOf GROUP-B
  • #175891

    Participant
    Topics: 5
    Replies: 5
    Points: 54
    Rank: Member

    Jason, yes I can do that there are a few different ways to skin the cat I just need the easiest for a modification of a ton of code.  I read somewhere about doing the pipeline on MS and I think it was in relation to using a user account or something and thought the group would work just fine.  So wrote all this code then to see it is not working :(.  Lesson learned I guess.

    I rebuilt the code by doing the code change to use the foreach to cut the amount of work needed in the long run and used some excel tricks with concatenation to build what I needed after doing some splits.  Thanks everyone for the help.

    I would still be interested in knowing why I can't pass $_ to the -Members as a value if someone knows the answer.

  • #175918

    Participant
    Topics: 6
    Replies: 78
    Points: 143
    Helping Hand
    Rank: Participant

    Best explanations I've found are here and here.

    "Same as $PSItem. Contains the current object in the pipeline object. You can use this variable in commands that perform an action on every object or on selected objects in a pipeline."

    "The current pipeline object; used in script blocks, filters, the process clause of functions, where-object, ForEach-object and switch"

    I've only ever used it in a ForEach-object or a Where-Object. I don't think it's available outside of those constructs as it enumerates whatever is on the pipeline.

  • #175924

    Participant
    Topics: 0
    Replies: 100
    Points: 363
    Helping Hand
    Rank: Contributor

    Lawson,

    The $_ is used for the current pipeline object; used in script blocks, filters, the process clause of functions, where-object, ForEach-object and switch.

    The correct method to use is one of the the following methods. The third method, my preferred method, is above in my original post.

    $Member = Get-ADGroup "Group1"
    Add-ADGroupMember -Identity "GROUP2" -Members $Member
    Get-ADGroup "GROUP1" | Foeach-Object {Add-ADGroupMember -Identity "GROUP2" -Members $_.ObjectGUID}

    I recommend reading the help file for the parameters you want to use in a cmdlet. Take for instance the Members parameter for Add-ADGroupMember notice the Accept Pipeline input is set to false. What this means is the parameter won't accept the value for members from another object. Even if you didn't need to use the $_ place holder.

    help Add-ADGroupMember -Parameter Members | Select-String Accept
    
    Accept pipeline input? false
    Accept wildcard characters? false

     

You must be logged in to reply to this topic.