0
votes

I am trying to import a list of users from a csv file and I get the following error:

script:

PS C:\Software> import-csv C:\Software\lastDumpPolished.csv | % {
    New-ADUser -Name $_.Name -SamAccountName $_.SamAccountName `
      -DisplayName $_.Name -Surname $_.Sn -GivenName $_.GivenName `
      -AccountPassword (ConvertTo-SecureString password1234 `
      -AsPlainText -Force) -Enabled:$true -Title $_.Title `
      -Description $_.describtion -Company Myland `
      -OfficePhone $_.Telephone -HomeDrive Z: `
      -HomeDirectory $homedrive -ChangePasswordAtLogon:$true `
} | Enable-Mailbox -Identity $_.SamAccountName -Alias $_.SamAccountName

Error:

Enable-Mailbox : Cannot bind argument to parameter 'Identity' because it is null.
At line:1 char:443
+ ... lbox -Identity $_.SamAccountName -Alias $_.SamAccountName
+                    ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Enable-Mailbox], ParameterBind
   ingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,M
   icrosoft.Exchange.Management.RecipientTasks.EnableMailbox

Powershell doesn't remember $_.SamAccountName for some reason. It there an easy 'inline method' of setting it to a variable that can be used after the pipe?

How do I get rid of that error so I can create and enable the mailboxes in bulk?

1

1 Answers

0
votes
import-csv C:\Software\lastDumpPolished.csv | % {
    New-ADUser -Name $_.Name -SamAccountName $_.SamAccountName `
      -DisplayName $_.Name -Surname $_.Sn -GivenName $_.GivenName `
      -AccountPassword (ConvertTo-SecureString password1234 `
      -AsPlainText -Force) -Enabled:$true -Title $_.Title `
      -Description $_.describtion -Company Myland `
      -OfficePhone $_.Telephone -HomeDrive Z: `
      -HomeDirectory $homedrive -ChangePasswordAtLogon:$true `
} | % { Enable-Mailbox -Identity $_.SamAccountName -Alias $_.SamAccountName }