![](/img/trans.png)
[英]Pass value using pipe symbol to a powershell Start-Process command, from cmd window
[英]Suppressing The Command Window Opening When Using Start-Process
我正在尝试找到一种方法来让PowerShell在使用Start-Process
运行可执行文件时不生成命令窗口。
如果我直接在脚本中调用可执行文件(例如.\\program.exe
),则程序运行(带有其参数),输出将返回到PowerShell窗口。
如果我使用Start-Process
,程序会生成一个命令窗口,程序运行并返回它的输出。
如果我尝试使用Start-Process
的-NoNewWindow
开关脚本然后错误地说它找不到exe文件。
我更喜欢使用Start-Process
来访问-Wait
开关,因为脚本制作的程序和配置可能需要一些时间才能完成,我不希望以后的命令启动。
此代码在单独的命令窗口中运行可执行文件:
Start-Process DeploymentServer.UI.CommandLine.exe -ArgumentList "download --autoDownloadOn --autoDownloadStartTime $StartTime --autoDownloadEndTime $EndTime" -Wait
此代码在PowerShell控制台中运行exe:
.\DeploymentServer.UI.CommandLine.exe download --autoDownloadOn --autoDownloadStartTime $StartTime --autoDownloadEndTime $EndTime
如果我将-NoNewWindow添加到Start-Process代码中
Start-Process DeploymentServer.UI.CommandLine.exe -ArgumentList "download --autoDownloadOn --autoDownloadStartTime $StartTime --autoDownloadEndTime $EndTime" -Wait -NoNewWindow
我收到以下错误:
Start-Process : This command cannot be executed due to the error: The system cannot find the file specifie At C:\Temp\SOLUS3Installv1.3.ps1:398 char:22 + Start-Process <<<< DeploymentServer.UI.CommandLine.exe -ArgumentList "download --autoDownloadStartTime $StartTime --autoDownloadEndTime $EndTime" -Wait -NoNewWindow + CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
使用-NoNewWindow
开关时,应该在可执行文件名前加上当前目录:
Start-Process .\DeploymentServer.UI.CommandLine.exe -ArgumentList "download --autoDownloadOn --autoDownloadStartTime $StartTime --autoDownloadEndTime $EndTime" -Wait -NoNewWindow
背景资料:
Start-Process
尝试做的第一件事是通过PowerShell规则解析-FilePath
参数的值。 如果成功,它将替换通过命令的完整路径传递的值。 如果没有,它会保持不变的价值。
在Windows API中,有两种方法可以启动新进程: CreateProcess
和ShellExecute
。 ShellExecute
是默认设置,但如果使用需要CreateProcess
的cmdlet参数(例如, -NoNewWindow
),则将使用CreateProcess
。 对于这个问题,它们之间的区别在于,当查找要执行的命令时, CreateProcess
使用当前进程的工作目录,而ShellExecute
使用指定的工作目录(默认情况下, Start-Process
基于当前文件系统传递-provider位置,除非通过-WorkingDirectory
明确指定)。
PS Test:\> 1..3 |
>> ForEach-Object {
>> New-Item -Path $_ -ItemType Directory | Out-Null
>> Add-Type -TypeDefinition @"
>> static class Test {
>> static void Main(){
>> System.Console.WriteLine($_);
>> System.Console.ReadKey(true);
>> }
>> }
>> "@ -OutputAssembly $_\Test.exe
>> }
PS Test:\> [IO.Directory]::SetCurrentDirectory((Convert-Path 2))
PS Test:\> Set-Location 1
PS Test:\1> Start-Process -FilePath Test -WorkingDirectory ..\3 -Wait # Use ShellExecute. Print 3 in new windows.
PS Test:\1> Start-Process -FilePath .\Test -WorkingDirectory ..\3 -Wait # Use ShellExecute. Print 1 in new windows.
PS Test:\1> Start-Process -FilePath Test -WorkingDirectory ..\3 -Wait -NoNewWindow # Use CreateProcess.
2
PS Test:\1> Start-Process -FilePath .\Test -WorkingDirectory ..\3 -Wait -NoNewWindow # Use CreateProcess.
1
当您更改FileSystem
提供程序的当前位置时,PowerShell不会更新当前进程的工作目录,因此目录可能不同。
键入时:
Start-Process DeploymentServer.UI.CommandLine.exe -Wait -NoNewWindow
Start-Process
无法通过PowerShell规则解析DeploymentServer.UI.CommandLine.exe
,因为默认情况下它不会查找当前的FileSystem
位置。 它使用CreateProcess
,因为你指定-NoNewWindow
开关。 因此,它最终在当前进程的工作目录中查找DeploymentServer.UI.CommandLine.exe
,该目录不包含此文件,从而导致错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.