You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

远程通过C#调用PowerShell启动EXE的两类交互异常问题

远程启动Notepad的问题及解决方法

问题

用C#远程执行PowerShell命令启动Notepad.exe时遇到两个问题:

  • 任务管理器可见Notepad进程,但无窗口显示
  • 关闭Runspace后,Notepad进程随之终止

相关代码

public PowerShellOutput RunRemotePowerShell(string computerName, string script)
 {
     AppConfig config = new AppConfig();
     PSCredential creds = CreateCredential(config.UserName, config.Password);
     Runspace runspace = GetRunSpace(computerName, creds);

     try
     {
         runspace.Open();

         using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create())
         {
             ps.Runspace = runspace;
             ps.AddScript(script);

             string results = PropOutput(ps.Invoke());  
             
             var errors = ps.Streams.Error;

             // 无错误时errors为空
             StringBuilder sbError = new StringBuilder();
             if (errors != null)
             {
                 foreach (var error in errors)
                 {
                     sbError.AppendLine(error.ToString());
                 }
             }

             string strError = sbError.ToString();
             PowerShellOutput objPSOutput = new PowerShellOutput();
             objPSOutput.PowerShellResult = results;    
             objPSOutput.PowerShellError = strError;

             return objPSOutput;

         }
     }

     catch
     {
         throw;
     }
     finally
     {
         if (runspace != null)
         {
             runspace.Close();
         }
     }
 }

 static void ExecuteExamples(string Script)
 {
     Script = "Start-Process -FilePath notepad.exe";     
     Console.WriteLine(Script);
     PowerShellHelper objRP = new PowerShellHelper();
     PowerShellOutput objPSOutput = new PowerShellOutput();
     objPSOutput = objRP.RunRemotePowerShell("RemoteComputerName", Script);
     Console.WriteLine(objPSOutput.PowerShellResult);
     Console.WriteLine(objPSOutput.PowerShellError);
 }

解决方法

问题1:无窗口显示

PowerShell远程会话默认运行在非交互式的Session 0,该会话没有用户桌面环境,因此进程能运行但窗口无法渲染。要让Notepad在用户的交互式桌面中启动,可通过任务计划创建临时任务实现:

替换执行的PowerShell脚本为:

$taskName = "TempLaunchNotepad"
# 创建启动Notepad的任务动作
$action = New-ScheduledTaskAction -Execute "notepad.exe"
# 设置2秒后立即执行的触发器
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddSeconds(2)
# 配置任务允许在电池模式下启动
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
# 注册并启动任务,完成后删除临时任务
Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -Settings $settings -User $env:USERNAME -Force | Out-Null
Start-ScheduledTask -TaskName $taskName
Start-Sleep -Seconds 3
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false

问题2:关闭Runspace后进程终止

原方式中Notepad是远程会话进程的子进程,会话关闭时会被强制终止。使用任务计划启动的进程属于独立进程组,不依赖远程会话,因此Runspace关闭后Notepad会继续运行。

内容的提问来源于stack exchange,提问作者Minesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 02:42:51