I'm trying to run next script remotely from client computer:
Get-Mailbox | foreach { Get-InboxRule -Mailbox $_.Name | Remove-InboxRule }
There is my code:
private const string SHELL_URI = "http://schemas.microsoft.com/powershell/Microsoft.Exchange";
static public void RunPowerShellScript(string scriptfile, List<CommandParameter> cmdList, string runasUsername, string runasPassword, string serverIP)
{
// Prepare the credentials that will be used when connecting
// to the server. More info on the user to use on the notes
// below this code snippet.
System.Uri serverUri = new Uri(String.Format("http://{0}/POWERSHELL?serializationLevel=Full", serverIP));
System.Security.SecureString securePassword = new System.Security.SecureString();
foreach (char c in runasPassword.ToCharArray())
{
securePassword.AppendChar(c);
}
System.Management.Automation.PSCredential creds = new System.Management.Automation.PSCredential(runasUsername, securePassword);
RunspaceConfiguration rc = RunspaceConfiguration.Create();
WSManConnectionInfo wsManInfo = new WSManConnectionInfo(serverUri, SHELL_URI, creds);
wsManInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
//Here's how you add a new script with arguments
Command myCommand = new Command(scriptfile, true);
if(cmdList != null)
{
foreach (var cmd in cmdList)
myCommand.Parameters.Add(cmd);
}
pipeline.Commands.Add(myCommand);
// Execute PowerShell script
var results = pipeline.Invoke();
}
As a result, the last line throws exception:
The term 'Get-Mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
I know, that i'm using PowerShell there, not ExchangeManagedShell. I`ve tried to add snapin like this:
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException);
Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
myRunSpace.Open(rsConfig);
but there is no ExchangeManagedShell on client computer.
P.S. I'm trying to connect to Exchange2013.
What im doing wrong?
[EDITION #1]
Oh, I've found out, what i'm doing wrong:
WSManConnectionInfo wsManInfo = new WSManConnectionInfo(serverUri, SHELL_URI, creds);
wsManInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
I even don't connect to remote computer. Creating wsManInfo, but don't use it in RunspaceFactory.CreateRunspace();.
But i still don't know how to solve the problem...
Thw solution is simple. Just needed to write this
System.Uri serverUri = new Uri(String.Format("https://{0}/POWERSHELL/Microsoft.Exchange", serverIP));
instead of that
System.Uri serverUri = new Uri(String.Format("http://{0}/POWERSHELL?serializationLevel=Full", serverIP));
Just added "Microsoft.Exchange"