簡體   English   中英

啟用Exchange 2010郵箱

[英]Enable exchange 2010 mailbox

我正在嘗試從C#代碼在Exchange 2010服務器上創建/啟用郵箱。 我到處看到的人都在使用下面顯示的代碼。

但是我收到以下錯誤:

術語“啟用郵箱”不被視為cmdlet,函數,腳本文件或可操作程序的名稱。 檢查名稱的拼寫,或者是否包含路徑,請驗證路徑是否正確,然后重試。

我究竟做錯了什么?

        SecureString password = new SecureString();

        string str_password = "myPassword";
        string username = "myUsername";

        //FQDN is ofcourse the (fully qualified) name of our exchange server..
        string liveIdconnectionUri = "http://FQDN/Powershell?serializationLevel=Full";

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

        PSCredential credential = new PSCredential(username, password);

        WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;

        Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
        PowerShell powershell = PowerShell.Create();
        PSCommand command = new PSCommand();

        command.AddCommand("Enable-Mailbox");
        command.AddParameter("Identity", "domainname.ltd/OUName/TestAcc Jap");
        command.AddParameter("Alias", "TestAccJap");
        command.AddParameter("Database", "DB-Name");

        powershell.Commands = command;

        try
        {
            runspace.Open();
            powershell.Runspace = runspace;
            powershell.Invoke();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            runspace.Dispose();
            runspace = null;
            powershell.Dispose();
            powershell = null;
        }

這可能是與Powershell相關的錯誤。 如果從遠程計算機(而不是交換服務器)運行代碼,則必須為有問題的用戶啟用遠程Powershell訪問,並確保防火牆允許在端口80上連接到交換服務器。服務器:

Set-User –identity username –RemotePowershellEnabled $True

用戶還必須是允許郵箱創建的交換管理角色的成員。

如果使用負載均衡器和/或具有DAG,則可能必須設置備用服務帳戶才能啟用Kerberos身份驗證。 有關詳細信息,請參見http://technet.microsoft.com/zh-cn/library/ff808313.aspx 我必須啟用它才能使代碼在我的環境中運行。 我對代碼進行了一些修改,以測試是否能夠運行exchange powershell命令。 如果成功,以下代碼將以USERIDENT用戶的全名響應。

static void Main(string[] args)
{
    SecureString password = new SecureString();
    string str_password = "PASS";
    string username = "domain\\user";

    //FQDN is ofcourse the (fully qualified) name of our exchange server.. 
    string liveIdconnectionUri = "http://SERVERFQDN/Powershell?serializationLevel=Full";
    foreach (char x in str_password)
    {
        password.AppendChar(x);
    }
    PSCredential credential = new PSCredential(username, password);
    WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
    Runspace runspace = null;
    PowerShell powershell = PowerShell.Create();
    PSCommand command = new PSCommand();
    command.AddCommand("Get-Mailbox");
    command.AddParameter("Identity", "USERIDENT");
    powershell.Commands = command;
    try
    {
        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;
        runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
        runspace.Open();
        powershell.Runspace = runspace;
        Collection<PSObject> commandResults = powershell.Invoke<PSObject>();
        foreach (PSObject result in commandResults)
        {
            Console.WriteLine(result.ToString());
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    finally
    {
        runspace.Dispose();
        runspace = null;
        powershell.Dispose();
        powershell = null;
    } 

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM