none
Powershell Populate ADGroup from CSV RRS feed

  • Question

  • I have populated a csv file using the following

       Get-ADGroupMember -recursive "_Escalations" | Select samAccountName | FT > c:\temp\escalations.csv

    I now want to use the created csv file to populate another group

       import-csv c:\temp\escalations.csv | % {add-adgroupmember -identity _Escalations1}

    And I'm getting the following message

       cmdlet Add-ADGroupMember at command pipeline position 1
       Supply values for the following parameters:
       Members[0]:

    I assume this means that the csv file is not being read.

    Can anyone help point out where I'm going wrong.

    Thanks

    Thursday, April 17, 2014 9:03 AM

Answers

  • The pipeline is your friend.

    Get-ADGroupMember returns the distinguishedname which is an acceptable ADPrincipal which can be used by Add-ADGroupmember.

    Therefore, you can reference the current object by $_

    Get-ADGroupMember -recursive "_Escalations" | % {Add-ADGroupMember "_Escalations1" $_}


    • Marked as answer by pgilbert11 Thursday, April 17, 2014 3:20 PM
    Thursday, April 17, 2014 2:06 PM

All replies

  • You haven't included any members.

    % {add-adgroupmember -identity _Escalations1 -members $_.SamAccountName}






    Thursday, April 17, 2014 11:19 AM
  • Hi, tried that and now get he following message:

    Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Supply an argument
    that is not null or empty and then try the command again.
    At line:1 char:85
    + ... ions1 -members $_.samaccountname}
    +                    ~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGrou
       pMember

    Thursday, April 17, 2014 11:33 AM
  • Food for thought and investigation:

    Look at your CSV file contents .  It will be full of nonsense.  It is NOT a CSV file:

      Get-ADGroupMember -recursive "_Escalations" | Select samAccountName | FT > c:\temp\escalations.csv

    This command is nonsense.  It does NOT create a CSV file.  Look up and read how each command works and what it does.

    HELP Exprt-CSV -FULL

    http://en.wikipedia.org/wiki/Comma-separated_values

    In order to use the technology it is necessary to spend some time researching the definition of terms.  Just adding 'CSV' to the end of a filename does not make it a CSV file.  A CSV file is a very technical data structure used in computers.  The CmdLets you are using are not trivial.  PowerShell assumes a knowledge of how to use a complex scripting system.  Things cannot be arbitrarily pasted together.

    Why are you trying to send everything to a CSV file in the first place?


    ¯\_(ツ)_/¯



    • Edited by jrv Thursday, April 17, 2014 12:11 PM
    Thursday, April 17, 2014 12:10 PM
  • The pipeline is your friend.

    Get-ADGroupMember returns the distinguishedname which is an acceptable ADPrincipal which can be used by Add-ADGroupmember.

    Therefore, you can reference the current object by $_

    Get-ADGroupMember -recursive "_Escalations" | % {Add-ADGroupMember "_Escalations1" $_}


    • Marked as answer by pgilbert11 Thursday, April 17, 2014 3:20 PM
    Thursday, April 17, 2014 2:06 PM
  • Thanks all for your help, unfortunately I got this task from someone else who had a go and couldn't get it to work.

    Thanks to jrv, I didn't realise that the csv export was more involved

    Also thanks to Newbie Jones this was exactly what I needed.


    • Edited by pgilbert11 Thursday, April 17, 2014 3:22 PM
    Thursday, April 17, 2014 3:22 PM
  • It is usually better to not send things through file but, as noted above, use the pipeline or variables.


    ¯\_(ツ)_/¯

    Thursday, April 17, 2014 3:29 PM