的Exchange托管Shell

的Exchange托管Shell

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



有我的代码:

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

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



我知道,我在那里使用PowerShell,而不是ExchangeManagedShell。我已经尝试添加管理单元,如下所示:
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。

附言我正在尝试连接到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();

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

最佳答案

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

关于c# - 来自C#的Exchange托管Shell,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20835691/

10-10 05:06