The ability to add a shared Mailbox Folders was a feature that was introduced in Exchange 2010 and carried over into 2013. When an additional Mailbox folder is added this updates an XML configuration documented in the OWA.OtherMailbox FAI Item for this Mailbox.
In Exchange 2010 this includes an Id to the Folder but in Exchange2013/OWA it just includes the PrimarySMTP of the Mailbox your adding while the folderId is left blank. Due to this reason this script won't work on a Exchange 2010 server.
Note : Its important to point out even on 2013/Office365 this type of script that modifies the configuration item would also not be supported as it may inadvertently corrupt the config document so it's provided as is for testing only.
The script to do this looks like
In Exchange 2010 this includes an Id to the Folder but in Exchange2013/OWA it just includes the PrimarySMTP of the Mailbox your adding while the folderId is left blank. Due to this reason this script won't work on a Exchange 2010 server.
Note : Its important to point out even on 2013/Office365 this type of script that modifies the configuration item would also not be supported as it may inadvertently corrupt the config document so it's provided as is for testing only.
The script to do this looks like
- ## Get the Mailbox to Access from the 1st commandline argument
- $MailboxName = $args[0]
- $maMailboxToAdd = $args[1]
- ## Load Managed API dll
- Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
- ## Set Exchange Version
- $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013
- ## Create Exchange Service Object
- $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
- ## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials
- #Credentials Option 1 using UPN for the windows Account
- $psCred = Get-Credential
- $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())
- $service.Credentials = $creds
- #Credentials Option 2
- #service.UseDefaultCredentials = $true
- ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates
- ## Code From http://poshcode.org/624
- ## Create a compilation environment
- $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
- $Compiler=$Provider.CreateCompiler()
- $Params=New-Object System.CodeDom.Compiler.CompilerParameters
- $Params.GenerateExecutable=$False
- $Params.GenerateInMemory=$True
- $Params.IncludeDebugInformation=$False
- $Params.ReferencedAssemblies.Add("System.DLL") | Out-Null
- $TASource=@'
- namespace Local.ToolkitExtensions.Net.CertificatePolicy{
- public class TrustAll : System.Net.ICertificatePolicy {
- public TrustAll() {
- }
- public bool CheckValidationResult(System.Net.ServicePoint sp,
- System.Security.Cryptography.X509Certificates.X509Certificate cert,
- System.Net.WebRequest req, int problem) {
- return true;
- }
- }
- }
- '@
- $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
- $TAAssembly=$TAResults.CompiledAssembly
- ## We now create an instance of the TrustAll and attach it to the ServicePointManager
- $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
- [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
- ## end code from http://poshcode.org/624
- ## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use
- #CAS URL Option 1 Autodiscover
- $service.AutodiscoverUrl($MailboxName,{$true})
- "Using CAS Server : " + $Service.url
- #CAS URL Option 2 Hardcoded
- #$uri=[system.URI] "https://casservername/ews/exchange.asmx"
- #$service.Url = $uri
- ## Optional section for Exchange Impersonation
- #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
- $exists = $false;
- $updated = $false;
- # Bind to the MsgFolderRoot folder
- $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root,$MailboxName)
- $Root = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
- #Check for existing Item
- $SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.ItemSchema]::ItemClass,"IPM.Configuration.OWA.OtherMailbox")
- $ivItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(1)
- $ivItemView.Traversal = [Microsoft.Exchange.WebServices.Data.ItemTraversal]::Associated
- $fiResults = $service.FindItems($Root.Id,$SfSearchFilter,$ivItemView)
- if($fiResults.Items.Count -eq 0){
- Write-Host ("No Config Item found, create new Item")
- $UMConfig = New-Object Microsoft.Exchange.WebServices.Data.UserConfiguration -ArgumentList $service
- $UMConfig.Save("OWA.OtherMailbox",$Root.Id)
- }
- else{
- Write-Host ("Existing Config Item Found");
- }
- $owaOtherMailbox = [Microsoft.Exchange.WebServices.Data.UserConfiguration]::Bind($service, "OWA.OtherMailbox", $Root.Id, [Microsoft.Exchange.WebServices.Data.UserConfigurationProperties]::All);
- #Make sure the server is running 2013 this script doesn't work on Exchange 2010
- if($service.service.ServerInfo.MajorVersion -ge 15){
- $xmlConfig = New-Object System.Xml.XmlDocument
- if ($owaOtherMailbox.XmlData -eq $null)
- {
- $xmlConfig.LoadXml("<OtherMailbox></OtherMailbox>");
- }
- else
- {
- $xmlConfig.LoadXml([System.Text.UTF8Encoding]::UTF8.GetString($owaOtherMailbox.XmlData));
- }
- if($xmlConfig.OtherMailbox.entry -ne $null){
- foreach($obMailbox in $xmlConfig.OtherMailbox.entry){
- Write-host ("Processing Mailbox : " + $obMailbox.principalSMTPAddress);
- if($obMailbox.principalSMTPAddress -eq $maMailboxToAdd.ToLower()){
- $exists = $true;
- }
- }
- }
- if (!$exists)
- {
- $ncCol = $service.ResolveName($maMailboxToAdd,[Microsoft.Exchange.WebServices.Data.ResolveNameSearchLocation]::DirectoryOnly,$true);
- if ($ncCol.Count -gt 0)
- {
- $newmb = $xmlConfig.CreateElement("entry");
- $newmb.SetAttribute("displayName", $ncCol[0].Contact.DisplayName);
- $newmb.SetAttribute("rootFolderId", "");
- $newmb.SetAttribute("principalSMTPAddress", $ncCol[0].Mailbox.Address);
- $newmb.InnerText = "";
- $xmlConfig.DocumentElement.AppendChild($newmb);
- $updated = $true;
- }
- }
- else
- {
- write-host ("Mailbox already added");
- }
- if ($updated) {
- $xmlbytes = [System.Text.UTF8Encoding]::UTF8.GetBytes($xmlConfig.OuterXml);
- $owaOtherMailbox.XmlData = $xmlbytes;
- $owaOtherMailbox.Update();
- write-host ("Config updated")
- }
- }