Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中的远程PC上执行PowerShell脚本#_C#_Powershell_Powershell Remoting - Fatal编程技术网

C# 如何在C中的远程PC上执行PowerShell脚本#

C# 如何在C中的远程PC上执行PowerShell脚本#,c#,powershell,powershell-remoting,C#,Powershell,Powershell Remoting,以下代码执行时没有任何错误,但没有返回任何结果。但当我在PowerShell控制台中手动执行它时,会得到预期的结果 var ps = System.Management.Automation.PowerShell.Create(); var mySpace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(); mySpace.Open(); ps.Runspace = mySpace; using

以下代码执行时没有任何错误,但没有返回任何结果。但当我在PowerShell控制台中手动执行它时,会得到预期的结果

var ps = System.Management.Automation.PowerShell.Create();
var mySpace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();
mySpace.Open();
ps.Runspace = mySpace;

using (var sa = new Impersonation("user", "pwd"))
{
    try
    {
        sa.Impersonate();

        ps.AddScript("Get-NetAdapter -CimSession REMOTE_PC");
        var result = ps.Invoke(); // empty data
    }
    finally
    {
        sa.UndoImpersonation();
    }
}

要在远程计算机上执行命令,powershell具有特定的命令,您可以尝试遵循本指南()并检查解决方案是否适合您