从C#交换托管shell

时间:2013-12-30 08:04:06

标签: c# powershell exchange-server exchange-management-shell

我正在尝试从客户端计算机远程运行下一个脚本:

  

Get-Mailbox | foreach {Get-InboxRule -Mailbox $ _。名称|   Remove-InboxRule}

有我的代码:

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();
    }

结果,最后一行抛出异常:

  

“Get-Mailbox”一词未被识别为cmdlet的名称,   功能,脚本文件或可操作程序。检查拼写   名称,或者如果包含路径,请验证路径是否正确   再试一次。

我知道,我在那里使用PowerShell,而不是ExchangeManagedShell。我试图像这样添加snapin:

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);

但客户端计算机上没有ExchangeManagedShell。

P.S。我正在尝试连接到Exchange2013。

我做错了什么?

[EDITION#1]

哦,我发现了,我做错了什么:

WSManConnectionInfo wsManInfo = new WSManConnectionInfo(serverUri, SHELL_URI, creds);
wsManInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();

Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();

我甚至没有连接到远程计算机。创建 wsManInfo ,但不要在 RunspaceFactory.CreateRunspace(); 中使用它。

但我仍然不知道如何解决问题...

2 个答案:

答案 0 :(得分:0)

您是否检查过您使用的凭据是否有权调用“Get-Mailbox”命令?如果没有,则必须赋予帐户通过RBAC执行命令的权限。

答案 1 :(得分:0)

解决方案很简单。只需要写这个

System.Uri serverUri = new Uri(String.Format("https://{0}/POWERSHELL/Microsoft.Exchange", serverIP));  

而不是那个

System.Uri serverUri = new Uri(String.Format("http://{0}/POWERSHELL?serializationLevel=Full", serverIP));

刚刚添加了“Microsoft.Exchange”