Enable-Mailbox via CSV on Remote Exchange2010

Hi Powershell users.

I have a script that creates users from a csv file. I also want the script to create a new-mailbox for these users in the csv file.

But im getting this error:

Value cannot be null.
Parameter name: serverSettings
+ CategoryInfo : NotSpecified: (:slight_smile: [Enable-Mailbox], ArgumentNullException
+ FullyQualifiedErrorId : System.ArgumentNullException,Microsoft.Exchange.Management.RecipientTasks.EnableMailbox
+ PSComputerName : VMSRVMail2

$mail = New-PSSession -ComputerName VMSRVMail2 
$users = import-csv 'C:\Script\users.csv' -Delimiter ';'
Foreach ($user in $users) {
    New-ADUser -Name ($user.fornavn + " " + $user.efternavn) -DisplayName ($user.fornavn + " " + $user.efternavn) -Title $user.titel -Description $user.beskrivelse -GivenName $user.fornavn -SurName $user.efternavn -SamAccountName $user.samnavn -UserPrincipalName $user.upn -Path $user.'path,,,' -ScriptPath "logon.bat" -HomeDrive "U:" -HomeDirectory "\\vmsrv2\CitrixData$\brugerbib\$sam" -AccountPassword (convertto-securestring $user.password -AsPlainText -Force) -ChangePasswordAtLogon:$True -Enabled:$True
    $sam = $user.samnavn
    $alias = $user.samnavn
    
    Invoke-Command -Session $mail -ScriptBlock {
        add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
        Enable-Mailbox -Identity $Using:alias  
     } 
}

I have also tried with this line: Enable-Mailbox -Identity ‘test.local/Bruger’ -Alias $_.alias -Database ‘MBX1’
Which is giving me:

Cannot validate argument on parameter ‘Alias’. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
+ CategoryInfo : InvalidData: (:slight_smile: [Enable-Mailbox], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Exchange.Management.RecipientTasks.EnableMailbox
+ PSComputerName : VMSRVMail2

In your example, you’re only providing -Identity. The error is indicating that it wants more than that.

In:

Enable-Mailbox -Identity 'test.local/Bruger' -Alias $_.alias -Database 'MBX1'

$_ has no meaning. To reference $alias, you would use $using:alias, just as you did in your original example.

I would focus on getting Enable-Mailbox to work, within Invoke-Command, using static values. Once you have it working using entirely static values, you can start substituting variables, and you’ll know that the command will either (a) work, or that (b) your variables don’t contain the values you thought they did.

With the Exchange snappin you shouldn’t add it like that. Exchange uses RBAC and when you connect even on the local server it makes the connection through remoteing. Try adding a session for the connection to your Exchange server and see if that helps.

$Session = New-PSSession -configurationName Microsoft.Exchange -connectionURI http://VMSRVMail2 .domain.com/powershell/ -Authentication Kerberos

Import-PSSession $Session

I just got new error’s

New-PSSession : [vmsrvmail2.test.local] Connecting to remote server vmsrvmail2.test.local failed with the following error message : The WinRM client received an HTTP status code of 403 from the remote WS-Managem
ent service. For more information, see the about_Remote_Troubleshooting Help topic.
At line:3 char:12

  • $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri ht …
  •   + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException
      + FullyQualifiedErrorId : -2144108273,PSSessionOpenFailed
    

New-ADUser : The specified account already exists
At line:6 char:5

  • New-ADUser -Name ($user.fornavn + " " + $user.efternavn) -DisplayName ($user ...
    
  •   + CategoryInfo          : ResourceExists: (CN=Michael Rasm...=test,dc=local:String) [New-ADUser], ADIdentityAlreadyExistsException
      + FullyQualifiedErrorId : ActiveDirectoryServer:1316,Microsoft.ActiveDirectory.Management.Commands.NewADUser
    
    

Invoke-Command : Cannot validate argument on parameter ‘Session’. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:9 char:31

  •   Invoke-Command -Session $Session -ScriptBlock {
    
  •                           ~~~~~~~~
    
    • CategoryInfo : InvalidData: (:slight_smile: [Invoke-Command], ParameterBindingValidationException
    • FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://VMSRVMail2.test.local/WindowsPowerShell/ -Authentication Kerberos
$users = import-csv 'C:\Script\users.csv' -Delimiter ';'
Foreach ($user in $users) {
    New-ADUser -Name ($user.fornavn + " " + $user.efternavn) -DisplayName ($user.fornavn + " " + $user.efternavn) -Title $user.titel -Description $user.beskrivelse -GivenName $user.fornavn -SurName $user.efternavn -SamAccountName $user.samnavn -UserPrincipalName $user.upn -Path $user.'path,,,' -ScriptPath "logon.bat" -HomeDrive "U:" -HomeDirectory "\\vmsrv2\CitrixData$\brugerbib\$sam" -AccountPassword (convertto-securestring $user.password -AsPlainText -Force) -ChangePasswordAtLogon:$True -Enabled:$True
    $sam = $user.samnavn
       
      Invoke-Command -Session $Session -ScriptBlock {      
      add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
        Enable-Mailbox -Identity $Using:samnavn   
     } 
}

Remoting has been enabled.

If it wont work, I will just use the mail server to create users with the Import-Module.

I believe this is going to be terribly inefficient because you are calling the Add-PSSnapin inside of your foreach statement. So it will be establishing the remote session and trying to download the Exchange cmdlets repeatedly for each iteration.

Simplest solution would be to run this from your Exchange Server within the Exchange Management Shell window and you could eliminate the remoting section. But if you insist on remoting you would want to establish the session first. I don’t know about your particular environment but you may need to input your credentials to do the remoting:

The default powershell directory is “PowerShell” not “WindowsPowerShell”, again might be different in your environment, but just pointing it out incase, I’ve corrected the URL

$credential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://VMSRVMail2.test.local/PowerShell/ -Authentication Kerberos -Credential $credential
Import-PSSession $Session

You may or may not need to import the Active Directory module depending where you run this from

Import-Module ActiveDirectory

Couple things with your foreach, you use the variable $sam when setting the “HomeDirectory” property for the new user, but you didn’t actually define the $sam variable until the next line, so we put this before your New-ADUser line. Also you had a typo “-Title $user.titel” fixed to -Title $user.title

Foreach ($user in $users) {
    $sam = $user.samnavn
    New-ADUser -Name ($user.fornavn + " " + $user.efternavn) -DisplayName ($user.fornavn + " " + $user.efternavn) -Title $user.title -Description $user.beskrivelse -GivenName $user.fornavn -SurName $user.efternavn -SamAccountName $user.samnavn -UserPrincipalName $user.upn -Path $user.'path,,,' -ScriptPath "logon.bat" -HomeDrive "U:" -HomeDirectory "\\vmsrv2\CitrixData$\brugerbib\$sam" -AccountPassword (convertto-securestring $user.password -AsPlainText -Force) -ChangePasswordAtLogon:$True -Enabled:$True

Enable-Mailbox -Identity $sam
}

Complete Script:

#Import Exchange cmdlets
$credential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://VMSRVMail2.test.local/PowerShell/ -Authentication Kerberos -Credential $credential
Import-PSSession $Session

#Optional, import Active Directory cmdlets
Import-Module ActiveDirectory

#Import the CSV and Create an ADUser
$users = import-csv 'C:\Script\users.csv' -Delimiter ';'

Foreach ($user in $users) {
       $sam = $user.samnavn
       New-ADUser -Name ($user.fornavn + " " + $user.efternavn) -DisplayName ($user.fornavn + " " + $user.efternavn) -Title $user.title -Description $user.beskrivelse -GivenName $user.fornavn -SurName $user.efternavn -SamAccountName $user.samnavn -UserPrincipalName $user.upn -Path $user.'path,,,' -ScriptPath "logon.bat" -HomeDrive "U:" -HomeDirectory "\\vmsrv2\CitrixData$\brugerbib\$sam" -AccountPassword (convertto-securestring $user.password -AsPlainText -Force) -ChangePasswordAtLogon:$True -Enabled:$True

       Enable-Mailbox -Identity $sam
       }

Thank you for the help, I like Cheese.
This works. :slight_smile:

Finally!!

Cheers