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 = "" 
$cred = New-Object System.Net.NetworkCredential("","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("") 

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

  1. $MailboxName = ""    
  3. $cred = New-Object System.Net.NetworkCredential("","password")     
  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    
  10. $service.Credentials = $cred    
  11. $service.autodiscoverurl($MailboxName,{$true})     
  13. ## Code From  
  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  
  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  
  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  
  43. ## end code from  
  46.     $expRequest = @" 
  47. <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="" xmlns:xsi="" xmlns:xsd=""> 
  48. <soap:Header><RequestServerVersion Version="Exchange2010_SP2" xmlns="" /> 
  49. </soap:Header> 
  50. <soap:Body> 
  51. <GetPasswordExpirationDate xmlns=""><MailboxSmtpAddress>$MailboxName</MailboxSmtpAddress> 
  52. </GetPasswordExpirationDate></soap:Body></soap:Envelope> 
  53. "@  
  55. $mbMailboxFolderURI = New-Object System.Uri($service.url)    
  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

Using the MSAL (Microsoft Authentication Library) in EWS with Office365

Last July Microsoft announced here they would be disabling basic authentication in EWS on October 13 2020 which is now a little over a year away. Given the amount of time that has passed since the announcement any line of business applications or third party applications that you use that had been using Basic authentication should have been modified or upgraded to support using oAuth. If this isn't the case the time to take action is now. When you need to migrate a .NET app or script you have using EWS and basic Authentication you have two Authentication libraries you can choose from ADAL - Azure AD Authentication Library (uses the v1 Azure AD Endpoint) MSAL - Microsoft Authentication Library (uses the v2 Microsoft Identity Platform Endpoint) the most common library you will come across in use is the ADAL libraries because its been around the longest, has good support across a number of languages and allows complex authentications scenarios with support for SAML etc. The

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 .  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 I wrote th

A walk-though using the Graph API Mailbox reports in Powershell

Quite recently the Reporting side of the Graph API has moved in GA from beta, there are quite a number of reports that can be run across various Office365 surfaces but in this post I'm going to focus on the Mailbox related ones. Accessing Office365 Reports using Powershell is nothing new and has been available in the previous reporting endpoint however from the end of January many of these cmdlets are now being depreciated in favour of the Graph API . Prerequisites  In comparison to using the Remote PowerShell cmdlets where only the correct Office365 Admin permissions where needed, to use the new Graph API reports endpoint you need to use OAuth for authentication so this requires an Application Registration  that is then given the correct oAuth Grants to use the Reports EndPoin
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.