【问题标题】:Call Powershell from Command Prompt Relative Path从命令提示符相对路径调用 Powershell
【发布时间】:2015-02-11 19:14:11
【问题描述】:

我正在使用以下行从命令提示符调用 PS 脚本

powershell "& 'c:/script.ps1'"

但是我需要脚本与命令提示符窗口相关。因此,如果命令提示符正在查看C:,那么该脚本实际上是

powershell "& 'script.ps1'"

有没有办法注入相对路径?

【问题讨论】:

    标签: powershell


    【解决方案1】:
    powershell "& '%cd%\script.ps1'"
    

    或者如果您打算使用在 powershell 脚本中启动批处理文件的目录,那么您可以通过将这种类型的行放在 PS 脚本的开头来使用参数...

    param([string]$Directory)
    

    然后从命令行调用填充它...

    Powershell C:\script.ps1 -Directory %cd%
    

    【讨论】:

      【解决方案2】:

      要注入相对路径,您必须在其中开始编写批处理文件

      powershell "& '%~dp0\script.ps1'" 
      

      【讨论】:

        猜你喜欢
        • 2014-08-09
        • 2015-01-04
        • 1970-01-01
        • 2019-02-05
        • 1970-01-01
        • 2017-07-02
        • 1970-01-01
        • 2013-08-29
        • 1970-01-01
        相关资源
        最近更新 更多