c# - 来自 C# 的 Exchange Managed Shell

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

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

Get-Mailbox | foreach { Get-InboxRule -Mailbox $_.Name | 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();
    }

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

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.

我知道,我在那里使用的是 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。

我做错了什么?

[第 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 Managed Shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20835691/

相关文章:

c# - WPF 中的透明边框以编程方式

c# - C#中的using语句和指令有什么区别?

.net - winforms关闭事件的不同原因

powershell - 交换powershell : get-mailbox outside default scope

c# - SmtpClient.Send随机导致SmtpException 4.7.0等待客户端输入超时

c# - 从基类继承字段的正确方法?

c# - Entity Framework 代码首先 : 1:0. .1 更改外键位置

powershell - 无法使用 SetEnvironmentVariable 设置 PATH

powershell - ConvertTo-JSON 具有单个项目的数组

java - Exchange 服务器不接受 javax.mail API 提供的用户名/密码