Problem creating Exchange 2007 Mail Enabled user using PowerShell
I'm trying to mail enable an existing user using a powershell script and I'm running into an issue when I run the PS script from a command line (cmd). I'm using another process to generate a script file called mailenable.ps1 with the contents below:Enable-MailUser -Identity 'foo foo' -Alias 'ffoo' -ExternalEmailAddress 'SMTP:foofoo@foobar.com'Set-MailUser -Identity 'foo foo' -HiddenFromAddressListsEnabled $trueIf I then run C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -noexit -command ". 'S:\IT\Scripts\Add Users\mailenable.ps1'" the user is mail enabled without any issues, but when the script tries to hide the user I get this error:Set-MailUser : foo foo is not a mail-enabled user.At S:\IT\Scripts\Add Users\mailenable.ps1:2 char:13+ Set-MailUser <<<< -Identity 'foo foo' -HiddenFromAddressListsEnabled $trueHowever, if I manually open the Exchange Management Shell then run . 'S:\IT\Scripts\Add Users\mailenable.ps1' it works just fine.Anyone know why it would work one way but not the other? This is driving me nuts, and really need to get my user account creation script working.Thanks for any thoughts you have. Alex
June 25th, 2009 10:30pm

I'm not the biggest Powershell user in the world, but I'm wondering if it is maintaining some information about the "foo foo" user BEFORE it was mail enabled. What would happen if you inserted between the Enable-MailUser and Set-MailUser a simple "Get-MailUser" command for "foo foo". Or, do something like this:Get-MailUser "foo foo" | Set-MailUser -HiddenFromAddressListsEnabled $trueJim McBee - Blog - http://mostlyexchange.blogspot.com
Free Windows Admin Tool Kit Click here and download it now
June 25th, 2009 11:37pm

