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

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 Gr...

Sending a Message in Exchange Online via REST from an Arduino MKR1000

This is part 2 of my MKR1000 article, in this previous post  I looked at sending a Message via EWS using Basic Authentication.  In this Post I'll look at using the new Outlook REST API  which requires using OAuth authentication to get an Access Token. The prerequisites for this sketch are the same as in the other post with the addition of the ArduinoJson library  https://github.com/bblanchon/ArduinoJson  which is used to parse the Authentication Results to extract the Access Token. Also the SSL certificates for the login.windows.net  and outlook.office365.com need to be uploaded to the devices using the wifi101 Firmware updater. To use Token Authentication you need to register an Application in Azure https://msdn.microsoft.com/en-us/office/office365/howto/add-common-consent-manually  with the Mail.Send permission. The application should be a Native Client app that use the Out of Band Callback urn:ietf:wg:oauth:2.0:oob. You ...

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why ...
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.