0

下面是我使用的代碼。我無法使用powershell Emailaddresses參數添加多個地址。代碼工作正常,如果我只是把一個電子郵件地址,但一旦我在下面的代碼中添加兩個地址它返回異常,指出無效的SMTP地址。如何添加多個電子郵件地址?

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(); 
runspace.Open(); 
powershell.Runspace = runspace; 

var secure = new SecureString(); 
foreach (char c in textBox5.Text) 
{ 
    secure.AppendChar(c); 
} 

PSCommand command2 = new PSCommand(); 
command2.AddCommand("Set-Mailbox"); 
command2.AddParameter("Identity", "lferrigno"); 
command2.AddParameter("EmailAddressPolicyEnabled", 0); 
command2.AddParameter("EmailAddresses", "SMTP:[email protected],[email protected]"); 

powershell.Commands = command2; 
powershell.Invoke(); 
+0

我已經能夠成功運行此代碼時,輸​​入每個電子郵件地址自己。 –

+0

雖然參數狀態我可以插入多個值 - 參數值1,值2等,它看起來像它看到它作爲一個完整的地址使其無效。 –

回答

1

這是我最終使用的代碼,因爲它是一個集合。

 string[] smtp = { "SMTP:" + textBox6.Text, 9 + "smtp:" + textBox4.Text + "@sscincorporated.com" }; 
    command2.AddParameter("EmailAddresses", smtp); 
0

-EmailAddresses參數需要一個數組參數(技術上SmtpProxyAddress對象的集合,但是這並不重要,你可以提供一個數組,並轉換將autonomatically處理),但它看起來像你'給它一個包含多個地址的單個字符串。您需要可以提供一個數組參數,其中每個元素是一個地址,或者試試這個:

command2.AddParameter("EmailAddresses","'SMTP:[email protected]','SMTP:[email protected]'"); 

儘管這仍然是你的C#代碼中的字符串,如果它傳遞給PowerShell的原樣,PowerShell的將它解釋作爲一個數組。

您應該也可以省略SMTP:前綴,因爲這是默認值,並且如果您未指定前綴將自動設置。

+0

我試過你的代碼。有人曾經提出相同的代碼,但沒有工作,所以他們刪除了他們的答案。我不得不去與一個數組 –

+0

這是我的主要觀點,你需要傳遞一個數組。我發佈的代碼只是一個替代的建議,關於如何將數組作爲字符串傳遞給PowerShell,因爲如果您在PowerShell命令行中提供逗號分隔的引用字符串列表,則會將其解釋爲數組參數。關鍵詞是「*如果*它按原樣傳遞給PowerShell」。 –

相關問題