EWS Create Mailbox folder Powershell module for Alternate and Workplace365 Mailboxes

EWS Create Mailbox folder Powershell module for Alternate and Workplace365 Mailboxes It is a rollup put up for a few scripts I’ve posted previously for creating folders utilizing EWS in an Alternate OnPremise or Alternate on-line Cloud mailbox. It could possibly do the next

  • Create a Folder within the Root of the Mailbox

Create-Folder -Mailboxname [email protected] -NewFolderName check

  • Create a Folder as a SubFolder of the Inbox

Create-Folder -Mailboxname [email protected] -NewFolderName check -ParentFolder ‘Inbox’

  • Create a Folder as a SubFolder of the Inbox utilizing EWS Impersonation

Create-Folder -Mailboxname [email protected] -NewFolderName check -ParentFolder ‘Inbox’ -useImpersonation

  • Create a brand new Contacts Folder as a SubFolder of the Mailboxes Contacts Folder

Create-Folder -Mailboxname [email protected] -NewFolderName check -ParentFolder ‘Contacts’ -FolderClass IPF.Contact

  • Create a brand new Calendar Folder as a SubFolder of the Mailboxes Calendar Folder

Create-Folder -Mailboxname [email protected] -NewFolderName check -ParentFolder ‘Calendar’ -FolderClass IPF.Appointment

    The script may even detect if a folder with that title presently exists earlier than attempting to create a brand new folder.

I’ve put this module up on my github repo right here you possibly can download a replica right here

The code seems like

perform Join-Alternate{ 
param(
[Parameter(Place=0, Obligatory=$true)] [string]$MailboxName,
[Parameter(Place=1, Obligatory=$true)] [System.Management.Automation.PSCredential]$Credentials
)
Start
{
Load-EWSManagedAPI

## Set Alternate Model
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Alternate2010_SP1

## Create Alternate Service Object
$service = New-Object Microsoft.Alternate.WebServices.Knowledge.ExchangeService($ExchangeVersion)

## Set Credentials to make use of two choices are availible Option1 to make use of explict credentials or Choice 2 use the Default (logged On) credentials

#Credentials Choice 1 utilizing UPN for the home windows Account
#$psCred = Get-Credential
$creds = New-Object System.Web.NetworkCredential($Credentials.UserName.ToString(),$Credentials.GetNetworkCredential().password.ToString())
$service.Credentials = $creds
#Credentials Choice 2
#service.UseDefaultCredentials = $true
#$service.TraceEnabled = $true
## Select to disregard any SSL Warning points attributable to Self Signed Certificates

Deal with-SSL

## Set the URL of the CAS (Consumer Entry Server) to make use of two choices are availbe to make use of Autodiscover to search out the CAS URL or Hardcode the CAS to make use of

#CAS URL Choice 1 Autodiscover
$service.AutodiscoverUrl($MailboxName,{$true})
Write-host ("Using CAS Server : " + $Service.url)

#CAS URL Choice 2 Hardcoded

#$uri=[system.URI] "https://casservername/ews/exchange.asmx"
#$service.Url = $uri

## Optionally available part for Alternate Impersonation

#$service.ImpersonatedUserId = new-object Microsoft.Alternate.WebServices.Knowledge.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
if(!$service.URL){
throw "Error connecting to EWS"
}
else
{
return $service
}
}
}

perform Load-EWSManagedAPI{
param(
)
Start
{
## Load Managed API dll
###CHECK FOR EWS MANAGED API, IF PRESENT IMPORT THE HIGHEST VERSION EWS DLL, ELSE EXIT
$EWSDLL = (($(Get-ItemProperty -ErrorAction SilentlyContinue -Path Registry::$(Get-ChildItem -ErrorAction SilentlyContinue -Path 'Registry::HKEY_LOCAL_MACHINESOFTWAREMicrosoftExchangeWeb Providers'|Kind-Object Identify -Descending| Choose-Object -First 1 -ExpandProperty Identify)).'Set up Listing') + "Microsoft.Exchange.WebServices.dll")
if (Check-Path $EWSDLL)
{
Import-Module $EWSDLL
}
else
{
"$(get-date -format yyyyMMddHHmmss):"
"This script requires the EWS Managed API 1.2 or later."
"Please download and install the current version of the EWS Managed API from"
"http://go.microsoft.com/fwlink/?LinkId=255472"
""
"Exiting Script."
exit
}
}
}

perform Deal with-SSL{
param(
)
Start
{
## Code From http://poshcode.org/624
## Create a compilation surroundings
$Supplier=New-Object Microsoft.CSharp.CSharpCodeProvider
$Compiler=$Supplier.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 Native.ToolkitExtensions.Web.CertificatePolicy{
public class TrustAll : System.Web.ICertificatePolicy {
public TrustAll() {
}
public bool CheckValidationResult(System.Web.ServicePoint sp,
System.Safety.Cryptography.X509Certificates.X509Certificates cert,
System.Web.WebRequest req, int downside) {
return true;
}
}
}
'@
$TAResults=$Supplier.CompileAssemblyFromSource($Params,$TASource)
$TAAssembly=$TAResults.CompiledAssembly

## We now create an occasion of the TrustAll and fix it to the ServicePointManager
$TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
[System.Net.ServicePointManager]::CertificatePolicy=$TrustAll

## finish code from http://poshcode.org/624

}
}

