2008-08-18 33 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开关每时每刻。

相关问题