从C#应用程序运行Powershell时PSRemotingTransportException访问被拒绝异常

时间:2019-04-05 11:36:22

标签: c# .net powershell

我有以下代码在IIS托管的服务中运行,以从AD中删除计算机。当我运行代码时,出现访问被拒绝异常。当我直接在powershell中运行命令时,它可以工作。有什么想法吗?

protected bool Run(string name, PSCredential admin)
{
     var command new Command($@"Get-ADComputer {name} | Remove-ADComputer -Confirm:$false", true)
     var connectionInfo = new WSManConnectionInfo(false, name, 5985, "/wsman", "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", admin);
     using (var runSpace = RunspaceFactory.CreateRunspace(connectionInfo))
     {
          runSpace.Open();
          var pipeline = runSpace.CreatePipeline();
          pipeline.Commands.Add(command);
          var results = pipeline.Invoke();
          return results.Count == 0;
     }
}

0 个答案:

没有答案
相关问题