[英]Supress PowerShell prompting “Press any key to continue…”
我正在從 a.bat 運行 powershell 腳本,以使其雙擊友好。 這是代碼:
powershell.exe -ExecutionPolicy unrestricted -NoExit -command "Start-Process powershell -Verb RunAs" {<Location of .ps1 file here>; <arguments>; pause }
它運行良好,但完成后顯示“按任意鍵繼續”,並且 powershell window 關閉,但我需要它保持打開狀態,因為在腳本結束時它顯示了一些我們需要運行以完成該過程的其他命令。
有任何想法嗎? 謝謝!
To keep a PowerShell window open (its session alive) after executing a command with -Command
(or -NoExit
), use the CLI 's -File
parameter , as already shown in your question for the outer PowerShell call.
然后,您不需要交互式延遲關閉命令,例如pause
(這是一個 function ,只需調用$null = Read-Host 'Press Enter to continue...'
)。
所以:
將-NoExit
參數添加到內部的Start-Process
-launched PowerShell 實例,或者 - 更有可能 -代替; 您需要向右滾動才能在下面的命令中看到它。
從內部命令字符串中刪除pause
命令。
注意:我假設您實際上不需要外部-NoExit
,因為它會通過輸入交互式 PowerShell session 來無限期地阻止批處理文件,因此在下面的命令中將其省略。
powershell.exe -ExecutionPolicy unrestricted -command "Start-Process powershell -Verb RunAs '-NoExit', '-Command', '...'"
請注意,將腳本塊( {... }
)與Start-Process
一起使用是沒有意義的,因為Start-Process
-Process 僅支持將字符串作為 arguments ; 因此,上面使用'...'
來表示要傳遞給內部 PowerShell 實例的命令字符串。
-NonInteractive
可能是您正在尋找的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.