2008-08-18 28 views

回答

24

拖放到CMD實例(或事實上PowerShell的本身),並輸入:

powershell -? 

你會看到powershell.exe有一個「 - noexit「參數,告訴它在執行」啓動命令「後不要退出。

-1

我相信你已經算出來,但我只是將它張貼

$CreateDate = (Get-Date -format 'yyyy-MM-dd hh-mm-ss') 

$RemoteServerName ="server name" 
$process = [WMICLASS]"\\$RemoteServerName\ROOT\CIMV2:win32_process" 
$result = $process.Create("C:\path to a script\test.bat") 
$result | out-file -file "C:\some path \Log-$CreatedDate.txt" 
+0

可以test.bat另一個ps1腳本(c:\腳本路徑\ test.ps1)?任何關於它的完整樣本? – Kiquenet 2012-05-30 10:38:35

4

當運行PowerShell.exe只是提供-noexit開關,像這樣:

PowerShell -NoExit -File "C:\SomeFolder\SomePowerShellScript.ps1" 

PowerShell -NoExit -Command "Write-Host 'This window will stay open.'" 

或者,如果你想運行一個文件,然後運行一個命令並保持窗口打開,你可以這樣做:

PowerShell -NoExit "& 'C:\SomeFolder\SomePowerShellScript.ps1'; Write-Host 'This window will stay open.'" 

如果未提供-Command參數是隱含的,在這裏我們使用&調用PowerShell腳本,和;分隔PowerShell命令。

另外,在my blog post的底部,我顯示了一個快速的註冊表更改,您可以在執行腳本/命令後始終保持打開PowerShell,以便您不必始終明確提供-NoExit開關每時每刻。

相關問題