【问题标题】:How do I make Powershell run a batch file and then stay open?如何让 Powershell 运行批处理文件然后保持打开状态?
【发布时间】:2010-09-06 01:42:18
【问题描述】:

例如;使用旧的命令提示符将是:

cmd.exe /k mybatchfile.bat

【问题讨论】:

    标签: powershell batch-file


    【解决方案1】:

    放入一个 cmd 实例(或者实际上是 PowerShell 本身)并输入:

    powershell -?
    

    您会看到 powershell.exe 有一个“-noexit”参数,告诉它在执行“启动命令”后不要退出。

    【讨论】:

      【解决方案2】:

      运行 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随时切换。

      【讨论】:

        【解决方案3】:

        我相信你已经知道了,但我只是发布它

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

        【讨论】:

        • 可以 test.bat 另一个 ps1 脚本 (c:\path to a script\test.ps1) 吗?有没有关于它的完整样本?
        猜你喜欢
        • 2014-10-14
        • 2019-01-15
        • 2012-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-21
        相关资源
        最近更新 更多