2011-02-22 46 views
0

我編寫了一個非常小的應用程序,它訪問Exchanger Server 2010 SP1的遠程電源外殼並執行一些腳本。這裏是示例代碼。一切都在嘗試和趕上塊。執行遠程Power Shell腳本時出錯(Exchange 2010)

string insecurePassword = "mypassword"; 
    SecureString securePassword = new SecureString(); 
    foreach (char passChar in insecurePassword.ToCharArray()) 
    { 
    securePassword.AppendChar(passChar); 
    } 
    PSCredential credential = new PSCredential("mydomain\\administrator", securePassword); 

    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://exchange2010.domain.com/powershell?serializationLevel=Full"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential); 
    connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos; 
    Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo); 
    PowerShell powershell = PowerShell.Create(); 
    PSCommand command = new PSCommand(); 
    ICollection<System.Management.Automation.PSObject> results; 


//The command I want to execute is Set-MailContact with some parameters. 



    command.AddCommand("Set-MailContact"); 
    command.AddParameter("Identity", "SomeIdentityOfContact"); 
    command.AddParameter("EmailAddressPolicyEnabled", false); 
    command.AddParameter("PrimarySmtpAddress", "[email protected]"); 
    command.AddParameter("Confirm", false); 
    command.AddParameter("Force", true); 
    powershell.Commands = command; 

    // open the remote runspace 
    runspace.Open(); 
    // associate the runspace with powershell 
    powershell.Runspace = runspace; 
    // invoke the powershell to obtain the results 
    results = powershell.Invoke(); 

我想設置一個MailContact的PrimarySmtpAddress,但由於某些原因,我收到以下異常:

System.Management.Automation.RemoteException:可以在參數無法處理參數轉換「PrimarySmtpAddress」 。無法將值「SMTP:[email protected]」鍵入「Microsoft.Exchange.Data.SmtpAddress」

我認爲它必須是由於序列化/反序列化。有人對如何正確傳遞電子郵件地址的價值有任何想法嗎?

任何提示幫助將不勝感激!

回答

1

嘗試關閉SMTP:限定符。這已經隱含在-primarySMTPAddress參數中。

+0

我沒有通過任何SMTP限定符,你不能在我的代碼中看到?我擔心的是爲MailContact設置兩個電子郵件地址。一個是ExternalEmailAddress,另一個可以是任何。然而,據我所知,我們只能有一個ExternalEmailAddress,但我應該使用哪個屬性來設置額外的電子郵件地址 – 2011-02-23 08:31:29

+0

您可以看到這段代碼清楚地顯示了我沒有添加SMTP限定:~~ command.AddParameter(「PrimarySmtpAddress 「,」[email protected]「); ~~ – 2011-02-23 08:40:30

+0

剛剛意識到我正在做的錯誤。感謝您的支持;) – 2011-02-23 10:02:00

2

我認爲你是令人困惑的smtp服務器地址與電子郵件地址,嘗試通過類似smtp.yourcomapnydomain.com而不是這樣的電子郵件地址,然後再次測試。

+0

我想設置MailContact的PrimarySmtpAddress。例如,[email protected]就是這樣的地址之一嗎?如果我必須設置除ExternalEmailddress外的其他電子郵件地址,您認爲應該採用正確的格式嗎? – 2011-02-23 08:26:06