我试图将别名的集合添加到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();
}