这个错误是由于在托管代码中尝试调用Exchange PowerShell命令而引起的。在托管代码中,必须使用Remote PowerShell来调用Exchange PowerShell命令。以下是使用Remote PowerShell添加和删除分发组成员的示例代码:
// 创建连接 WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://myexchange/PowerShell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", new PSCredential(username, password));
// 创建Runspace和Pipeline using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo)) { runspace.Open(); using (Pipeline pipeline = runspace.CreatePipeline()) { // 添加分发组成员 Command addMember = new Command("Add-DistributionGroupMember"); addMember.Parameters.Add("Identity", "group1@contoso.com"); addMember.Parameters.Add("Member", "user1@contoso.com"); pipeline.Commands.Add(addMember); pipeline.Invoke();
// 删除分发组成员
Command removeMember = new Command("Remove-DistributionGroupMember");
removeMember.Parameters.Add("Identity", "group1@contoso.com");
removeMember.Parameters.Add("Member", "user1@contoso.com");
pipeline.Commands.Add(removeMember);
pipeline.Invoke();
}
}
注意:在使用Remote PowerShell调用Exchange PowerShell命令时,需要在系统上安装Exchange管理工具。