0

I'm running a script in exchange management shell, using variables to output a command for creating user email accounts. Write-Host does a nice job showing the command string in the management shell, but of course does not execute it. I've had success using Invoke-Expression followed by a variable representing a string using commandlets such as New-Addresslists and New-Accepteddomain, but when using Invoke-Expression with a variable containing the New-Mailbox commandlet, I get error messages. How do I properly run a variable string from scripts specifically into the exchange management shell to create user email accounts?

Powershell / Exchange 2019

 $COMMAND = "New-Mailbox -Name $CustomerFirstName $CustomerLastName$ -UserPrincipalName $EmailPrefix@$CustomerDomainName -Password (ConvertTo-SecureString -String '$CustomerMailPassword' -AsPlainText -Force) -FirstName $CustomerFirstName -LastName $CustomerLastName"

  Invoke-Expression $COMMAND


  • Why not just `New-Mailbox -Name "$CustomerFirstName $CustomerLastName" -UserPrincipalName "$EmailPrefix@$CustomerDomainName" -Password (ConvertTo-SecureString -String '$CustomerMailPassword' -AsPlainText -Force) -FirstName $CustomerFirstName -LastName $CustomerLastName"`? – An-dir Dec 29 '22 at 09:36
  • I forgot to mention the execution is running within an "IF" statement. However, even when running on a separate line I get an error; A positional parameter cannot be found that accepts argument 'smith''. + CategoryInfo : InvalidArgument: (:) [New-Mailbox], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,New-Mailbox + PSComputerName : blabla – danmazur Dec 29 '22 at 13:25
  • Yet, I can copy and past the result of the string variable and it executes perfectly. – danmazur Dec 29 '22 at 13:28

0 Answers0