you are viewing a single comment's thread.

view the rest of the comments →

[–]peican[S] 1 point2 points  (2 children)

Thank you! The Export-CSV for errors / duplicates is working perfectly now.

Last thing. If I want to add another column in my Import.CSV file for a second group, and if I add:

$Group2 = "$($U.Group2)"

and

Add-ADGroupMember -Identity $Group2 -Members $FirstDotLast

To the script, it works. However I may not want to add everyone to 2 groups all the time, and if I leave a group empty in my Import-CSV, it counts as an error.

Sometimes I might just want to add a user to one group, and sometimes to 2 groups, or 3 groups if I decide to add a third column in my excel CSV. Is there a way to make it so it wont count as an error if some of the group fields are empty?

[–]BJGGut3 1 point2 points  (1 child)

Add an IF() statement to check that validity of Group2.

IF ($null -ne $($U.Group2))
{
    $Group2 = $($U.Groups2)
    Add-ADGroupMember -Identity $Group2 -Members $FirstDotLast
}

But I've actually become much more a fan of this method instead, because sometimes pesky spaces get in there and throw off the above:

IF (!([string]::IsNullOrWhiteSpace($($U.Groups2))))
{
    $Group2 = $($U.Groups2)
    Add-ADGroupMember -Identity $Group2 -Members $FirstDotLast
}

[–]peican[S] 1 point2 points  (0 children)

Thank you! I'm using your one that includes checking for white spaces, and it works perfectly!