Skip to main content

Using the new EWS password expiration operation in Exchange 2010 SP2 in Powershell

If it hadn't escaped your notice Exchange 2010 SP2 was released this week although from an EWS perspective there isn't that much to shout about one of the interesting new operations is the GetPasswordExpiration operation. This allows you to get the DateTime when the password for an account will expire. Currently version 1.1 of the Managed API doesn't support this operation but from the information that has been released the next version should. In the meantime you can take advantage of this new operation using some raw SOAP.

The following is a quick sample script that makes a GetPasswordExpiration request there is some SSL ignore code so it should run okay in a test environment without a valid SSL cert. To use this script you need to configure the following variables.

$MailboxName = "user@domain.com" 
      
$cred = New-Object System.Net.NetworkCredential("user@domamin.com","password")

If you don't have autodiscover configured if your trying in a dev/test enviroment you can also hardcode the CASURL in

$mbMailboxFolderURI = New-Object System.Uri("https://192.168.42.132/ews/exchange.asmx") 

I've put a download of this script here the script itself looks like


  1. $MailboxName = "user@domain.com"    
  2.          
  3. $cred = New-Object System.Net.NetworkCredential("user@domamin.com","password")     
  4.     
  5. $dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"    
  6. [void][Reflection.Assembly]::LoadFile($dllpath)     
  7. $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1)     
  8. $service.TraceEnabled = $false    
  9.     
  10. $service.Credentials = $cred    
  11. $service.autodiscoverurl($MailboxName,{$true})     
  12.   
  13. ## Code From http://poshcode.org/624  
  14. ## Create a compilation environment  
  15. $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider  
  16. $Compiler=$Provider.CreateCompiler()  
  17. $Params=New-Object System.CodeDom.Compiler.CompilerParameters  
  18. $Params.GenerateExecutable=$False  
  19. $Params.GenerateInMemory=$True  
  20. $Params.IncludeDebugInformation=$False  
  21. $Params.ReferencedAssemblies.Add("System.DLL") | Out-Null  
  22.   
  23. $TASource=@' 
  24.   namespace Local.ToolkitExtensions.Net.CertificatePolicy{ 
  25.     public class TrustAll : System.Net.ICertificatePolicy { 
  26.       public TrustAll() {  
  27.       } 
  28.       public bool CheckValidationResult(System.Net.ServicePoint sp, 
  29.         System.Security.Cryptography.X509Certificates.X509Certificate cert,  
  30.         System.Net.WebRequest req, int problem) { 
  31.         return true; 
  32.       } 
  33.     } 
  34.   } 
  35. '@   
  36. $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)  
  37. $TAAssembly=$TAResults.CompiledAssembly  
  38.   
  39. ## We now create an instance of the TrustAll and attach it to the ServicePointManager  
  40. $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")  
  41. [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll  
  42.   
  43. ## end code from http://poshcode.org/624  
  44.        
  45.       
  46.     $expRequest = @" 
  47. <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
  48. <soap:Header><RequestServerVersion Version="Exchange2010_SP2" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" /> 
  49. </soap:Header> 
  50. <soap:Body> 
  51. <GetPasswordExpirationDate xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"><MailboxSmtpAddress>$MailboxName</MailboxSmtpAddress> 
  52. </GetPasswordExpirationDate></soap:Body></soap:Envelope> 
  53. "@  
  54.         
  55. $mbMailboxFolderURI = New-Object System.Uri($service.url)    
  56.   
  57. $wrWebRequest = [System.Net.WebRequest]::Create($mbMailboxFolderURI)     
  58. $wrWebRequest.KeepAlive = $false;     
  59. $wrWebRequest.Headers.Set("Pragma""no-cache");     
  60. $wrWebRequest.Headers.Set("Translate""f");     
  61. $wrWebRequest.Headers.Set("Depth""0");     
  62. $wrWebRequest.ContentType = "text/xml";     
  63. $wrWebRequest.ContentLength = $expRequest.Length;     
  64. $wrWebRequest.Timeout = 60000;     
  65. $wrWebRequest.Method = "POST";     
  66. $wrWebRequest.Credentials = $cred    
  67. $bqByteQuery = [System.Text.Encoding]::ASCII.GetBytes($expRequest);     
  68. $wrWebRequest.ContentLength = $bqByteQuery.Length;     
  69. $rsRequestStream = $wrWebRequest.GetRequestStream();     
  70. $rsRequestStream.Write($bqByteQuery, 0, $bqByteQuery.Length);     
  71. $rsRequestStream.Close();     
  72. $wrWebResponse = $wrWebRequest.GetResponse();     
  73. $rsResponseStream = $wrWebResponse.GetResponseStream()     
  74. $sr = new-object System.IO.StreamReader($rsResponseStream);     
  75. $rdResponseDocument = New-Object System.Xml.XmlDocument     
  76. $rdResponseDocument.LoadXml($sr.ReadToEnd());     
  77. $ExpNodes = @($rdResponseDocument.getElementsByTagName("PasswordExpirationDate"))     
  78. $ExpNodes[0].'#text'  

Popular posts from this blog

Export calendar Items to a CSV file using EWS and Powershell

Somebody asked about this last week and while I have a lot of EWS scripts that do access the Calendar I didn't have a simple example that just exported a list of the Calendar events with relevant information to a CSV file so here it is. I've talked on this one before in this howto  but when you query the calendar folder using EWS you need to use a CalendarView which will expand any recurring appointments in a calendar. There are some limits when you use a calendarview in that you can only return a maximum of 2 years of appointments at a time and paging will limit the max number of items to 1000 per call. So if you have a calendar with a very large number of appointments you need to break your query into small date time blocks. In this example script I'm just grabbing the next 7 days of appointments if you want to query a longer period you need to adjust the following lines (keeping in mind what I just mentioned) #Define Date to Query $StartDate = (Get-Date) $EndDate

Downloading a shared file from Onedrive for business using Powershell

I thought I'd quickly share this script I came up with to download a file that was shared using One Drive for Business (which is SharePoint under the covers) with Powershell. The following script takes a OneDrive for business URL which would look like https://mydom-my.sharepoint.com/personal/gscales_domain_com/Documents/Email%20attachments/filename.txt This script is pretty simple it uses the SharePoint CSOM (Client side object Model) which it loads in the first line. It uses the URI object to separate the host and relative URL which the CSOM requires and also the SharePointOnlineCredentials object to handle the Office365 SharePoint online authentication. The following script is a function that take the OneDrive URL, Credentials for Office365 and path you want to download the file to and downloads the file. eg to run the script you would use something like ./spdownload.ps1 ' https://mydom-my.sharepoint.com/personal/gscales_domain_com/Documents/Email%20attachments/filena

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Graph is limited to a m
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.