Advertisement
Guest User

test

a guest
Dec 14th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.97 KB | None | 0 0
  1. <#
  2. .SYNOPSIS
  3.    <A brief description of the script>
  4. .DESCRIPTION
  5.    <A detailed description of the script>
  6. .PARAMETER <paramName>
  7.    <Description of script parameter>
  8. .EXAMPLE
  9.    <An example of using the script>
  10. #>
  11.  
  12. Import-Module -Name “C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll
  13.  
  14. ## Define UPN of the Account that has impersonation rights
  15. $ImpersonationAccount = "ppittman@ihep.org"
  16. $ImpersonationCreds = "Dolo9177"
  17.  
  18. $username = "ppittman@ihep.org"
  19. $password = "Dolo9177"
  20. $secstr = New-Object -TypeName System.Security.SecureString
  21. $password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
  22. $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
  23.  
  24. ## Define the impersonation Target
  25. $ImpersonationTarget = "kcook@ihep.org"
  26. $Identity = Get-Mailbox "kcook@ihep.org"
  27.  
  28. ## AutoDiscover Redirect
  29.     static bool RedirectionCallback(string url)
  30.     {
  31.     ## Return true if the URL is an HTTPS URL.
  32.     return (url.ToLower().StartsWith("https://"));
  33.     }
  34.  
  35.     static void GetUsersEwsUrl(
  36.         string userEmailAddress,
  37.         SecureString userPassword
  38.         )
  39.     {
  40.     ExchangeService service = new ExchangeService();
  41.  
  42.     ## Set specific credentials.
  43.     service.Credentials = new NetworkCredential(
  44.         userEmailAddress,
  45.         userPassword
  46.         );
  47.  
  48.     ## Look up the user's EWS endpoint by using Autodiscover.
  49.     service.AutodiscoverUrl(
  50.         userEmailAddress,
  51.         RedirectionCallback
  52.         );
  53.  
  54.     Console.WriteLine("EWS Endpoint: {0}", service.Url);
  55.     }
  56.  
  57. $Service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1);            
  58. $mailAddress = $Identity.PrimarySmtpAddress.ToString();            
  59.  
  60. ## Look up the user's EWS endpoint by using Autodiscover.
  61. $Service.AutodiscoverUrl (userEmailAddress, RedirectionCallback);
  62.  
  63. $enumSmtpAddress = [Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress            
  64. $Service.ImpersonatedUserId =  New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId($enumSmtpAddress,$mailAddress);
  65. $folderid = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$ImpersonationTarget);
  66.  
  67. ## Verify the Auto Discover process is redirected to the correct url, use this version:
  68.  ##$TestUrlCallback = {
  69.  ## param ([string] $url)
  70.  ## if ($url -eq "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml") {$true} else {$false}
  71. }
  72.  ## $service.AutodiscoverUrl("$enumSmtpAddres", $TestUrlCallback
  73.  
  74. ## NOTE:  add UPN Suffixes
  75. ## https://technet.microsoft.com/en-us/library/cc772007.aspx
  76.  
  77. ## Define the Name(s) of the new folders to create.
  78. $newFolderName  = "Asana777"
  79.  
  80. ## Load Exchange web services DLL
  81. $dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
  82. Import-Module $dllpath
  83.  
  84. ## Set Exchange Version
  85. $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1
  86.  
  87. ## Create Exchange Service Object
  88. $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
  89.  
  90. ## $psCred = Get-Credential
  91. $creds = New-Object System.Net.NetworkCredential($ImpersonationAccount, $ImpersonationCreds)
  92. ## $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())
  93. $service.Credentials = $creds
  94. ## $service.Credentials = $creds
  95.  
  96. ## Set the URL of the CAS (Client Access Server)
  97. $service.AutodiscoverUrl($ImpersonationAccount,{$true})
  98.  
  99. ## Login to Mailbox with Impersonation
  100. Write-Host Using $ImpersonationAccount to Impersonate $ImpersonationTarget
  101.     ## $service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$ImpersonationTarget);
  102.  
  103. ## Connect to the Inbox and Create Folder(s)
  104. $InboxFolder = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$ImpersonationTarget)
  105. $InboxFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
  106. ## $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$InboxFolder)
  107. $NewFolder = New-Object Microsoft.Exchange.WebServices.Data.Folder($service)
  108. $NewFolder.DisplayName = $newFolderName
  109. ## $NewFolder.DisplayName = $NewFolderName
  110. ## $NewFolderName  = "Asana666"
  111. $NewFolder.Save($InboxFolder.Id.UniqueId)
  112.  
  113.  
  114. ## Connect to the Inbox and display basic statistics
  115. $InboxFolder = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$ImpersonationTarget)
  116. $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$InboxFolder)
  117. Write-Host 'Total Item count for Inbox:' $Inbox.TotalCount
  118. Write-Host 'Total Items Unread:' $Inbox.UnreadCount
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement