2

次のコードが実行されます。

        SecureString password = new SecureString();
        string runasUsername = "USERNAME";
        string runasPassword = "PASSWORD";

        string liveIdconnectionUri = "http://EXCHANGE_SERVER/PowerShell";

        foreach (char x in runasPassword)
        {
            password.AppendChar(x);
        }

        PSCredential credential = new PSCredential(runasUsername, password);

        // Set the connection Info
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
        credential);

        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic; //AuthenticationMechanism.Default;

        // create a runspace on a remote path
        // the returned instance must be of type RemoteRunspace

        Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);

        PowerShell powershell = PowerShell.Create();
        PSCommand command = new PSCommand();

        command.AddCommand("Enable-Mailbox");
        command.AddParameter("Identity", "first.last");
        command.AddParameter("Alias", "Fist Last");

        powershell.Commands = command;
        try
        {
            // open the remote runspace
            runspace.Open();
            // associate the runspace with powershell
            powershell.Runspace = runspace;
            // invoke the powershell to obtain the results
            var result = powershell.Invoke();
        }
        catch (Exception ex)
        {

            Console.WriteLine(ex.Message);
        }
        finally
        {
            // dispose the runspace and enable garbage collection
            runspace.Dispose();
            runspace = null;
            // Finally dispose the powershell and set all variables to null to free
            // up any resources.
            powershell.Dispose();
            powershell = null;
        }

        Console.WriteLine("done");
        Console.Read();

例外スロー:

リモート サーバーへの接続に失敗し、次のエラー メッセージが表示されました: WinRM クライアントは要求を処理できません。暗号化されていないトラフィックは、クライアント構成で現在無効になっています。クライアント構成を変更して、要求を再試行してください。詳細については、about_Remote_Troubleshooting ヘルプ トピックを参照してください。

すでに基本認証を設定しており、暗号化されていないトラフィックを許可しています。

ここで解決策を試しましたpowershell v2 remoting - How do you enable unencrypted traffic、運が悪い。

4

3 に答える 3

2

申し訳ありませんが、長い間苦労し、可能な組み合わせを変更し続け、最終的にこれで動作します:

であるAuthenticationMechanism必要がありますAuthenticationMechanism.Default(AuthenticationMechanism.Basic奇妙です)。

最終的な作業バージョンは次のとおりです。

        SecureString password = new SecureString();
        string runasUsername = "USERNAME";
        string runasPassword = "PASSWORD";

        string liveIdconnectionUri = "http://EXCHANGE_SERVER/PowerShell";

        foreach (char x in runasPassword)
        {
            password.AppendChar(x);
        }

        PSCredential credential = new PSCredential(runasUsername, password);

        // Set the connection Info
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
        credential);

        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default; //AuthenticationMechanism.Default;

        // create a runspace on a remote path
        // the returned instance must be of type RemoteRunspace

        Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);

        PowerShell powershell = PowerShell.Create();
        PSCommand command = new PSCommand();

        command.AddCommand("Enable-Mailbox");
        command.AddParameter("Identity", "MAIL_USER_ID_HERE");

        powershell.Commands = command;
        try
        {
            // open the remote runspace
            runspace.Open();
            // associate the runspace with powershell
            powershell.Runspace = runspace;
            // invoke the powershell to obtain the results
            var result = powershell.Invoke();
            if (result.Count > 0)
                Console.WriteLine("sucessful!");
            else
                Console.WriteLine("failed!");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            // dispose the runspace and enable garbage collection
            runspace.Dispose();
            runspace = null;
            // Finally dispose the powershell and set all variables to null to free
            // up any resources.
            powershell.Dispose();
            powershell = null;
        }

        Console.WriteLine("done");
        Console.Read();
于 2013-02-25T01:02:21.197 に答える
1

同じ問題がありました。また、PowerShell インスタンスをホストしている EXCHANGE_SERVER 上の仮想ディレクトリについては、IIS マネージャーでSSL 設定を "受け入れる" に構成する必要がありますが、既定の自己署名証明書があることを前提として、"SSL を要求する" には構成しないでください。サーバーにインストールされます。それに加えて、「AuthenticationMechanism.Default」設定により、次の行で発生した無数の例外が取り除かれました。

runspace.Open();

また、これをローカルで単体テストする場合は、デスクトップにExchange 管理ツールをインストールする必要があります。

...または、Windows 8 をお持ちでない場合は、次のアプローチを試してください: PowerShell Managed code in Exchange 2010 .

于 2013-09-19T19:27:43.950 に答える