这个问题可能是因为运行的远程Exchange PowerShell脚本权限不足。解决方法是使用Exchange Management Shell(EMS)来删除中继条目,然后在ASP.NET MVC / C#中运行脚本。可通过以下的代码示例实现:
using System.Management.Automation;
using System.Management.Automation.Runspaces;
public string DeleteRelayEntry(string serverName, string connectorName)
{
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
Command cmd = new Command("Invoke-Command");
cmd.Parameters.Add("ComputerName", serverName);
cmd.Parameters.Add("ScriptBlock", ScriptBlock.Create("Remove-TransportRelayConnector -Identity \"" + connectorName + "\""));
pipeline.Commands.Add(cmd);
var output = pipeline.Invoke();
runspace.Close();
return output[0].ToString(); //返回处理结果
}
这个代码示例使用PowerShell的Invoke-Command命令来运行脚本块,从而允许我们以超级管理员身份在远程服务器上运行指定的脚本。在这里,我们使用了PowerShell的Remove-TransportRelayConnector命令来删除中继条目。最后,我们返回PowerShell的处理结果。