3

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

获取邮箱 | foreach { Get-InboxRule -Mailbox $_.Name | 删除收件箱规则 }

有我的代码:

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。我尝试像这样添加管理单元:

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。

PS 我正在尝试连接到 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();中使用它 .

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

4

2 回答 2

0

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

于 2014-01-03T17:51:48.747 回答
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”

于 2014-01-30T13:24:16.653 回答