【问题标题】:Running a PS1 script using a batch file (.bat)使用批处理文件 (.bat) 运行 PS1 脚本
【发布时间】:2011-09-30 02:35:07
【问题描述】:

目前以下是我启动 VMware vSphere PowerCLI 命令提示符的路径。我希望使用批处理文件自动运行我的sample.ps1 脚本。如何将sample.ps1 加入此路径并创建批处理文件?

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -c ". \"C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\""

【问题讨论】:

  • 你在问什么?您始终可以通过运行 powershell.exe 从 cmd 脚本调用 powershell 脚本,就像您在上面所做的那样。你还想做什么?
  • -psc = -PSConsoleFile -> 加载控制台文件以自动导入他的 SNAPIN。 -noe = -NoExit -> 为了保持 powershell 会话。然后是他要启动的脚本。

标签: powershell batch-file vmware virtual-machine powercli


【解决方案1】:

如果您使用的是 PowerShell 2.0,则可以使用 PowerShell.exe 的 -file 参数

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -file "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"

如果您使用的是 PowerShell 1.0,则可以通过这种方式使用 -command 参数

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -command "& 'C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1'"

【讨论】:

    【解决方案2】:
    echo off
    
    Title,Report Script &color 9e
    for /f "usebackq delims=$" %%a in (`cd`) do (
      set SCRIPTDIR=%%a
    )
    
    (Set ScriptFile=%SCRIPTDIR%\Report.ps1)
    
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -c ". \"C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\";%ScriptFile%"
    

    【讨论】:

      【解决方案3】:

      您可以使用它来通过 .bat 文件启动任意 .ps1 脚本,方法是像 ps1 一样调用 bat 文件。然后批量提取文件名并调用powershell。

      对于即用型解决方案,请使用以下 Gist:https://gist.github.com/JonasGroeger/10417237

      【讨论】:

        【解决方案4】:

        我在另一个页面中看到了这段代码,我在 W2012 R2 中对其进行了测试,它运行了。

        我希望它有效:

        C:\>powershell "C:\>1\file.ps1"
        

        【讨论】:

          猜你喜欢
          • 2023-04-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多