views:

286

answers:

1

Goal: Provide a web service using Visual Basic or C# or .NET that interacts with the Exchange Management Shell, sending it commands to run cmdlets, and return the results as XML. (Note that we could use any lanaguage to write the service, but since it is a Windows Box and we have Visual Studio 2008, it seemed like easiest solution would be just use it to create a VB/.NET web service. Indeed, it was quite easy to do so, just point and click.)

Problem: How to run an Exchange Management Shell cmdlet from the web service, e.g, Get-DistributionGroupMember "Live Presidents"

Seems that we should be able to create a PowerShell script that runs the cmdlet, and be able to call that from the command line, and thus just call it from within the program. Does this sound correct? If so how would I go about this? Thanks. Answer can be language agnostic, but Visual Basic would probably be best since that is what I loaded the test web service up in.

A: 

Well, didn't get an answer, but sort of figured it out. I had a problem getting a 64-bit PowerShell to run, but eventually upgraded to Exchange 2010 and used C# and then there was no longer a problem.

The short answer is that you create a new PowerShell application in Visual Studio, then you add a reference to the System.Management.Automation dll. This allows you to set up a namespace for Powershell and make calls to it. http://msdn.microsoft.com/en-us/library/system.management.automation(VS.85).aspx You create a Pipeline using the available Pipeline class http://msdn.microsoft.com/en-us/library/system.management.automation.runspaces.pipeline(VS.85).aspx in order to pipe your commands back. Then you put your commands in, add parameters if needed. Run the app and it will return the results from the cmdlets you called in the PowerShell and you can go from there.

nowarninglabel