locked
Information on working of Pipeline of powerhsell RRS feed

  • Question

  • Okay, here we go.

    I was testing some AD cmdlets in my lab. 
    Primarily Add-ADPrincipalGroupMembership & Get-ADPrincipalGroupMembership

    I used a scriptblock to copy all groups of the user ADMINISTRATOR to all the newly created admin accounts in my lab.

    Get-ADUser -Filter * -SearchBase "<OU NAME>" | % { Add-ADPrincipalGroupMembership -Identity $_ -MemberOf (Get-ADPrincipalGroupMembership -Identity administrator) }


    Now I tried this in a different way, and I got an error.


    get-adprincipalgroupmembership administrator | % { add-adprincipalgroupmembership -identity (Get-ADUser -Filter * -SearchBase "OU=AdministrativeUsers,OU=UserObjects,OU=HomelabObjects,DC=homelab,DC=in") -memberof $_ }



    The error being:

    Add-ADPrincipalGroupMembership : Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADP
    rincipal' required by parameter 'Identity'. Specified method is not supported.
    At line:1 char:92
    + get-adprincipalgroupmembership administrator | % { add-adprincipalgroupmembership -identity <<<<  (Get-ADObject -Filt
    er * -SearchBase "OU=AdministrativeUsers,OU=UserObjects,OU=HomelabObjects,DC=homelab,DC=in") -memberof $_ }
        + CategoryInfo          : InvalidArgument: (:) [Add-ADPrincipalGroupMembership], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.AddADPrincipalGroupM
       embership

    I checked a little and found Get-ADUser -Filter * -SearchBase "<OU NAME>" created objects of the below type
    TypeName: Microsoft.ActiveDirectory.Management.ADUser

    Now the question is, when I pipe it in, one by one using foreach this works, but when I use parenthesis and pipe the AD groups it doesnt. 

    Oh, I know that -Memberof doesnt take pipeline input, but the error doesnt say anything about memberof, it is talking about parameter identity

    Can someone explain me why ?

    Thank you in Advance :)



    Sunday, September 20, 2015 3:27 PM

Answers

  • Are you trying to say that you want to copy the membership of a user to every account in an OU?

    If that is what you are trying to do then your second example doesn't make any sense.  It is redundant.

    Don't jam everything on one line and it may be easier to see.

    get-adprincipalgroupmembership administrator |
        %{
            add-adprincipalgroupmembership -identity
                   (Get-ADUser -Filter * -SearchBase "OU=AdministrativeUsers,OU=UserObjects,OU=HomelabObjects,DC=homelab,DC=in")
            -memberof $_
        }

    Look up identity to see what it is.


    \_(ツ)_/

    Sunday, September 20, 2015 4:30 PM
  • Identity is a single value. You are trying to send it a collection.  I know other properties of other CmdLets can take a collection or array.  Look into how to read the help and the definition of the parameters.  "ByValue" has nothing to do with it.  What is the type?

    Note: "you can set this parameter to an object instance." 

    An instance not a collection.


    \_(ツ)_/

    Sunday, September 20, 2015 4:56 PM

All replies

  • What are you trying to do?  The code is wrong no matter what it may be but it is not possible to figure out what you are trying to accomplish from the bad code.

    Start with: "HELP get-adprincipalgroupmembership -full"

    State what it is you are trying to do in plain English.


    \_(ツ)_/

    Sunday, September 20, 2015 3:45 PM
  • Hey,

    Thank you for your reply. Administrator has several groups like "Domain Admins or Schema Admins" etc.

    So I extracted those groups and added the same groups to a bunch of users from an OU. The below code works :)

    Get-ADUser -Filter * -SearchBase "<OU NAME>" | % { Add-ADPrincipalGroupMembership -Identity $_ -MemberOf (Get-ADPrincipalGroupMembership -Identity administrator) }

    Now I was trying to do it in another way, by using the user information for identity from a parenthesis and piping the groups one by one to memberof.

    I have read the help. Was confused with that error for the second code block. Hence asked.

    Having said all this. I am going back to the chapter for pipeline as I am clearly stuck on usage of it :)



    Sunday, September 20, 2015 3:53 PM
  • Are you trying to say that you want to copy the membership of a user to every account in an OU?

    If that is what you are trying to do then your second example doesn't make any sense.  It is redundant.

    Don't jam everything on one line and it may be easier to see.

    get-adprincipalgroupmembership administrator |
        %{
            add-adprincipalgroupmembership -identity
                   (Get-ADUser -Filter * -SearchBase "OU=AdministrativeUsers,OU=UserObjects,OU=HomelabObjects,DC=homelab,DC=in")
            -memberof $_
        }

    Look up identity to see what it is.


    \_(ツ)_/

    Sunday, September 20, 2015 4:30 PM
  • Thanks for your input. I am studying the chapter again. Identity is (byvalue). So still a lil confused. Hopefully will get clear once i am done with it

    If you find this helpful, kindly mark as answer. If you have any queries, please post back as a reply. Will look forward to your feedback. Thanking You Soumyajyoti Biswas

    Sunday, September 20, 2015 4:49 PM
  • Identity is a single value. You are trying to send it a collection.  I know other properties of other CmdLets can take a collection or array.  Look into how to read the help and the definition of the parameters.  "ByValue" has nothing to do with it.  What is the type?

    Note: "you can set this parameter to an object instance." 

    An instance not a collection.


    \_(ツ)_/

    Sunday, September 20, 2015 4:56 PM
  • Hello,

    Your are right. Brilliant

    I read the help file again

    Get-ADUser -Filter * -SearchBase "<OU NAME>" --  is a collection, right ?
    where each row represents an object and the columns being properties of it.

    So I send a collection to the second code and it doesnt work.

    So in the first code it was one by one and it works. :)

    Thanks Now i understand.


    If you find this helpful, kindly mark as answer. If you have any queries, please post back as a reply. Will look forward to your feedback. Thanking You Soumyajyoti Biswas


    Sunday, September 20, 2015 5:18 PM