Thursday, December 08, 2011

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'