Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- <A brief description of the script>
- .DESCRIPTION
- <A detailed description of the script>
- .PARAMETER <paramName>
- <Description of script parameter>
- .EXAMPLE
- <An example of using the script>
- #>
- Import-Module -Name “C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll”
- ## Define UPN of the Account that has impersonation rights
- $ImpersonationAccount = "ppittman@ihep.org"
- $ImpersonationCreds = "Dolo9177"
- $username = "ppittman@ihep.org"
- $password = "Dolo9177"
- $secstr = New-Object -TypeName System.Security.SecureString
- $password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
- $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
- ## Define the impersonation Target
- $ImpersonationTarget = "kcook@ihep.org"
- $Identity = Get-Mailbox "kcook@ihep.org"
- ## AutoDiscover Redirect
- static bool RedirectionCallback(string url)
- {
- ## Return true if the URL is an HTTPS URL.
- return (url.ToLower().StartsWith("https://"));
- }
- static void GetUsersEwsUrl(
- string userEmailAddress,
- SecureString userPassword
- )
- {
- ExchangeService service = new ExchangeService();
- ## Set specific credentials.
- service.Credentials = new NetworkCredential(
- userEmailAddress,
- userPassword
- );
- ## Look up the user's EWS endpoint by using Autodiscover.
- service.AutodiscoverUrl(
- userEmailAddress,
- RedirectionCallback
- );
- Console.WriteLine("EWS Endpoint: {0}", service.Url);
- }
- $Service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1);
- $mailAddress = $Identity.PrimarySmtpAddress.ToString();
- ## Look up the user's EWS endpoint by using Autodiscover.
- $Service.AutodiscoverUrl (userEmailAddress, RedirectionCallback);
- $enumSmtpAddress = [Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress
- $Service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId($enumSmtpAddress,$mailAddress);
- $folderid = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$ImpersonationTarget);
- ## Verify the Auto Discover process is redirected to the correct url, use this version:
- ##$TestUrlCallback = {
- ## param ([string] $url)
- ## if ($url -eq "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml") {$true} else {$false}
- }
- ## $service.AutodiscoverUrl("$enumSmtpAddres", $TestUrlCallback
- ## NOTE: add UPN Suffixes
- ## https://technet.microsoft.com/en-us/library/cc772007.aspx
- ## Define the Name(s) of the new folders to create.
- $newFolderName = "Asana777"
- ## Load Exchange web services DLL
- $dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
- Import-Module $dllpath
- ## Set Exchange Version
- $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1
- ## Create Exchange Service Object
- $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
- ## $psCred = Get-Credential
- $creds = New-Object System.Net.NetworkCredential($ImpersonationAccount, $ImpersonationCreds)
- ## $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())
- $service.Credentials = $creds
- ## $service.Credentials = $creds
- ## Set the URL of the CAS (Client Access Server)
- $service.AutodiscoverUrl($ImpersonationAccount,{$true})
- ## Login to Mailbox with Impersonation
- Write-Host Using $ImpersonationAccount to Impersonate $ImpersonationTarget
- ## $service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$ImpersonationTarget);
- ## Connect to the Inbox and Create Folder(s)
- $InboxFolder = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$ImpersonationTarget)
- $InboxFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
- ## $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$InboxFolder)
- $NewFolder = New-Object Microsoft.Exchange.WebServices.Data.Folder($service)
- $NewFolder.DisplayName = $newFolderName
- ## $NewFolder.DisplayName = $NewFolderName
- ## $NewFolderName = "Asana666"
- $NewFolder.Save($InboxFolder.Id.UniqueId)
- ## Connect to the Inbox and display basic statistics
- $InboxFolder = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$ImpersonationTarget)
- $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$InboxFolder)
- Write-Host 'Total Item count for Inbox:' $Inbox.TotalCount
- Write-Host 'Total Items Unread:' $Inbox.UnreadCount
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement