Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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# 运行powershell脚本时,命令提示符窗口会立即打开和关闭_C#_Powershell_Command Line_Executable_Start Process - Fatal编程技术网

C# 运行powershell脚本时,命令提示符窗口会立即打开和关闭

C# 运行powershell脚本时,命令提示符窗口会立即打开和关闭,c#,powershell,command-line,executable,start-process,C#,Powershell,Command Line,Executable,Start Process,我正在尝试运行powershell脚本,如下所示: Start-Process -FilePath "C:\svn\Services\trunk\Services.In4m.Agent.Host\bin\agent.exe" -Argument --help 任何可能发生这种情况的原因。另外,如何编写脚本以使命令提示符窗口不会立即关闭?程序似乎即将结束。如果命令仅输出帮助文本并结束,则窗口将在完成后关闭 您最好捕获命令的输出并将其显示给用户 & "C:\svn\Services\tru

我正在尝试运行powershell脚本,如下所示:

Start-Process -FilePath "C:\svn\Services\trunk\Services.In4m.Agent.Host\bin\agent.exe" -Argument --help

任何可能发生这种情况的原因。另外,如何编写脚本以使命令提示符窗口不会立即关闭?

程序似乎即将结束。如果命令仅输出帮助文本并结束,则窗口将在完成后关闭

您最好捕获命令的输出并将其显示给用户

& "C:\svn\Services\trunk\Services.In4m.Agent.Host\bin\agent.exe" --help | Write-Host
如果要打开一个单独的窗口并等待用户按enter键将其关闭,可以执行以下操作:

Start-Process 
  -FilePath "powershell.exe" 
  -ArgumentList "& 'C:\svn\Services\trunk\Services.In4m.Agent.Host\bin\agent.exe' --help; Read-Host"

这取决于您试图实现的目标。

在powershell脚本的末尾添加以下内容:
它要求用户按任意键,并等待用户按某个键

Write-Host "Press any key to continue ...".
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

之所以发生这种情况,是因为agent.exe已完成并正在关闭。你也可以试着用等待来等待,它会关闭。此PowerShell的任何其他解决方案都不支持-等待request@the_coder_in_me这绝对不是真的。这将保留powershell窗口,不是通过agent.exeOh生成的cmd窗口,那么您可以使用noexit标记。@这个问题中的编码器听起来像是您的程序。@不可纠正1这是powershell的常见问题,不是我的程序。你可以试着用谷歌搜索一下,仅供参考。我正在尝试打开agent.exe并保持由agent.exe运行触发的窗口。你在上面运行了哪个命令,导致窗口关闭过快?从命令行独立于PowerShell运行agent.exe时会发生什么情况?当我从命令行运行agent.exe时,它是否立即返回到命令提示符。。它会在cmd中弹出一个小文档,列出我可以添加的所有执行模式和命令行参数。。。当我进入文件夹并从powershell运行agent.exe时,也会执行相同的操作。。不知道当我使用外部脚本运行同一个agent.exe文件时,它会做出反应的原因