none
How to get Import-CSV to work with Add-Adgroupmember

    Question

  • Hello,

    My goal is to populate an Active Directory Global Security Group with members which are contained in a CSV file.

    I execute the following:

    $members = Import-CSV c:\GroupMembers.Csv

    $members | ForEach-Object {add-Adgroupmember -Identity 'AD_Group' -members $_}

    These are the errors I get:

    Add-ADGroupMember : Cannot bind parameter 'Members'. Cannot convert the "@{a0100041=a62008}" value of t
    ype "System.Management.Automation.PSCustomObject" to type "Microsoft.ActiveDirectory.Management.ADPrinc
    ipal".
    At line:1 char:78
    + $members | ForEach-Object {add-Adgroupmember -Identity 'AD_Group'  -members <<<<  $_}
        + CategoryInfo          : InvalidArgument: (:) [Add-ADGroupMember], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.ActiveDirectory.Management.Comm
       ands.AddADGroupMember

    Is this saying Import-CSV produces an object of a certain type the ForEach-Object cmdlet cant handle?  Thanks in advance.


    Thanks for your help! SdeDot
    Wednesday, October 05, 2011 1:21 PM

Answers

  • Hi,
     
    What column header you have in csv ? If you havent it try that:

    $members = Import-CSV p:\test.Csv -Header group
    $members | ForEach-Object {add-Adgroupmember -Identity 'tg' -members $_.group}

    If you have header in file then leave -Header group and in foreach use $_.<name of header>

     

    • Edited by MichalGajdaMVP Wednesday, October 05, 2011 1:36 PM
    • Marked as answer by SdeDot Wednesday, October 05, 2011 1:58 PM
    Wednesday, October 05, 2011 1:34 PM
  • Yes, that is what it is saying with this message:

    Cannot convert the "@{a0100041=a62008}" value of type "System.Management.Automation.PSCustomObject" to type "Microsoft.ActiveDirectory.Management.ADPrincipal".

    Each member (row) of $members is itself an array (of columns).  Perhaps this approach would help:

     

    $members | ForEach-Object {add-Adgroupmember -Identity 'AD_Group' -members $_.samAccountname}
    

     

    ...assuming your csv file has a column called samAccountName.


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    • Edited by Bigteddy Wednesday, October 05, 2011 1:40 PM
    • Marked as answer by SdeDot Wednesday, October 05, 2011 1:58 PM
    Wednesday, October 05, 2011 1:39 PM

All replies

  • Hi,
     
    What column header you have in csv ? If you havent it try that:

    $members = Import-CSV p:\test.Csv -Header group
    $members | ForEach-Object {add-Adgroupmember -Identity 'tg' -members $_.group}

    If you have header in file then leave -Header group and in foreach use $_.<name of header>

     

    • Edited by MichalGajdaMVP Wednesday, October 05, 2011 1:36 PM
    • Marked as answer by SdeDot Wednesday, October 05, 2011 1:58 PM
    Wednesday, October 05, 2011 1:34 PM
  • Yes, that is what it is saying with this message:

    Cannot convert the "@{a0100041=a62008}" value of type "System.Management.Automation.PSCustomObject" to type "Microsoft.ActiveDirectory.Management.ADPrincipal".

    Each member (row) of $members is itself an array (of columns).  Perhaps this approach would help:

     

    $members | ForEach-Object {add-Adgroupmember -Identity 'AD_Group' -members $_.samAccountname}
    

     

    ...assuming your csv file has a column called samAccountName.


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    • Edited by Bigteddy Wednesday, October 05, 2011 1:40 PM
    • Marked as answer by SdeDot Wednesday, October 05, 2011 1:58 PM
    Wednesday, October 05, 2011 1:39 PM
  • Thanks guys for the responses and help.

    MichalGaja: I did not have a header in my CSV file.  When I made the revisions you suggested, it worked. Thank you.

    BigTeddy: Your explanation makes sense.  Thank you.


    Thanks for your help! SdeDot
    Wednesday, October 05, 2011 1:58 PM