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?
https://my.exchange.server/Powershell
, and I pass "schemas.microsoft.com/powershell/Microsoft.Exchange" as the ShellUri. Does Exchange expose a different endpoint that exposes the Exchange cmdlets? – Avner Shahar-Kashtan