I try to script for office 365 adding distribution groups and then add users later.
But my code does not work until the end … (I learn the powershell …)
You’re passing in the “name” property of the $users, which is imported from a CSV file. Are you certain that the CSV file contains a header called name? If it does, are there rows which do not contain a value in that column? The error indicates that $_.name is causing the issue.
This line overwrites your CSV: $users = foreach-Object {$_.name}. When you attempt to reference $_.Type, it would not exist. It actually converts it to an array of computers "Computer1", Computer2"....
This line is outside of your loop: $PSmtpAddress = $_.name + $domain. This means that it has no idea what $_.Name is, but as it's outside the loop the value would not change. So, if it resolved the $_.Name to Computer1 and then you loop through Computer1 and Computer2, when you loop to Computer2 it would still be using Computer1 because it's not resolved in the loop. See the example below
$computers = "Computer1", "Computer2"
#Here, we reference computer, so we create the $computer
#variable in the loop, so it has no idea what $computer is
$PSmtpAddress = "{0}@domain.fr" -f $computer
foreach ($computer in $computers) {
$PSmtpAddress
}