简体   繁体   中英

How to execute PowerShell script on a remote PC in C#

The following code executes without any errors, but there are no results returned. But when I execute it manually in PowerShell console, I get expected results.

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具有特定的命令,您可以尝试遵循本指南( https://www.howtogeek.com/117192/how-to-run-powershell-commands-on-remote-computers/ )并检查解决方案是否适合您

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM