简体   繁体   English

如何让Powershell运行批处理文件然后保持打开状态?

[英]How do I make Powershell run a batch file and then stay open?

For example; 例如; with the old command prompt it would be: 使用旧的命令提示符,它将是:

cmd.exe /k mybatchfile.bat

Drop into a cmd instance (or indeed PowerShell itself) and type this: 放入cmd实例(或者实际上是PowerShell本身)并输入:

powershell -?

You'll see that powershell.exe has a "-noexit" parameter which tells it not to exit after executing a "startup command". 你会看到powershell.exe有一个“-noexit”参数,告诉它在执行“启动命令”后不要退出。

When running PowerShell.exe just provide the -NoExit switch like so: 运行PowerShell.exe时只需提供-NoExit开关,如下所示:

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

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

Or if you want to run a file and then run a command and have the window stay open, you can do something like this: 或者,如果要运行文件然后运行命令并使窗口保持打开状态,则可以执行以下操作:

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

The -Command parameter is implied if not provided, and here we use the & to call the PowerShell script, and the ; 如果没有提供-Command参数,这里我们使用来调用PowerShell脚本,并且; separates the PowerShell commands. 分离PowerShell命令。

Also, at the bottom of my blog post I show a quick registry change you can make in order to always have PowerShell remain open after executing a script/command, so that you don't need to always explicitly provide the -NoExit switch all the time. 此外,在我的博客文章的底部, 显示了一个快速的注册表更改,您可以在执行脚本/命令后始终保持打开PowerShell,这样您就不需要始终显式提供-NoExit开关所有时间。

I am sure that you already figure this out but I just post it 我相信你已经弄清楚了,但我发布了它

$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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM