Exchange 2010 SP2, Windows 2008 R2. I have been playing with EWS in hopes that I can meet the goals my client would like to achieve. For newly created mailboxes, I need to create a root-level folder which contains several sub-folders. I have been able to script the creation of a SINGLE folder and I need assistance with the sub-folder creation. Here is the script I am using to create the parent folder and if you could please tell me what I need to add in order to create multiple sub-folders.
function CreateFolder($MailboxName)
{
#Change the user to Impersonate
$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$MailboxName);
#Create the folder object
$oFolder = new-object Microsoft.Exchange.WebServices.Data.Folder($service)
$oFolder.DisplayName = $FolderName
#Call Save to actually create the folder
$oFolder.Save([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::msgfolderroot)
$service.ImpersonatedUserId = $null
}
#Change the name of the folder
$FolderName = "My Custom Folders"
Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll"
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2)
# Set the Credentials
$service.Credentials = new-object Microsoft.Exchange.WebServices.Data.WebCredentials("username","password","domain")
# Use AutoDiscover
$UseAutoDiscover = $true
$a = get-mailbox "alias"
$a | foreach-object {
$WindowsEmailAddress = $_.WindowsEmailAddress.ToString()
CreateFolder($WindowsEmailAddress)
}