Question

I'm trying to run a remote Powershell Cmdlet (one of Exchange 2010's cmdlets) from a remote machine without the Exchange cmdlets installed, using Powershell remoting (v2) in my C# code. I have something like this:

var connection = new WSManConnectionInfo("http://my.exchange.srv/PowerShell", "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credentials);
var runspace = RunspaceFactory.CreateRunspace(connection);
var ps = PowerShell.Create();
ps.Runspace = runspace;
runspace.Open();
var results = ps.AddCommand("Enable-MailPublicFolder").AddArgument(folderId).Invoke();

This returns a RemoteException: The term 'Enable-MailPublicFolder is not recognized' error. This is because the local machine isn't familiar with the Exchange Cmdlet.

However, I have no idea how to import the Exchange module into my runspace. I've found various solutions, but all are partial. Some tell me to use a RunspaceConfiguration to add the Exchange 2010 snapin, but there are no overloads for RunspaceFactory.CreateRunspace that accept a RunspaceConfiguration and also allow me to pass a WSManConnectionInfo to connect to a remote host. Another solution is to call AddScript instead of AddCommand, which seems to not throw an exception, but also didn't seem to do anything without telling me why. I would rather work relatively strongly typed (with AddCommand) rather than pass random strings in.

Is there another method that doesn't involve installing modules on the local machine? Or, barring that, how do I install the Exchange 2010 cmdlets locally?

Was it helpful?

Solution

The error your getting typically indicates that the account you're using for the remote connection is not a member of the necessary RBAC Role Group in Exchange.

When the remote session is initialized, the server will check which Roles the account making the connection has, and create a set of proxy functions in the session customized for that set of role memeberships. If the account doesn't belong to a Role Group that manages public folders, you will not have proxy functions for the public folder management cmdlets available in the session.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top