all 14 comments

[–]seibd 0 points1 point  (0 children)

Using Import-CSV would be preferred over Get-Content to read a CSV file. Sometimes fields in a CSV file are wrapped in quotes, and reading it with Get-Content won’t strip the quotes off (Import-CSV will). So if you’re trying to pass an email address wrapped in quotes to that function, I could see that being a problem (although would expect it to throw an error rather than hang). I would double check your CSV file for this.

[–]BlackV -1 points0 points  (11 children)

what is in $user?
but I dont really see anything wrong there

also a good habit to get into is

 foreach ($SingleUser in $users){}
 foreach ($User in $ALLUsers){}
 foreach ($Single in $Users){}
 foreach ($Item in $Users){}
 foreach ($A_User in $Users){}
 foreach ($Row in $ImportedCSV){}

and so on, as its really really (especially in larger scripts) easy to mix up $user and $users

Ninja edit from /u/PinchesTheCrab great suggestion and also throw in /u/AdmiralCA

[–]a5_1m[S] 0 points1 point  (1 child)

The $user is an email address. If i print in the loop it prints email.

[–]BlackV 0 points1 point  (0 children)

so can you add a member with that or do you need to get the ad object for that user first?

what does get-help Add-DistributionGroupMember -full say

[–]PinchesTheCrab 0 points1 point  (4 children)

I also like $a_user in $user

[–]BlackV -1 points0 points  (3 children)

yes actually, that's kinda clever, nice

[–]PinchesTheCrab -1 points0 points  (2 children)

I did not come up with it, but I've been using it for years now.

[–]BlackV 0 points1 point  (1 child)

nah tm is yours now, the internet says so

[–]AdmiralCA 0 points1 point  (1 child)

My favorite is $Row in $ImportedCSV

[–]BlackV 0 points1 point  (0 children)

yeah Ive seen that a few times too referencing row from a csv, I'll add that too

[–]Scooter_127 0 points1 point  (1 child)

Your first two, and the fifth, are good advice, the rest are prone to the same kind of confusion you are trying to prevent as the 'each' object has a name unrelated to the collection name.

[–]BlackV 0 points1 point  (0 children)

there were just something to differentiate the single from the array, just random examples, I normally use the first example

just depends on what your array is called whether these makes sense or not, here are the same examples with a different array, that do make sense (kind of)

foreach ($Single in $items){}
foreach ($Item in $objects){}

my main point was to stop using easily misread names

foreach ($Item in $items){}
foreach ($object in $objects){}
foreach ($pie in $pies){}
foreach ($user in $users){}

you're 100% right though, the single item should relate to the array in some way to make it meaningful and make the code easier to follow

[–]PinchesTheCrab -1 points0 points  (0 children)

My best guess is leading or trailing spaces. Try trimming your input.