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

This topic contains 8 replies, has 4 voices, and was last updated by Profile photo of frshell frshell 1 week, 4 days ago.

  • Author
    Posts
  • #58525
    Profile photo of frshell
    frshell
    Participant

    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 🙂

  • #58526
    Profile photo of Matt McNabb
    Matt McNabb
    Participant

    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.

  • #58532
    Profile photo of frshell
    frshell
    Participant

    Thanks for replies 🙂
    yes, sure !

    my csv :

    name,type
    xxxxx,Distribution
    xxxxx,Distribution
    xxxxx,Distribution
    xxxxx,Distribution
    
  • #58552
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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
    }
    
    • #58562
      Profile photo of frshell
      frshell
      Participant

      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 ! 🙂

    • #58564
      Profile photo of Albert van Boerum
      Albert van Boerum
      Participant

      Can you try to replace the foreach with ForEach-Object

      If this does not work, What is the error?

  • #58567
    Profile photo of frshell
    frshell
    Participant

    it is already ForEach-Object

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

  • #58571
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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
    
  • #58595
    Profile photo of frshell
    frshell
    Participant

    Hi,

    Thanks you for this. I progress

You must be logged in to reply to this topic.