Good thought. I was thinking the same type of thing too (about the powershell instance retaining info from before it was mail enabled). Unfortunatly doing a get-mailuser or a get-mailuser and piping it to set-mailuser yeilds the same results :-(However, I seem to have found a workaround that I'm OK with... I was thinking about how the script works form within Exchange Management Shell but not when I open Powershell with the exchange console file. The biggest difference is that using the latter method, I'm not running the Exchange.ps1 script that EMS runs when opened. After looking through the exchange.ps1 script and doing some testing, adding the following lines to the beginning of my script seems to have worked:$global:AdminSessionADSettings = [Microsoft.Exchange.Data.Directory.AdminSessionADSettings]::Instance$global:AdminSessionADSettings.ViewEntireForest = $falseI'm not sure why it works but it does. I created and deleted "foo foo" multiple times without issue. I even ramped my script up to creating 8 users in one pass, and all 8 were created, mail-usser enabled and hidden just the way I want.Does anyone know why adding those two lines allows my script to work? Alex
June 26th, 2009 5:47am

Tested in the lab (Exchange 2007 SP1), and herere the procedure Step 1: Create script =====================Script.PS1===================== Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin Enable-MailUser -Identity 'foo foo' -Alias 'ffoo' -ExternalEmailAddress 'SMTP:foofoo@foobar.com' Set-MailUser -Identity 'foo foo' -HiddenFromAddressListsEnabled $true =====================Script.PS1===================== Notes: the Add-PSSnapin line is used to load the EMS snap-in, as we will use Windows PowerShell which wont load Exchange snap-in automatically Step 2: Run the script Please follow the instruction in the Running Scripts Without Starting Windows PowerShellsection of Running Windows PowerShell Scripts Notes: I saw theres a blank space in the path of the script file, so we need to add & into the command link, the detailed info is in the link above Per my knowledge, the links you added later has loaded some parts of the EMS cmdlet as like the one in my script
Free Windows Admin Tool Kit Click here and download it now
June 26th, 2009 10:26am

James,Thanks for the thoughts, and although it works for you, I couldn't get it to work for me :-( I kept having the same problem.Here's the VMBScript I'm using: sub mailenableuser() dim mailenable email = attribs(5) dim fso, fil const ForWriting = 2 Set fso = CreateObject("Scripting.FileSystemObject") set fil = fso.opentextfile("mailenable.ps1", forwriting, true) fil.writeline "$global:AdminSessionADSettings = [Microsoft.Exchange.Data.Directory.AdminSessionADSettings]::Instance" fil.writeline "$global:AdminSessionADSettings.ViewEntireForest = $false" fil.writeline "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin" fil.writeline "Enable-MailUser -Identity '" & fullname & "' -Alias '" & username & "' -ExternalEmailAddress 'SMTP:" & email & "'" fil.writeline "Set-MailUser '" & fullname & "' -HiddenFromAddressListsEnabled $true" fil.close ' mailenable = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile ""C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1"" -command "". 'S:\IT\Scripts\Add Users\mailenable.ps1'""" mailenable = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit &'S:\IT\Scripts\Add Users\mailenable.ps1'" Set objShell = CreateObject("WScript.Shell") objShell.run mailenable,,True set objShell = nothing set fil = fso.getFile("mailenable.ps1") fil.delete() set fil = nothing end sub 'mailenableuser Attribs(5), Fullname, and username come from elsewhere in the code (I know It's sloppy but it works).If I set the variables and call that procedure I getthe sameerror in powershell saying that Foo Foo is not a mail-enabled user.I'm OK with living with my code (the commented lines above), but even that's not working in another script where I'm creating actual mailboxes for users. Could this be some kind of Global Catalog issue? My exchange server is a domain controller but it isn't a Global Catalog. Our other domain controller is a GC, and it's in the same site (the two boxes are on the same network and connected by a Gigabit switch).My other script uses the same procedure as above, but generates the following script: $global:AdminSessionADSettings = [Microsoft.Exchange.Data.Directory.AdminSessionADSettings]::Instance" $global:AdminSessionADSettings.ViewEntireForest = $false" Enable-Mailbox -Identity 'Foo Foo' -Alias 'FFoo' -Database 'MAILSERVER\First Storage Group\Mailbox Database <MAILSERVER>' -ManagedFolderMailboxPolicy 'Default Managed Folder Policy' -ActiveSyncMailboxPolicy 'Default' -ManagedFolderMailboxPolicyAllowed -Confirm:$False Add-MailboxPermission -Identity 'Foo Foo' -User 'CORP\Exchange Admins' -AccessRights 'FullAccess' Add-MailboxPermission -Identity 'Foo Foo' -User 'CORP\BPS' -AccessRights 'FullAccess' Add-ADPermission -Identity 'Foo Foo' -User 'CORP\Exchange Admins' -ExtendedRights 'Send-as' Set-CASMailbox -Identity 'Foo Foo' -ImapEnabled $False -PopEnabled $false Note, my method for opening Powershell automatically loads the EMS snap-ins by using the -PSConsoleFile parameter so it doesn't need to be in the script.If I add a Start-Sleep s 30 after creating the mailbox but before modifying it, I don't get any errors in the powershell window. Of course forcing a script to sleep for 30 seconds seems silly and will be a problemwhen I need to create a lot of users.Another issue I'm seeing is that using my script above only the corp\bps FullAccess shows up on the mailbox. I have to add a sleep in between each Add-MailboxPermission command. This again makes me think that Active Directory and/or exchange aren't keeping up with the speed of the powershell script.Any more thoughts?Alex
June 27th, 2009 1:47am

James,When you tested your script in your lab, was the Exchange 2007 Sp1 server also a DC and Global Catalog? I ask because in a test environment I built, I had the same issues I've been experiencing wth my production environment until I made my Exchange/AD server also a global catalog. The problem with that approach is that now the Exchange/AD server takes an extremly long time to reboot.Is there a way to not turn the Exchange server into a GC, but still create mailboxes/mail-enabled users and edit them in quick succession, i.e. using a script?Alex
Free Windows Admin Tool Kit Click here and download it now
June 29th, 2009 1:01am

Well, I suggest you to use Development Forum which would be the best place for the script question Notes: My exchange 2007 box is separated from DC/GC
June 29th, 2009 4:48am

Hi, Try to add -DomainController parameter in each of your command like $dc= "DC.Domain.local" Enable-MailUser -Identity 'foo foo' -Alias 'ffoo' -ExternalEmailAddress 'SMTP:foofoo@foobar.com' Set-MailUser -Identity 'foo foo' -HiddenFromAddressListsEnabled $true -DomainController $dc I guess it may solve ur problem. Regards, Laeeq Qazi | Snr Software Engineer(Exchange+Sharepoint+BES+DynamicsCRM) www.hostingcontroller.com
Free Windows Admin Tool Kit Click here and download it now
June 29th, 2009 12:08pm

Laeeq,Thanks for the thought, but that didn't help either.However, I did find the issue and a solution. When Exchange is running on a Domain Controller, it needs to be a Global Catalog. I had specifically NOT made this DC/Exchange server a GC because of warnings about the server taking a long time to shut down and Exchange services possibly not starting properly.So now I've made this server a Global Catalog and my scripts work just fine. No errors and no need to add in delays to wait for updates.To speed up system shutdowns/restarts I've added a shutdown script to the system that stops the Exchange services before the rest of the system shuts down. I'm going to monitor the services when I restart the system. If I see that they're not starting properly on their own, I'll look into the suggestions in this article: http://support.microsoft.com/Default.aspx?kbid=940845Alex
June 29th, 2009 7:09pm

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

Other recent topics Other recent topics