Problem with script to add a new email address to a user
Hello, I am trying to run a Powershell command to add a new address to a user. If I run the command: Code Snippet $user = Get-Mailbox -Identity "doim1@intra.net.ase.ro"; $user.emailaddresses.Remove("testdoim1@yahoo.com"); $user | Set-Mailbox in the Exchange Management Shell the address is added to the user OK. However I need to run the command like this Code Snippet C:\WINDOWS\system32\windowspowershell\v1.0\PowerShell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" $user = Get-Mailbox -Identity "doim1@test.ro"; $user.emailaddresses.Add("testdoim1@yahoo.com"); $user | Set-Mailbox i get the following error:'Set-Mailbox' is not recognized as an internal or external command,operable program or batch file. If I remove $user | Set-Mailbox from the command the error is: Missing ')' in method call. At line:1 char:80 + $user = Get-Mailbox -Identity doim1@test.ro; $user.emailaddresses.Ad d(t <<<< estdoim1@yahoo.com); Can anyone spot what am I doing wrong in the command? Thank you, Hagi.
November 11th, 2008 3:24pm

Hi Hagi, Since you already opened powershell you can simply add Exchange snap-in into it with Add-PSSnapin command. Run below commands in Windows Powershell... Code Snippet Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin $user = Get-Mailbox -Identity "doim1@intra.net.ase.ro"; $user.emailaddresses.Remove("testdoim1@yahoo.com"); $user | Set-Mailbox
Free Windows Admin Tool Kit Click here and download it now
November 11th, 2008 3:30pm

Hello Amit, Thanks for you help, I need to add the address to the user in one command that is why I am trying to use: Code Snippet C:\WINDOWS\system32\windowspowershell\v1.0\PowerShell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" $user = Get-Mailbox -Identity "doim1@intra.net.ase.ro"; $user.emailaddresses.Add("testdoim1@yahoo.com");$user | Set-Mailbox" However, I can't figure out why it is not working. TheMicrosoft.Exchange.Management.PowerShell.Admin snapin is added (if I try to add it I get an error that states it is allready added). As I said, If I first open Exchange Shell and run: Code Snippet $user = Get-Mailbox -Identity "doim1@intra.net.ase.ro"; $user.emailaddresses.Add("testdoim1@yahoo.com"); $user | Set-Mailbox everyting works. Do you have any ideeas? Thank you, Hagi.
November 11th, 2008 3:40pm

Hello, Me again. I figured out the command that works: Code Snippet C:\WINDOWS\system32\windowspowershell\v1.0\PowerShell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" "$user = Get-Mailbox -Identity "doim1@intra.net.ase.ro"; $user.emailaddresses.Add('testdoim1@yahoo.com'); $user | Set-Mailbox" Its not preety but it works. Thanks for your help, Hagi.
Free Windows Admin Tool Kit Click here and download it now
November 11th, 2008 3:52pm

Hi Hagi, Can you explain what are you trying to do? If you want to run this script from cmd.exe prompt then save Exchange cmdlet syntax in AddEmailaddress.ps1 file andrun like below. PowerShell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\Bin\ExShell.psc1" -Command ". '<Path to Your Script>'" PowerShell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\Bin\ExShell.psc1" -Command ". 'C:\My Scripts\AddEmailaddress.ps1'" Reference: Check "Running a Script from Cmd.exe" session in this article for correct syntax. Scripting with the Exchange Management Shell http://technet.microsoft.com/en-us/library/bb123798.aspx You can also add your command instead of giving path of ps1 script.
November 11th, 2008 3:54pm

Hello, I found one problem with the command I am using. I will add several aliases for a user for a user at the same time from a multithreaded application. So, for example at the same time 4 aliases might me added to a user at the same time from different threads. All the commands execute successfully, however not all aliases are added to that user. This depends from run to run, sometimes only 2 aliases are added, sometimes only 3, sometimes all 4. As I said there is no error when the commands run. I tried running 4 commands at the same time from the command prompt (with different aliases of course) and the same results unfortunately. Does anyone have an ideea of what is happening? My guess is that if Get-Mailbox of the seccond command runs before Set-Mailbox of the first command, the mailbox from the second command does not contain the first alias, and after the second command adds its alias and calls Set-Mailbox only the seccond alias is saved. Can I work around this issue? Thank you for your help, Hagi.
Free Windows Admin Tool Kit Click here and download it now
November 14th, 2008 6:45pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics