簡體   English   中英

使用啟動過程時禁止打開命令窗口

[英]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中,有兩種方法可以啟動新進程: CreateProcessShellExecute 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM