Cannot bind argument to parameter 'Name' because it is an empty string.

Hi everybody,

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 …)

# #$icsv = "C:\Users\xxxxxx\Desktop\distribution csv\Import_Name_Distribution.csv" #$member = "C:\Users\xxxxxx\Desktop\distribution csv\Import_memberDistr.csv" # ######################## # Import CSV $filepath1 = "C:\Users\xxxxxx\Desktop\distribution csv\Import_Name_Distribution2.csv" $users = import-csv $filepath1 # add domain $users = foreach-Object {$_.name} $domain = "@DOMAIN.FR" $PSmtpAddress = $_.name + $domain # Script variable $users | ForEach-Object { New-DistributionGroup -Name $_.name -Type $_.type -PrimarySmtpAddress $PSmtpAddress | Set-DistributionGroup -RequireSenderAuthenticationEnabled $False }

Thanks for your’ help :slight_smile:

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.

Thanks for replies :slight_smile:
yes, sure !

my csv :

name,type
xxxxx,Distribution
xxxxx,Distribution
xxxxx,Distribution
xxxxx,Distribution

Give this a try (Note: You will need to remove the -WhatIf to perform the update after you test):

#	Import CSV
$filepath1 = "C:\Users\xxxxxx\Desktop\distribution csv\Import_Name_Distribution2.csv"
$users = import-csv $filepath1

foreach ($user in $users) {
    $params = @{
         Name = $user.Name 
         Type = $user.Type 
         PrimarySmtpAddress = "{0}@DOMAIN.FR" -f $user.Name
    }
    
    New-DistributionGroup @params | 
    Set-DistributionGroup -RequireSenderAuthenticationEnabled $False -WhatIf
}

Ohhhh thanks, good !

A another reply by technet :

$filepath1 = "C:\Users\xxxxxx\Desktop\distribution csv\Import_Name_Distribution2.csv"
Import-Csv $filepath1 | Foreach {
    $PSmtpAddress = $_.name + '@DOMAIN.FR'
    New-DistributionGroup -Name $_.name -Type $_.type -PrimarySmtpAddress $PSmtpAddress | 
        Set-DistributionGroup -RequireSenderAuthenticationEnabled $False
}

Why my code doesn’t work ? (sorry for my english)

Whats difference between this code ?

Thansk ! :slight_smile:

Can you try to replace the foreach with ForEach-Object

If this does not work, What is the error?

it is already ForEach-Object

I have try with for each in à precedent script and doesnt 'work agin. But i progress ^^

A couple of problems:

  • 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
} 

Output:

@domain.fr
@domain.fr

Now try this code:

$computers = "Computer1", "Computer2"

foreach ($computer in $computers) {
    $PSmtpAddress = "{0}@domain.fr" -f $computer
    $PSmtpAddress
} 

Output:

Computer1@domain.fr
Computer2@domain.fr

Hi,

Thanks you for this. I progress