1

C# プログラム内でトランスポート エージェントを Exchange Server にインストールすることはできますか?

通常、Agent .dll を作成してから、Exchange 管理シェルを開いて次のコマンドを実行する必要があります。

Install-TransportAgent -Name "Agent Name" -TransportAgentFactory "Factory.Class.Name" -AssemblyPath "C:\Path\to\agent.dll"

enable-transportagent -Identity "Agent Name"

優先度の設定:

Set-TransportAgent -Identity "Agent Name" -Priority 3

C# アプリケーション内から (PowerShell コマンドを呼び出すか、.NET Framework を直接使用して) トランスポート エージェントをインストールするにはどうすればよいですか?

4

3 に答える 3

2

C# から直接 PowerShell を使用し、対応するコマンドレットを呼び出すソリューションを見つけました。

: 完全なコードは、https: //github.com/Pro/dkim-exchange/blob/master/Src/Configuration.DkimSigner/Exchange/ExchangeServer.cs#L111およびhttps://github.com/Proで入手できます。 /dkim-exchange/blob/master/Src/Configuration.DkimSigner/Exchange/PowershellHelper.cs#L29

/// <summary>
/// Installs the transport agent by calling the corresponding PowerShell commands (Install-TransportAgent and Enable-TransportAgent).
/// The priority of the agent is set to the highest one.
/// You need to restart the MSExchangeTransport service after install.
///
/// Throws ExchangeHelperException on error.
/// </summary>
public static void installTransportAgent()
{
    using (Runspace runspace = RunspaceFactory.CreateRunspace(getPSConnectionInfo()))
    {

        runspace.Open();
        using (PowerShell powershell = PowerShell.Create())
        {
            powershell.Runspace = runspace;

            int currPriority = 0;
            Collection<PSObject> results;

            if (!isAgentInstalled())
            {
                // Install-TransportAgent -Name "Exchange DkimSigner" -TransportAgentFactory "Exchange.DkimSigner.DkimSigningRoutingAgentFactory" -AssemblyPath "$EXDIR\ExchangeDkimSigner.dll"
                powershell.AddCommand("Install-TransportAgent");
                powershell.AddParameter("Name", AGENT_NAME);
                powershell.AddParameter("TransportAgentFactory", "Exchange.DkimSigner.DkimSigningRoutingAgentFactory");
                powershell.AddParameter("AssemblyPath", System.IO.Path.Combine(AGENT_DIR, "ExchangeDkimSigner.dll"));

                results = invokePS(powershell, "Error installing Transport Agent");

                if (results.Count == 1)
                {
                    currPriority = Int32.Parse(results[0].Properties["Priority"].Value.ToString());
                }

                powershell.Commands.Clear();

                // Enable-TransportAgent -Identity "Exchange DkimSigner"
                powershell.AddCommand("Enable-TransportAgent");
                powershell.AddParameter("Identity", AGENT_NAME);

                invokePS(powershell, "Error enabling Transport Agent");
            }

            powershell.Commands.Clear();

            // Determine current maximum priority
            powershell.AddCommand("Get-TransportAgent");

            results = invokePS(powershell, "Error getting list of Transport Agents");

            int maxPrio = 0;
            foreach (PSObject result in results)
            {

                if (!result.Properties["Identity"].Value.ToString().Equals(AGENT_NAME)){
                    maxPrio = Math.Max(maxPrio, Int32.Parse(result.Properties["Priority"].Value.ToString()));
                }
            }

            powershell.Commands.Clear();

            if (currPriority != maxPrio + 1)
            {
                //Set-TransportAgent -Identity "Exchange DkimSigner" -Priority 3
                powershell.AddCommand("Set-TransportAgent");
                powershell.AddParameter("Identity", AGENT_NAME);
                powershell.AddParameter("Priority", maxPrio + 1);
                results = invokePS(powershell, "Error setting priority of Transport Agent");
            }
        }
    }

}

/// <summary>
/// Checks if the last powerShell command failed with errors.
/// If yes, this method will throw an ExchangeHelperException to notify the callee.
/// </summary>
/// <param name="powerShell">PowerShell to check for errors</param>
/// <param name="errorPrependMessage">String prepended to the exception message</param>
private static Collection<PSObject> invokePS(PowerShell powerShell, string errorPrependMessage)
{
    Collection<PSObject> results = null;
    try
    {
        results = powerShell.Invoke();
    }
    catch (System.Management.Automation.RemoteException e)
    {
        if (errorPrependMessage.Length > 0)
            throw new ExchangeHelperException("Error getting list of Transport Agents:\n" + e.Message, e);
        else
            throw e;
    }
    if (powerShell.Streams.Error.Count > 0)
    {
        string errors = errorPrependMessage;
        if (errorPrependMessage.Length > 0 && !errorPrependMessage.EndsWith(":"))
            errors += ":";

        foreach (ErrorRecord error in powerShell.Streams.Error)
        {
            if (errors.Length > 0)
                errors += "\n";
            errors += error.ToString();
        }
        throw new ExchangeHelperException(errors);
    }
    return results;
}
于 2014-06-12T12:49:32.917 に答える
0

はい、C# を使用して Exchange トランスポート エージェントをインストール\アンインストールできます。実際、私はそれをやったことがあります。

私がしたことは、PowerShell を使用して Exchange コマンドレットを呼び出し、エージェントを Exchange ハブ サーバーにインストールしたことです。また、C# を使用して MS Exchange Transport Agent サービスを停止/開始する必要がありました。さらに、エージェントファイルを配置するフォルダーを作成し、そのフォルダーに対するネットワークサービスの読み取り/書き込みアクセス許可も付与する必要がありました。

よろしく、 レーク・カジ

于 2014-06-11T20:37:08.817 に答える