【发布时间】:2016-12-03 08:01:02
【问题描述】:
当我在 Visual Studio 2015 中调试时运行下面的代码时,它工作正常。当它部署到 IIS 时,我在第二个 ps.Invoke() 行收到以下错误:
WinRM 服务无法处理该请求。命令已存在 使用客户端指定的命令 ID。
public static PowerShellResponse AddToDistributionGroup(Credentials creds, string groupName, string memberEmail)
{
PSCredential cred = new PSCredential(creds.Username, creds.Password.ToSecureString());
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri(Settings.ExchangeServerAutomationUrl), Settings.ExchangeAutomationSchemaName, cred);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
using (PowerShell ps = PowerShell.Create())
{
runspace.Open();
ps.Runspace = runspace;
//can't pipe OU to Add-DistrubtionGroupMember b/c it blows up w/ "null reference exception" when member already exists
var group =
ps
.AddCommand("Get-DistributionGroup")
.AddParameter("Identity", groupName)
.AddParameter("OrganizationalUnit", creds.GetUserDN())
.Invoke()
.SingleOrDefault();
if (group == null)
return new PowerShellResponse() { Errors = new List<string> { "Group not found." } };
ps.AddStatement();
ps.AddCommand("Add-DistributionGroupMember")
.AddParameter("Identity", ((dynamic)group).Identity)
.AddParameter("Member", memberEmail);
ps.Invoke(); //this is where the error shows up
return ps.GetResponse();
}
}
}
我正在使用 C# 和 PowerShell 3.0 连接到 Exchange(API 文档:https://technet.microsoft.com/en-us/library/dn641234(v=exchg.160).aspx),试图将成员添加到 Exchange 中的通讯组。
PowerShellResponse 是我们的自定义类,ps.GetResponse() 是自定义函数来创建说PowerShellResponse。
【问题讨论】:
标签: c# powershell exchange-server powershell-3.0