Exchange Web Services can be used to do lots of stuffs in the mailbox like access email, reply the email, access calender contacts etc. EWS has made live easier for us. EWS was introduced from Exchange 2007 and earlier lots of legacy application used to use Webdev to do the same. Webdev does not provide the great flexibility to the programmers. EWS API provides a easy access and lot more flexibility which webdev did not provide. Even as a Administrator we can use EWS with Powershell to do lots of stuffs on the mailbox.
Below is the piece of code from Glen Scales(http://gsexdev.blogspot.com), who knows in and out of Exchange EWS and Powershell. I have modified it for a requirement. Lets have a requirement to get the list of all the yahoo contacts on all the mailbox in the Exchange organization in the spread Sheet.
We need to make sure we are using a account which has full access on all the mailbox in the organization. To pull of the contacts the EWS has to get into the mailbox and get the list.
Required configuration
1. Exchange 2007 Management Shell
2. .net 3.5
3. Exchange CAS -EWS Urls
4. Account which has access on all the mailbox in the organization
5. Below code can be executed in Lab. This is because normally lab will normally have only self signed certificate not a trusted SAN certificate installed on the CAS server and without certificate EWS may not work. To mitigate the issue we have added few lines of code between 8 -36. In production you can remove this code and execute as you may have SAN certificate installed.
Start-Transcript c:\Transcript.txt$casserverName = "CASServer"$userName = "username"$password = "password"$domain = "domainname"## Code From <a href="http://poshcode.org/624">http://poshcode.org/624</a>## Create a compilation environment$Provider=New-Object Microsoft.CSharp.CSharpCodeProvider$Compiler=$Provider.CreateCompiler()$Params=New-Object System.CodeDom.Compiler.CompilerParameters$Params.GenerateExecutable=$False$Params.GenerateInMemory=$True$Params.IncludeDebugInformation=$False$Params.ReferencedAssemblies.Add("System.DLL") | Out-Null$TASource=@' namespace Local.ToolkitExtensions.Net.CertificatePolicy{ public class TrustAll : System.Net.ICertificatePolicy { public TrustAll() { } public bool CheckValidationResult(System.Net.ServicePoint sp, System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Net.WebRequest req, int problem) { return true; } } }'@$TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)$TAAssembly=$TAResults.CompiledAssembly## We now create an instance of the TrustAll and attach it to the ServicePointManager$TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")[System.Net.ServicePointManager]::CertificatePolicy=$TrustAll$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.0\Microsoft.Exchange.WebServices.dll"[void][Reflection.Assembly]::LoadFile($dllpath)$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()$sidbind = "LDAP://<SID=" + $windowsIdentity.user.Value.ToString() + ">"$aceuser = [ADSI]$sidbind$uri=[system.URI] ("<a href="https://%22/">https://"</a> + $casserverName + "/ews/exchange.asmx")$service.Url = $uri$service.Credentials = New-Object System.Net.NetworkCredential($username,$password,$domain)new-item -path c:\reportfile.csv -type file -forceget-mailbox -resultsize unlimited | Select PrimarySmtpAddress | foreach{$ContactsFolderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Contacts,$_) $view = New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000) $findResults = $Service.FindItems($ContactsFolderid,$view) if($findResults) { foreach($contact in $findResults) { if($contact.EmailAddresses[0].address -like "*yahoo.com") { $result = $_ + " , " + $contact.EmailAddresses[0].address } Add-content -path c:\reportfile.csv -value $result }}}Stop-Transcript
result file will be created with name C:\Reportfile.csv. It will have the list of users primarysmtpaddress and there list of contacts which has yahoo.com in it
Every one will have there own requirement and you can modify to suite your requirement. let me know if you have any requirement and i can help you on this.
Thanks Glen 🙂