perform Get-FolderFromPath{
param (
[Parameter(Place=0, Obligatory=$true)] [string]$FolderPath,
[Parameter(Place=1, Obligatory=$true)] [string]$MailboxName,
[Parameter(Place=2, Obligatory=$true)] [Microsoft.Exchange.WebServices.Data.ExchangeService]$service,
[Parameter(Place=3, Obligatory=$false)] [Microsoft.Exchange.WebServices.Data.PropertySet]$PropertySet
)
course of{
## Discover and Bind to Folder primarily based on Path
#Outline the trail to look needs to be seperated with
#Bind to the MSGFolder Root
$folderid = new-object Microsoft.Alternate.WebServices.Knowledge.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)
$tfTargetFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
#Cut up the Search path into an array
$fldArray = $FolderPath.Cut up("")
#Loop by way of the Cut up Array and do a Seek for every degree of folder
for ($lint = 1; $lint -lt $fldArray.Size; $lint++) {
#Carry out search primarily based on the displayname of every folder degree
$fvFolderView = new-object Microsoft.Alternate.WebServices.Knowledge.FolderView(1)
if(![string]::IsNullOrEmpty($PropertySet)){
$fvFolderView.PropertySet = $PropertySet
}
$SfSearchFilter = new-object Microsoft.Alternate.WebServices.Knowledge.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,$fldArray[$lint])
$findFolderResults = $service.FindFolders($tfTargetFolder.Id,$SfSearchFilter,$fvFolderView)
if ($findFolderResults.TotalCount -gt 0){
foreach($folder in $findFolderResults.Folders){
$tfTargetFolder = $folder
}
}
else{
Write-host ("Error Folder Not Found check path and try again")
$tfTargetFolder = $null
break
}
}
if($tfTargetFolder -ne $null){
return [Microsoft.Exchange.WebServices.Data.Folder]$tfTargetFolder
}
else{
throw ("Folder Not found")
}
}
}


#######################
<#
.SYNOPSIS
Creates a Folder in a Mailbox utilizing the Alternate Internet Providers API

.DESCRIPTION
Creates a Folder in a Mailbox utilizing the Alternate Internet Providers API

Requires the EWS Managed API from https://www.microsoft.com/en-us/download/particulars.aspx?id=42951

.EXAMPLE
Instance 1 To create a Folder named check within the Root of the Mailbox
Create-Folder -Mailboxname [email protected]om -NewFolderName check

Instance 2 To create a Folder as a SubFolder of the Inbox
Create-Folder -Mailboxname [email protected] -NewFolderName check -ParentFolder 'Inbox'

Instance Three To create a brand new Folder Contacts SubFolder of the Contacts Folder
Create-Folder -Mailboxname [email protected] -NewFolderName check -ParentFolder 'Contacts' -FolderClass IPF.Contact

Instance Four To create a brand new Folder utilizing EWS Impersonation
Create-Folder -Mailboxname [email protected] -NewFolderName check -ParentFolder 'Inbox' -useImpersonation

#>
########################
perform Create-Folder{
param(
[Parameter(Place=0, Obligatory=$true)] [string]$MailboxName,
[Parameter(Place=1, Obligatory=$true)] [System.Management.Automation.PSCredential]$Credentials,
[Parameter(Place=2, Obligatory=$true)] [String]$NewFolderName,
[Parameter(Place=3, Obligatory=$false)] [String]$ParentFolder,
[Parameter(Place=4, Obligatory=$false)] [String]$FolderClass,
[Parameter(Place=5, Obligatory=$false)] [switch]$useImpersonation
)
Start
{
$service = Join-Alternate -MailboxName $MailboxName -Credentials $Credentials
if($useImpersonation.IsPresent){
$service.ImpersonatedUserId = new-object Microsoft.Alternate.WebServices.Knowledge.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
}
$NewFolder = new-object Microsoft.Alternate.WebServices.Knowledge.Folder($service)
$NewFolder.DisplayName = $NewFolderName
if(([string]::IsNullOrEmpty($folderClass))){
$NewFolder.FolderClass = "IPF.Note"
}
else{
$NewFolder.FolderClass = $folderClass
}
$EWSParentFolder = $null
if(([string]::IsNullOrEmpty($ParentFolder))){
# Bind to the MsgFolderRoot folder
$folderid= new-object Microsoft.Alternate.WebServices.Knowledge.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)
$EWSParentFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
}
else{
$EWSParentFolder = Get-FolderFromPath -MailboxName $MailboxName -service $service -FolderPath $ParentFolder
}
#Outline Folder Veiw Actually solely wish to return one object
$fvFolderView = new-object Microsoft.Alternate.WebServices.Knowledge.FolderView(1)
#Outline a Search folder that's going to do a search primarily based on the DisplayName of the folder
$SfSearchFilter = new-object Microsoft.Alternate.WebServices.Knowledge.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,$NewFolderName)
#Do the Search
$findFolderResults = $service.FindFolders($EWSParentFolder.Id,$SfSearchFilter,$fvFolderView)
if ($findFolderResults.TotalCount -eq 0){
Write-host ("Folder Doesn't Exist")
$NewFolder.Save($EWSParentFolder.Id)
Write-host ("Folder Created")
}
else{
Write-error ("Folder already Exist with that Name")
}


}
}

Leave a Reply