locked
Powershell AD Users, groups and CSV RRS feed

  • Question

  • Hi All, 

    I`m trying to create a script that will prompt for username, template and depending on the input of each it will add the user to specific security groups and distribution groups from the CSV. I have this working to an extent, it will add the user to one group but when I list a second group in the CSV it fails...here`s what I have so far;

    $user = Read-Host -Prompt "Enter User Name"
    $template = Read-Host -Prompt "Enter Template Name"
    $Salesgroups = get-content C:\temp\SalesGroup.csv
    $FinanceGroups = get-content C:\temp\FinanceGroup.csv

        If ($Template -eq "Sales")
        {Add-ADGroupMember -Identity $Salesgroups –Member $user}

        If ($Template -eq "Finance")
        {Add-ADGroupMember -Identity $FinanceGroups –Member $user}

    I`d also like a Write-Host message to confirm if it was successful and if the template isn`t "Sales" for example to move onto the next  "If" check. 

    Any help would be appreciated. 

    Scott.

    Monday, June 20, 2016 2:36 PM

Answers

  • Just to clarify. Is this the content of your csv?

    Name,
    Sales,
    Sales1,
    Distributiongroup,
    DL UK SALES

    You can only have one header per column. So maybe try it with this csv content

    "Name"
    "Sales"
    "Sales1"
    "DL UK SALES"

    And you obviously changed this line of code

     Add-ADGroupMember -Identity $group.Distributiongroups

    Maybe you can post both, your code and csv content. Makes it easier to help.

    Thanks,

    Andreas



    Monday, June 20, 2016 3:51 PM
  • Try to use one row for each group in your csv

    "JobTitle","Group"
    "SOE-Accounts Officer","Group1SOE"
    "SOE-Accounts Officer","Group2SOE"
    "HSD-Accounts Payable Assistant","Group1HSD"
    "HSD-Accounts Payable Assistant","Group2HSD"

    and this code

    [void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
    
    $user = [Microsoft.VisualBasic.Interaction]::inputbox('Please enter your user','User name')
    
    $csv = Import-Csv -Path C:\temp\Groups_new.csv 
    $template = $csv.JobTitle |
    Select-Object -Unique  |
    Out-GridView -OutputMode Single -Title 'Please select a Template'
    
    Write-Host 'Selected Template: '$template
    
    
    $csv |ForEach-Object -Process {
    if ($template -eq $_.JobTitle)
    {
        Add-ADGroupMember -Identity $_.group -Members $user
    }
    }

    Friday, August 12, 2016 8:44 AM

All replies

  • Hi,

    Add-ADGroupMember only works on a single group at a time. If you want multiple groups you'd need to loop.

    Alternatively, look into use Add-ADPrinicipalGroupMembership instead.

    http://ss64.com/ps/add-adgroupmember.html

    http://ss64.com/ps/add-adprincipalgroupmembership.html

    You don't need a Write-Host command for confirmation, you'll get an error if one occurs.


    Monday, June 20, 2016 2:51 PM
  • Format your csv content like this

    Name
    Group1
    Group2

    and use a code like this to loop

    $user = Read-Host -Prompt 'Enter User Name'
    $template = Read-Host -Prompt 'Enter Template Name'
    
    $Salesgroups = Import-Csv -Path C:\temp\SalesGroup.csv
    $FinanceGroups = Import-Csv -Path C:\temp\FinanceGroup.csv
    
    If ($template -eq 'Sales')
    { 
        foreach ($group in $Salesgroups)
        {
            Add-ADGroupMember -Identity $group.name -Member $user
        }
    }
    
    If ($template -eq 'Finance')
    { 
        foreach ($group in $FinanceGroups)
        {
            Add-ADGroupMember -Identity $group.name -Member $user
        }
    }

    • Proposed as answer by Andreas Bode Monday, June 20, 2016 3:52 PM
    Monday, June 20, 2016 3:00 PM
  • Thanks very much for the quick response, strangely the "Finance" template isn`t generating an error but "Sales" is;

     

    Add-ADGroupMember : Cannot find an object with identity: '' under: 'DC=test,DC=local'.
    At line:11 char:9
    +         Add-ADGroupMember -Identity $group.name -Member $user
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (:ADGroup) [Add-ADGroupMember], ADIdentityNotFoundException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management 
       .Commands.AddADGroupMember

    Add-ADGroupMember : Cannot find an object with identity: '' under: 'DC=test,DC=local'.
    At line:11 char:9
    +         Add-ADGroupMember -Identity $group.name -Member $user
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (:ADGroup) [Add-ADGroupMember], ADIdentityNotFoundException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management 
       .Commands.AddADGroupMember

    Any ideas? The CSV and commands are identical.

    Would I do the same for distribution groups?

    Thanks,

    Scott 


    Monday, June 20, 2016 3:18 PM
  • Hi,

    somehow the identity is null ($group.name). Maybe you have a break at the end of the csv file. Please verify your csv content. Is the user added to sales groups though?

    Monday, June 20, 2016 3:32 PM
  • Hi, 

    I`ve checked and it appears fine. Correct, groups are added.

    CSV format:

    Name,

    Sales,
    Sales1,

    Distributiongroup,

    DL UK SALES

    Error;

    Add-ADGroupMember : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again.
    At C:\Users\Administrator\Desktop\AD Script.ps1:16 char:37
    +         Add-ADGroupMember -Identity $group.Distributiongroups -Member $user
    +                                     ~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember
     
    Add-ADGroupMember : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again.
    At C:\Users\Administrator\Desktop\AD Script.ps1:16 char:37
    +         Add-ADGroupMember -Identity $group.Distributiongroups -Member $user
    +                                     ~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember
     
    Add-ADGroupMember : Cannot find an object with identity: '' under: 'DC=test,DC=local'.
    At C:\Users\Administrator\Desktop\AD Script.ps1:15 char:9
    +         Add-ADGroupMember -Identity $group.name -Member $user
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (:ADGroup) [Add-ADGroupMember], ADIdentityNotFoundException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management 
       .Commands.AddADGroupMember
     
    Add-ADGroupMember : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again.
    At C:\Users\Administrator\Desktop\AD Script.ps1:16 char:37
    +         Add-ADGroupMember -Identity $group.Distributiongroups -Member $user
    +                                     ~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember
     
    Add-ADGroupMember : Cannot find an object with identity: 'Distributiongroup' under: 'DC=test,DC=local'.
    At C:\Users\Administrator\Desktop\AD Script.ps1:15 char:9
    +         Add-ADGroupMember -Identity $group.name -Member $user
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (Distributiongroup:ADGroup) [Add-ADGroupMember], ADIdentityNotFoundException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management 
       .Commands.AddADGroupMember
     
    Add-ADGroupMember : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again.
    At C:\Users\Administrator\Desktop\AD Script.ps1:16 char:37
    +         Add-ADGroupMember -Identity $group.Distributiongroups -Member $user
    +                                     ~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember
     
    Add-ADGroupMember : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again.
    At C:\Users\Administrator\Desktop\AD Script.ps1:16 char:37
    +         Add-ADGroupMember -Identity $group.Distributiongroups -Member $user
    +                                     ~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember

    Thank you.

    Monday, June 20, 2016 3:41 PM
  • Just to clarify. Is this the content of your csv?

    Name,
    Sales,
    Sales1,
    Distributiongroup,
    DL UK SALES

    You can only have one header per column. So maybe try it with this csv content

    "Name"
    "Sales"
    "Sales1"
    "DL UK SALES"

    And you obviously changed this line of code

     Add-ADGroupMember -Identity $group.Distributiongroups

    Maybe you can post both, your code and csv content. Makes it easier to help.

    Thanks,

    Andreas



    Monday, June 20, 2016 3:51 PM
  • "Name" "Sales" "Sales1" "DL UK SALES"

    The above worked for me, thanks very much...saved me a lot of time, much appreciated.

    Scott.

    Monday, June 20, 2016 3:55 PM
  • Glad to hear that. Maybe you can mark my first post as answer.

    Thanks,

    Andreas

     
    Monday, June 20, 2016 3:59 PM
  • Hi All, 

    I know this is marked as answered but I have another question;

    With the above is there anyway for the CSV to contain all the security groups for each department? For example;

    If user is in Sales to add all the specific groups for Sales only which will be in the CSV, if the user is in Finance to do the same but pull it from the same CSV? or tab? I want to just have a master CSV if possible, otherwise I`ll have to create quite a few CSV`s (100s).

    Thanks in advance, 

    Scott.

    Tuesday, August 2, 2016 1:52 PM
  • Use the field names in your csv file to identify the groups. For example:

    Sales,Finance
    Sales1,Finance1
    Sales2,Finance2
    DL UK SALES,DL UK Finance

    Then reference the group names in your script, similar to below:

    $user = Read-Host -Prompt 'Enter User Name'
    $template = Read-Host -Prompt 'Enter Template Name'
    
    $Groups = Import-Csv -Path C:\temp\Groups.csv
    
    If ($template -eq 'Sales')
    { 
        foreach ($Group in $Groups)
        {
            Add-ADGroupMember -Identity $Group.Sales -Member $user
        }
    }
    
    If ($template -eq 'Finance')
    { 
        foreach ($Group in $Groups)
        {
            Add-ADGroupMember -Identity $group.Finance -Member $user
        }
    }


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)


    Tuesday, August 2, 2016 2:12 PM
  • Format your csv content like this

    Group,Dept
    Group1,Sales
    Group2,Sales
    Group3,HR
    Group2,HR

    and use a code like this to loop

    $user = Read-Host -Prompt 'Enter User Name'
    $template = Read-Host -Prompt 'Enter Template Name'
    
    $Groups = Import-Csv -Path C:\temp\Groups.csv
    
    foreach ($group in $groups){
    
        if ($group.dept -eq $template){
            
            Add-ADGroupMember -Identity $group.name -Member $user
    
        }
    
    }


    Tuesday, August 2, 2016 2:14 PM
  • Thanks for the quick reply from both of you.

    Andreas; This doesn`t appear to work, once I`ve entered username and template it then prompts again for username and template but not in the popup box.

    I think it`s because I have the Read-Host input twice, see below;

    [void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')

    $user = [Microsoft.VisualBasic.Interaction]::inputbox('Please enter your user','User name')
    $Template = "Finance","Sales","UK Accounts Officer" | Out-GridView -OutputMode Single -Title "Please select a Template"

    $user = Read-Host -Prompt 'Enter User Name'
    $template = Read-Host -Prompt 'Enter Template Name'

    $Groups = Import-Csv -Path C:\temp\Groups.csv

    foreach ($group in $groups){

        if ($group.dept -eq $template){
            
            Add-ADGroupMember -Identity $group.name -Member $user

        }

    }

    Thanks

    Scott


    Tuesday, August 2, 2016 2:27 PM
  • Hi Andreas, 

    I have tested the below but I think I have an issue with my CSV file, it worked for a small amount of columns but I now have just under 300 with a maximum of 50 rows in some of them.

    What I need is to prompt for a username, then select from a list of job titles, from these job titles it looks up the CSV and adds to correct groups to the user.

    I`m currently getting the below error over and over;

    Add-ADGroupMember : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again.
    At C:\Data\UK Infrastructure\New User Process\AD Script_V5.ps1:280 char:37
    + ...   Add-ADGroupMember -Identity $group."CMV-Accounts Assistant" -Member ...
    +                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember

    Script loop;

    If ($template -eq 'CMV-Accounts Assistant')

        foreach ($Group in $Groups)
        {
            Add-ADGroupMember -Identity $group."CMV-Accounts Assistant" -Members $user

    Thanks, 

    Scott


    Thursday, August 11, 2016 8:42 AM
  • A valid CSV file must have the same number of fields in each row. Each field should be delimited by commas (unless you specify another delimiter), and fields with embedded delimiters (commas) must be quoted.

    Is $Group."CMV-Accounts Assistant" the sAMAccountName or distinguishedName of the groups? Can it be missing?

    Edit: For troubleshooting, modify the script to just output the contents of the CSV file to make sure the fields make sense, or at least the fields that are important, like $Group."CMV-Accounts Assistant".


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)


    Thursday, August 11, 2016 2:50 PM
  • Hi Richard, 

    That`s definitely an issue I have with my CSV, is it possible to add something into the the rows so it continues?

    The "CMV-Account Assistant" is a job title which I`m trying to reference, I want to add specific groups for each job title, each job title has a different amount of groups I would like to add.

    I don`t know if there`s a simpler process I can try? 

    Here`s what I have so far, I have a loop for every job title;

    [void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')

    $user = [Microsoft.VisualBasic.Interaction]::inputbox('Please enter your user','User name')
    $Template = "CMV-Accounts Assistant",
    "SOE-Accounts Officer",
    "HSD-Accounts Payable Assistant",
    "HSD-Accounts Payable Supervisor",
    "HSAH-Accounts Payable Teamleader",
    "OPT-Administrator",
    "HSD-Advisor Human Resources",
    "VSL-Applications Specialist",
    "HSAH-Assistant Accountant",
    "HSAH-Assistant Accountant",
    "SOE-Best Practice Consultant",
    "HSD-Bids & Tenders Specialist",
    "HSD-Bids & Tenders Supervisor",
    "HSD-Business & Financial Analyst",
    "HSD-Business Analyst",
    "SOE-Business Development Coordinator",
    "HSD-Business Process Controller",
    "SOE-Business Solutions Manager",
    "SOE-Business Success Consultant",
    "HSD-Business Support Administrator",
    "HSD-Business Systems Analyst",
    "HSD-Business Systems Development Manager",
    "HSAH-Buyer","HSD-Cerec Sales Specialist",
    "HSD-CFO European Dental Northern & Western Region",
    "HSE-Chief Financial Officer",
    "OPT-Commercial Director",
    "HSD-Contracts Specialist",
    "VSL-Conversions Coordinator",
    "SOE-Conversions Manager",
    "SOE-Conversions Technician",
    "SOE-Corporate Accounts Manager", 
    "HSD-Corporate Brand Assistant",  
    "HSD-Creative Services Supervisor",  
    "HSD-Credit Control Supervisor",  
    "HSAH-Credit Control Team Leader",
    "HSD-Credit Controller", 
    "SOE-CRM Technical Account Manager",
    "HSD-CSS Manager",  
    "HSD-CSS Supervisor",
    "HSAH-Customer Service Data Administrator", 
    "HSD-Customer Service Manager",
    "HSAH-Customer Service Representative", 
    "HSAH-Customer Service Supervisor", 
    "HSD-Customer Service Team Leader",  
    "SOE-Customer Success & Primespeak Consultant", 
    "HSD-Data Analyst",  
    "HSAH-Data Analyst Administratort",
    "HSD-Decontamination Engineer",
    "HSD-Design Draughtsperson",
    "HSD-Designer",
    "VSL-Developer",
    "VSL-Development Manager",
    "HSD-Digital Imaging Specialist",
    "HSD-Director Business Systems",
    "HSD-Director Business Systems",
    "HSD-Director Diversified Healthcare",
    "HSD-Director Equipment",
    "HSD-Director of Sales",
    "HSD-Director Sales & Marketing International BA",
    "HSD-Director Special Markets",
    "HSD-Drawing Office Supervisor",
    "HSAH-Driver Team Leader - H",
    "SOE-EasyPost Sales Consultant",
    "HSD-Education & Events Manager",
    "HSD-Enterprise Administration Manager",
    "HSD-Enterprise Systems Manager",
    "HSD-Equipment Buyer",
    "HSD-Equipment Buying Assistant",
    "HSAH-Equipment Marketing Specialist",
    "HSD-Equipment Sales Manager",
    "HSD-Equipment Sales Specialist",
    "HSD-Equipment Sales Support",
    "HSD-Equipment Sales Support Team Leader",
    "HSE-European AH Commercial Development Director",
    "HSAH-European Marketing Director, Animal Health",
    "HSAH-European Product Manager Laser",
    "HSD-European Security Director",
    "HSD-European Special Buying Manager",
    "HSD-Events Co-Ordinator & Office Manager",
    "HSD-Exclusive Product Specialist",
    "OPT-Executive Assistant",
    "SOE-Features Implementation Team Leader",
    "VSL-Field Engineer",
    "HSD-Field Sales Consultant",
    "HSD-Finance Analyst",
    "OPT-Finance Manager",
    "HSE-Finance Planning & Analysis Manager",
    "HSD-Finance Project Manager",
    "HSD-Financial Accountant",
    "HSD-Financial Controller",
    "SOE-Financial Operations Manager",
    "SOE-Financial Operations Team Leader",
    "HSD-Financial Reporting Accountant",
    "HSD-Gatekeeper",
    "HSD-General Manager Business Solutions",
    "KEN-General Manager Direct Marketing",
    "HSD-General Manager Medical",
    "HSAH-Graduate Trainee",
    "SOE-Head of Corporate Business",
    "VSL-Head of Implementation & Product Development",
    "SOE-Head of Operations & Support",
    "VSL-Head of Sales & Marketing",
    "HSD-Helpdesk & Compliance Administrator",
    "HSD-Hi Tech Support Technician",
    "HSAH-HR Administrator",
    "HSD-Human Resource Manager",
    "HSAH-Human Resources Manager",
    "HSD-Human Resources Training Manager",
    "SOE-Head of Corporate Business",
    "VSL-Implementation Manager",
    "SOE-Implementation Operations Manager",
    "KEN-Inbound & Ecommerce Support",
    "KEN-Inbound Telesales Representative",
    "HSD-Infrastructure Analyst",
    "HSD-Infrastructure Engineer",
    "HSD-Installation Support Administrator",
    "HSD-Installations Supervisor",
    "HSD-Internal Sales Processing Co-Ordinator",
    "SOE-Internal Sales Team Leader",
    "HSD-International Finance Manager",
    "HSD-International Finance Manager",
    "HSD-International Tax Director",
    "HSD-International Tax Manager",
    "CMV-IRL - System Support",
    "HSD-IT Administrator",
    "HSAH-IT Customer Support Advisor",
    "HSD-IT Development Manager",
    "HSD-IT Manager",
    "HSD-IT Specialist",
    "HSAH-IT Team Leader",
    "HSD-IT Technical Support",
    "HSAH-Junior Buyer",
    "HSD-Junior CSS Advisor",
    "VI-Key Accounts Manager",
    "HSD-Laboratory Business Manager",
    "HSD-Laboratory Field Sales Manager",
    "HSD-Laser Specialist",
    "HSD-Lead Analyst",
    "HSD-License & Data Centre Supervisor",
    "HSD-Logistics Controller",
    "HSAH-Maintenance Team Leader",
    "SOE-Management Accountant",
    "SOE-Manager, Corporate Brand and Category Management",
    "SOE-Managing Director",
    "HSIRL-Managing Director Republic Of Ireland",
    "HSD-Margin Enhancement Systems Analyst",
    "HSAH-Marketing Administrator",
    "SOE-Marketing Executive",
    "KEN-Marketing Services Manager",
    "VSL-Module Development Consultant",
    "SOE-MPC Consultant",
    "OPT-National Sales Manager",
    "SOE-National Sales Manager, Corporates",
    "RXW-Office Administrator",
    "HSD-Office Manager",
    "SOE-Online Booking Consultant",
    "HSD-Operations Director",
    "HSD-Operations Manager",
    "HSAH-Operations Safety & Security Supervisor",
    "HSD-Operations Supervisor",
    "HSAH-Operations Team Leader",
    "HSD-Order Point Systems Manager",
    "HSD-Order Processor",
    "HSD-Orthodontic Product Manager",
    "KEN-Outbound Telesales Representative",
    "SOE-Patient Appointment Coordinator",
    "SOE-Patient Marketing Consultant",
    "HSD-Payroll & Benefits Coordinator",
    "HSD-Payroll & Benefits Manager",
    "HSD-Payroll & Benefits Officer",
    "SOE-President International Healthcare Group",
    "SOE-Product Coordinator",
    "VSL-Product Development Manager",
    "VI-Product Development Work Placement",
    "HSAH-Product Manager",
    "HSD-Profit Improvement Analyst",
    "HSD-Project Manager CRM",
    "SOE-Public Sector Sales Executive",
    "HSD-Purchasing Assistant",
    "HSD-Purchasing Director",
    "CMV-Purchasing Manager",
    "HSD-Purchasing Supervisor",
    "HSAH-Purchasing Team Leader",
    "VSL-QA Lead",
    "HSD-Quality & Compliance Manager - Regulatory",
    "VI-Quality Control Manager",
    "SOE-Recall Specialist",
    "HSD-Receptionist",
    "SOE-Regional Business Manager",
    "HSD-Regional Sales Manager",
    "SOE-Sales Administration Team Leader",
    "SOE-Sales Administrator",
    "HSD-Sales and Marketing Manager",
    "HSD-Sales Ledger Assistant",
    "HSD-Sales Manager",
    "CMV-Sales Representative",
    "HSD-Security & Safety Supervisor",
    "HSAH-Senior Buyer",
    "VSL-Senior Client Support Technician",
    "HSD-Senior Credit Controller",
    "HSD-Senior Finance Analyst",
    "HSD-Senior Financial Accountant",
    "HSD-Senior IT Technical Support",
    "HSD-Senior Management Accountant",
    "KEN-Senior Manager Direct Marketing",
    "HSD-Senior Manager Financial Reporting",
    "HSD-Senior Manager IT Infrastructure",
    "HSD-Senior Sales Ledger Assistant",
    "HSD-Senior Service Technician",
    "SOE-Senior Thrive Administrator",
    "HSD-Service & Sales Team Leader",
    "OPT-Service Administrator",
    "HSD-Service Coordinator",
    "HSD-Service Desk Administrator",
    "HSIRL-Service Desk Coordinator",
    "HSD-Service Manager",
    "HSD-Service Technician",
    "SOE-SMS Sales Consultant",
    "SOE-Software Support Manager",
    "SOE-Software Support Technician",
    "HSD-Special Markets Contract Analyst",
    "HSD-Special Markets Customer Support Manager",
    "HSD-Special Markets Representative",
    "HSD-Speciality Buyer",
    "HSD-STIBO Supervisor",
    "HSD-STIBO Team Leader",
    "HSD-Stores Person",
    "SOE-Support Services Executive",
    "SOE-Support Team Leader",
    "SOE-Support Team Trainer",
    "HSD-Surgical Product Manager",
    "HSD-Surgical Sales Specialist",
    "VSL-Systems Engineer",
    "VI-Technical Director",
    "HSD-Technical Operations Manager",
    "OPT-Technical Services Manager",
    "HSD-Technical Support Coordinator",
    "VI-Technical Support Manager",
    "VI-Technical Support Nurse",
    "HSD-Technical Support Supervisor",
    "HSD-Telesales Manager",
    "HSD-Telesales Representative",
    "HSD-Telesales Representative Support",
    "HSD-Telesales Supervisor",
    "KEN-Telesales Team Leader",
    "SOE-Thrive Administrator",
    "SOE-Thrive Business Development Manager",
    "HSAH-Trade Supervisor & Fieldsales Consultant",
    "HSD-Trainee Engineer",
    "HSD-Trainee Junior Designer",
    "VSL-Trainer",
    "HSD-Training & Benefits Coordinator",
    "SOE-Training Consultant",
    "SOE-Training Manager",
    "CMV-Transport Manager",
    "HSAH-Transport Supervisor",
    "HSD-Treasury Analyst",
    "SOE-UK Product Release",
    "HSD-Ultradent Product Manager",
    "HSAH-Vice President & Managing Director UK, Ireland & France Region",
    "HSE-Vice President European Animal Health",
    "HSD-Vice President European Dental Northern Region",
    "HSD-Vice President Human Resources",
    "HSD-Vice President Operations UK & Western Europe",
    "HSD-Vice President UK & Western Europe Inventory Management",
    "VSL-VSL QA Test Engineer",
    "HSD-Worldwide Inventory Process Manager" | Out-GridView -OutputMode Single -Title "Please select a Template"


    $Groups = Import-Csv -Path C:\temp\Groups_new.csv 

    If ($template -eq 'CMV-Accounts Assistant')

        foreach ($Group in $Groups)
        {
            Add-ADGroupMember -Identity $group."CMV-Accounts Assistant" -Members $user
        }
    }

    If ($template -eq 'SOE-Accounts Officer')
    { cls
        foreach ($Group in $Groups)
        {
            Add-ADGroupMember -Identity $group."SOE-Accounts Office" -Members $user
        }
    }

    If ($template -eq 'HSD-Accounts Payable Assistant')

        foreach ($Group in $Groups)
        {
            Add-ADGroupMember -Identity $group."HSD-Accounts Payable Assistant" -Members $user
        }
    }


    Thanks

    Scott.

    Thursday, August 11, 2016 2:59 PM
  • I guess this is your issue: $group."CMV-Accounts Assistant"

    How does your csv look like?


    Friday, August 12, 2016 8:12 AM
  • So I have all of the job titles along the top, followed by x amount of groups below if.

    300 columns followed by a varied number of rows for each.

    Similar to the below;

    Thanks,

    Scott

    Friday, August 12, 2016 8:20 AM
  • We have to rethink this. That is not a valid CSV, as there are different numbers of fields in each row. That explains the errors about null arguments.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Friday, August 12, 2016 8:36 AM
  • Try to use one row for each group in your csv

    "JobTitle","Group"
    "SOE-Accounts Officer","Group1SOE"
    "SOE-Accounts Officer","Group2SOE"
    "HSD-Accounts Payable Assistant","Group1HSD"
    "HSD-Accounts Payable Assistant","Group2HSD"

    and this code

    [void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
    
    $user = [Microsoft.VisualBasic.Interaction]::inputbox('Please enter your user','User name')
    
    $csv = Import-Csv -Path C:\temp\Groups_new.csv 
    $template = $csv.JobTitle |
    Select-Object -Unique  |
    Out-GridView -OutputMode Single -Title 'Please select a Template'
    
    Write-Host 'Selected Template: '$template
    
    
    $csv |ForEach-Object -Process {
    if ($template -eq $_.JobTitle)
    {
        Add-ADGroupMember -Identity $_.group -Members $user
    }
    }

    Friday, August 12, 2016 8:44 AM
  • Hi Andreas,

    It looks like this has worked, I think the main problem (apart from my scripting skills) is the ability to create CSV files correctly, I created this format in excel and saved as CSV but the format was incorrect when I opened it in notepad. 

    I manually created the format in notepad and it worked, is there a quick way to add quotations to all of these job titles and groups?

    Thanks very much for both of your input. 

    Thanks

    Scott

    Friday, August 12, 2016 9:22 AM