我想用一個命令從java應用程式啟動powershell視窗。Cmd 被我們公司的政策阻止。
我試過了
new ProcessBuilder("powershell.exe", "start \"servicemix\" powershell -noexit -command \"dir\"").start();
但是它不會打開新視窗。我設法打開powershell視窗的唯一方法是
Desktop.getDesktop().open(new File("full/path/to/powershell"));
但我還沒有想出如何在該視窗中自動運行命令的方法。
作業系統:windows
uj5u.com熱心網友回復:
請嘗試以下操作:
new ProcessBuilder(
"powershell.exe",
"Start-Process powershell.exe '-NoExit \"[Console]::Title = ''servicemix''; Get-ChildItem\"'"
).start();
你自己嘗試嘗試使用的語法cmd.exe-internalstart命令(不能從PowerShell的通過顯式呼叫中使用除cmd.exe),而PowerShell的start是別名為它Start-Process,它的語法不同(它不支持將視窗標題,因此包含[Console]::Title = ...作為在新 PowerShell 會話中執行的命令)。
同樣,dir是 PowerShell 的Get-ChildItemcmdlet的別名。
請注意,我已經洗掉-Command了簡潔,因為它是隱含的,當你傳遞給命令的引數powershell.exe時,Windows PowerShell中CLI。還要注意的是pwsh.exe,在PowerShell的(核心)7 CLI,現在需要使用-Command(或-c,簡稱)如果傳遞的命令,因為它現在默認-File的UNIX兼容的利益。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/339027.html
