【问题标题】:Run script with path based on environment variable使用基于环境变量的路径运行脚本
【发布时间】:2019-05-02 22:36:05
【问题描述】:

我将环境变量 ARTEMIS_HOME 设置为 c:\artemis

PS C:\artemis_brokers> $env:ARTEMIS_HOME C:\阿尔忒弥斯

在此目录下,我有一个名为 bin 的文件夹,其中包含一个 artemis.cmd 脚本。如何使用系统变量从 PowerShell 中的任何位置运行此脚本?

我尝试了以下方法但没有成功:

PS C:\artemis_brokers> $env:ARTEMIS_HOME/bin/artemis 在行:1 字符:19 + $env:ARTEMIS_HOME/bin/artemis + ~ 您必须在“/”运算符之后提供值表达式。 在行:1 字符:19 + $env:ARTEMIS_HOME/bin/artemis + ~~~~~~~~~~~ 表达式或语句中出现意外的标记“bin/artemis”。 + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException +fullyQualifiedErrorId:ExpectedValueExpression PS C:\artemis_brokers> ./$env:ARTEMIS_HOME/bin/artemis ./$env:ARTEMIS_HOME/bin/artemis : 术语 './$env:ARTEMIS_HOME/bin/artemis' 不被识别为 cmdlet、函数、脚本文件或可操作的名称 程序。检查名称的拼写,或者如果包含路径,请验证 路径正确,然后重试。 在行:1 字符:1 + ./$env:ARTEMIS_HOME/bin/artemis + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (./$env:ARTEMIS_HOME/bin/artemis:String) [], CommandNotFoundException +fullyQualifiedErrorId:CommandNotFoundException PS C:\artemis_brokers> ./$env:ARTEMIS_HOME/bin/artemis.cmd ./$env:ARTEMIS_HOME/bin/artemis.cmd :术语 './$env:ARTEMIS_HOME/bin/ artemis.cmd' 未被识别为 cmdlet、函数、脚本的名称 文件或可运行的程序。检查名称的拼写,或者如果路径 已包含,请验证路径是否正确,然后重试。 在行:1 字符:1 + ./$env:ARTEMIS_HOME/bin/artemis.cmd + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (./$env:ARTEMIS_HOME/bin/artemis.cmd:String) [], CommandNotFoundException +fullyQualifiedErrorId:CommandNotFoundException PS C:\artemis_brokers> ./"$env:ARTEMIS_HOME/bin/artemis.cmd" ./$env:ARTEMIS_HOME/bin/artemis.cmd :术语 './$env:ARTEMIS_HOME/bin/ artemis.cmd' 未被识别为 cmdlet、函数、脚本的名称 文件或可运行的程序。检查名称的拼写,或者如果路径 已包含,请验证路径是否正确,然后重试。 在行:1 字符:1 + ./"$env:ARTEMIS_HOME/bin/artemis.cmd" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (./$env:ARTEMIS_HOME/bin/artemis.cmd:String) [], CommandNotFoundException +fullyQualifiedErrorId:CommandNotFoundException PS C:\artemis_brokers> .\$env:ARTEMIS_HOME/bin/artemis .\$env:ARTEMIS_HOME/bin/artemis :术语 '.\$env:ARTEMIS_HOME/bin/artemis' 不被识别为 cmdlet、函数、脚本文件或可操作的名称 程序。检查名称的拼写,或者如果包含路径,请验证 路径正确,然后重试。 在行:1 字符:1 + .\$env:ARTEMIS_HOME/bin/artemis + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (.\$env:ARTEMIS_HOME/bin/artemis:String) [], CommandNotFoundException +fullyQualifiedErrorId:CommandNotFoundException PS C:\artemis_brokers> .\$env:ARTEMIS_HOME/bin/artemis.cmd .\$env:ARTEMIS_HOME/bin/artemis.cmd :术语 '.\$env:ARTEMIS_HOME/bin/ artemis.cmd' 未被识别为 cmdlet、函数、脚本的名称 文件或可运行的程序。检查名称的拼写,或者如果路径 已包含,请验证路径是否正确,然后重试。 在行:1 字符:1 + .\$env:ARTEMIS_HOME/bin/artemis.cmd + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (.\$env:ARTEMIS_HOME/bin/artemis.cmd:String) [], CommandNotFoundException +fullyQualifiedErrorId:CommandNotFoundException PS C:\artemis_brokers> .\"$env:ARTEMIS_HOME/bin/artemis.cmd" .\$env:ARTEMIS_HOME/bin/artemis.cmd :术语 '.\$env:ARTEMIS_HOME/bin/ artemis.cmd' 未被识别为 cmdlet、函数、脚本的名称 文件或可运行的程序。检查名称的拼写,或者如果路径 已包含,请验证路径是否正确,然后重试。 在行:1 字符:1 + .\"$env:ARTEMIS_HOME/bin/artemis.cmd" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (.\$env:ARTEMIS_HOME/bin/artemis.cmd:String) [], CommandNotFoundException +fullyQualifiedErrorId:CommandNotFoundException

【问题讨论】:

  • 只做cmd.exe /c "$env:ARTEMIS_HOME\bin\artemis.cmd"

标签: powershell syntax environment-variables


【解决方案1】:

在尝试执行之前,您需要扩展变量并将路径的其余部分连接成一个字符串。要执行生成的字符串,请使用&the call operator

& "$env:ARTEMIS_HOME/bin/artemis.cmd"

【讨论】:

    【解决方案2】:

    试试:

    & $env:ARTEMIS_HOME\bin\artemis.cmd
    

    【讨论】:

      猜你喜欢
      • 2017-09-08
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      • 2018-12-03
      • 2011-08-02
      • 2017-06-26
      • 1970-01-01
      • 2017-01-23
      相关资源
      最近更新 更多