Working with Email from Powershell via Exchange Web Services

I came across one of those situations the other day where we were considering purchasing a product purely for one simple function, extracting attachments from an email and saving it to a folder.. It’s kind of like the time I brought the fanciest food process because I was too lazy to grate cheese (first world problems I know). Sure it did a great job grating cheese, but it was an absolute bugger to clean! But hey, it looks good, so worth it right….

Anyway, like my Cheese Grater analogy, the issue with buying a fancy piece of software is the cost and maintenance are overkill for a simple task. So I rolled up my sleeves and went about attacking this the powershell way. A couple of late nights neglecting my screaming kids I put together a script to do exactly what we wanted. So here is the masterpiece!

The first thing you want to do is install the Microsoft Exchange Web Services Managed API 2.2 . This will give you a library to enable all the fancy pants libraries to interface directly with your Exchange Server (or Office 365) leveraging Exchange Web Services (EWS). You only need to refer to the DLL in your script to take advantage of the all the exchange EWS goodness.

First up you will need to import the DLL as follows:

Import-Module -Name “C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll”

After this, you can start using all of the EWS objects and get down and dirty with your Exchange environment!

Next, create an ExchangeService Object, and set some variables specific to your Exchange enviornment.

Right, now that we’ve setup our environment, let’s grab an email! In the below email I’ve searched my inbox for any email containing ‘stapler’ in the subject. Then, I save any attachments found to a local folder on my computer.

Voila, thats it! Now if I look in c:\temp I can see all of the email attachments saved there.

Milton - Office Space

To make all this a bit more reusable, I’ve created a Powershell module to simplify things. Simply import the PSM1 file (the EWS DLL needs to be available) then you can perform functions similar to below:

EmailManagement.psm1 (8 Kb)

Enjoy, I’m off to eat something greasy, why do I feel like cheese all of a sudden..

Get Installed Software Remotely

Much like my 80 year old Grandmother, there is a problem I regularly run into. Fortunately in my case it doesn’t involve a car and a garage door. It’s quickly getting a list of software from a remote computer. Also in my case, running into this issue for the third time didn’t result in my driver license being revoked. But, just like my Grandmother, I thought it wise to change my approach nonetheless 🙂

Throughout my career there are times when I want to get a list of software from a computer, or number of computers. This could be due to an audit, or pure curiosity as to what that awful piece of software one of my colleagues was using. (“Rhianna’s Music Quiz”… Nice one Mitch). As always, I’m following best practice and using a non administrative account so want to also use alternative credentials when querying the remote computer.

At first glance, the best approach here I’ve determined is WMI. On initial investigation the Win32_Product class seems like the perfect fit, running it locally I get a list of software, and it seems to give me a number of useful variables. Unfortunately, it takes a long time to run.. well 30 seconds, and that’s an age for us impatient Gen Y’s. Running it remotely this can blow out to minutes. So if you want to run a command like this across multiple computers… You’ll be waiting a while, and I’ve got important things to do! (like sending my mates a Snapchat of me as a Lemur). Apparently, the issue has something to do with the Win32_Product query performing a validation on all of your software, it get’s quite technical, a zoned out reading the reasons, drooled a bit and moved on.

So enter the registry. If you query HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall you can see all of your installed software there. However, only the 64bit software. The 32bit software is buried under the Wow6432Node of the registry, and most of the subkeys have lovely GUID’s so it’s quite the job working through that manually. But, after figuring out the logic and putting a script together, it would run in seconds. A win!

The best way to query the registry in my opinion, is to use WMI as you can then specify alternate credentials. If you are on the edge of your seat reading this then treat yourself to an earlier blog covering off just this thing. It’s bloody good I tell ya…

Anyway, after some coffee, some script munging, and a lot of trial and error, I produced the below script. It will accept a ComputerName and Credentials parameters then return all 32 and 64bit software from the remote box. It also includes the UninstallString so you can easily uninstall remote software, I’ll cover that off in another post, so you can actually get rid of that annoying software from your colleagues computer…

And here is an example of it in action!