我試圖將別名的集合添加到Exchange服務器。這隻能通過Powershell cmdlet完成。 由於微軟在powershell下包裝並且分佈式調用只能在運行空間中完成,所以我使用System.Management.Automation實用程序來實現此目的。 命令,增加了別名,看起來像這樣的:如何使用Powershell封裝添加/刪除集合中的元素System.Management.Automation
Set-Mailbox -Identity [email protected] -EmailAddresses @{add=」[email protected]」}
哪裏設置郵箱是一個命令,所有其他字段是參數和@add表明我們的新元素添加到現有的集合。
由於Exchange運行空間在PSLanguageMode.NoLanguage模式下運行,因此只能執行Command,而不能執行腳本。通過這種方法異常上升:可以執行
Command addAliasCommand = new Command("Set-Mailbox -Identity [email protected] -EmailAddresses @{add=」[email protected]」}", true);
只有參數明確的命令:
Command addAliasCommand = new Command("Set-Mailbox", true);
addAliasCommand.Parameters.Add("identity", "[email protected]");
addAliasCommand.Parameters.Add("EmailAddresses", "[email protected], [email protected]");
但問題這種方法,它是完全重寫的別名的集合,當我想添加/刪除新的。
問題是如何添加指針@Add將顯示這些值被添加到ProxyAddressCollection的現有集合?
全碼:
System.Security.SecureString secureString = new System.Security.SecureString();
foreach (char c in Password)
secureString.AppendChar(c);
PSCredential credential = new PSCredential(AdminLogin, secureString);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://ps.outlook.com/PowerShell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
connectionInfo.SkipCACheck = true;
connectionInfo.SkipCNCheck = true;
connectionInfo.MaximumConnectionRedirectionCount = 4;
IList<string> gmResults = null;
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
runspace.Open();
using (Pipeline plPileLine = runspace.CreatePipeline())
{
try
{
Command addAliasCommand = new Command("Set-Mailbox", true);
addAliasCommand.Parameters.Add("identity", "[email protected]");
addAliasCommand.Parameters.Add("EmailAddresses", "[email protected], [email protected]");
var rsResultsresults = plPileLine.Invoke();
if (!string.IsNullOrEmpty(resultObjectName))
{
gmResults =
rsResultsresults.Select(obj => obj.Members[resultObjectName].Value.ToString()).ToList();
}
plPileLine.Stop();
}
catch (Exception e)
{
return null;
}
finally
{
runspace.Close();
runspace.Dispose();
}
}
runspace.Close();
}