共用方式為


Creating folder using EWS Managed API 1.1 from PowerShell

A customer of mine wanted to create a folder under Inbox for thousands of mailboxes. He also wanted to use PowerShell to do it. What better way that using Exchange Web Service(Managed API)  with Impersonation to do the job.

The list of the users are available in a text file. The First row denotes the Field Names.The file is named UserAccounts.txt. Format of the text file is as below:

 WindowsEmailAddress
akasha@contoso.com
akashb@contoso.com
akashc@contoso.com

Create a .PS1 file as below:

 # The script requires the EWS managed API, which can be downloaded here:
# https://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=c3342fb3-fbcc-4127-becf-872c746840e1
# This also requires PowerShell 2.0
# Make sure the Import-Module command below matches the DLL location of the API.
# This path must match the install location of the EWS managed API. Change it if needed.[string]$info = "White"                # Color for informational messages
[string]$warning = "Yellow"            # Color for warning messages
[string]$error = "Red"                 # Color for error messages
[string]$LogFile = "C:\Temp\Log.txt"   # Path of the Log Filefunction CreateFolder($MailboxName)
{
    Write-host "Creating Folder for Mailbox Name:" $MailboxName -foregroundcolor  $info
    Add-Content $LogFile ("Creating Folder for Mailbox Name:" + $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]::Inbox)

    Write-host "Folder Created for " $MailboxName -foregroundcolor  $warning
    Add-Content $LogFile ("Folder Created for " + $MailboxName)    $service.ImpersonatedUserId = $null
}#Change the name of the folder
$FolderName = "My Folder"Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"

$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1)# Set the Credentials
$service.Credentials = new-object Microsoft.Exchange.WebServices.Data.WebCredentials("serviceAccount","Password","Domain")# Change the URL to point to your cas server
$service.Url= new-object Uri(https://CAS-Server/EWS/Exchange.asmx)# Set $UseAutoDiscover to $true if you want to use AutoDiscover else it will use the URL set above
$UseAutoDiscover = $false#Read data from the UserAccounts.txt
import-csv UserAccounts.txt | foreach-object {
    $WindowsEmailAddress = $_.WindowsEmailAddress.ToString()

    if ($UseAutoDiscover -eq $true) {
        Write-host "Autodiscovering.." -foregroundcolor $info
        $UseAutoDiscover = $false
        $service.AutodiscoverUrl($WindowsEmailAddress)
        Write-host "Autodiscovering Done!" -foregroundcolor $info
        Write-host "EWS URL set to :" $service.Url -foregroundcolor $info

    }
    #To catch the Exceptions generated
    trap [System.Exception] 
    {
        Write-host ("Error: " + $_.Exception.Message) -foregroundcolor $error;
        Add-Content $LogFile ("Error: " + $_.Exception.Message);
        continue;
    }
    CreateFolder($WindowsEmailAddress)
}

To Run the script:
1)Open the Exchange Management Shell
2)Navigate to the location where you have stored the Data and the script file.
3)Type in .\ScriptName.PS1 and hit enter to execute the script

Enjoy!

Comments

  • Anonymous
    November 29, 2012
    HiWill this work for Exchange 2007 sp2 as well.Can you suggest any changes if required.
  • Anonymous
    December 07, 2012
    Yes, this should work on Exchange 2007 SP 2 as well. The only change would be to change the ExchangeVersion to Exchange2007_SP2$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP2)
  • Anonymous
    January 16, 2013
    If we want to create a folder in the archive mailbox for the email address do we only change the"$oFolder.Save([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)"to"$oFolder.Save([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::ArchiveMsgFolderRoot)"  ?
  • Anonymous
    January 18, 2013
    Yes that should do it. It will create a folder at root(Same level as Inbox in the MsgFolderRoot).