代码之家  ›  专栏  ›  技术社区  ›  João Menighin

基于环境变量使用路径运行脚本

  •  1
  • João Menighin  · 技术社区  · 7 年前

    ARTEMIS_HOME 设置为 c:\artemis .

    PS C:\artemis_brokers> $env:ARTEMIS_HOME
    C:\artemis
    

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

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

    PS C:\artemis_brokers> $env:ARTEMIS_HOME/bin/artemis
    At line:1 char:19
    + $env:ARTEMIS_HOME/bin/artemis
    +                   ~
    You must provide a value expression following the '/' operator.
    At line:1 char:19
    + $env:ARTEMIS_HOME/bin/artemis
    +                   ~~~~~~~~~~~
    Unexpected token 'bin/artemis' in expression or statement.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : ExpectedValueExpression
    
    PS C:\artemis_brokers> ./$env:ARTEMIS_HOME/bin/artemis
    ./$env:ARTEMIS_HOME/bin/artemis : The term './$env:ARTEMIS_HOME/bin/artemis'
    is not recognized as the name of a cmdlet, function, script file, or operable
    program. Check the spelling of the name, or if a path was included, verify
    that the path is correct and try again.
    At line:1 char: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 : The term './$env:ARTEMIS_HOME/bin/
    artemis.cmd' is not recognized as the name of a cmdlet, function, script
    file, or operable program. Check the spelling of the name, or if a path
    was included, verify that the path is correct and try again.
    At line:1 char: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 : The term './$env:ARTEMIS_HOME/bin/
    artemis.cmd' is not recognized as the name of a cmdlet, function, script
    file, or operable program. Check the spelling of the name, or if a path
    was included, verify that the path is correct and try again.
    At line:1 char: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 : The term '.\$env:ARTEMIS_HOME/bin/artemis'
    is not recognized as the name of a cmdlet, function, script file, or operable
    program. Check the spelling of the name, or if a path was included, verify
    that the path is correct and try again.
    At line:1 char: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 : The term '.\$env:ARTEMIS_HOME/bin/
    artemis.cmd' is not recognized as the name of a cmdlet, function, script
    file, or operable program. Check the spelling of the name, or if a path
    was included, verify that the path is correct and try again.
    At line:1 char: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 : The term '.\$env:ARTEMIS_HOME/bin/
    artemis.cmd' is not recognized as the name of a cmdlet, function, script
    file, or operable program. Check the spelling of the name, or if a path
    was included, verify that the path is correct and try again.
    At line:1 char:1
    + .\"$env:ARTEMIS_HOME/bin/artemis.cmd"
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (.\$env:ARTEMIS_HOME/bin/artemis.cmd:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException
    
    2 回复  |  直到 7 年前
        1
  •  3
  •   mklement0    7 年前

    在尝试执行之前,需要展开变量并将路径的其余部分连接到字符串中。要执行结果字符串,请使用 & , the call operator 以下内容:

    & "$env:ARTEMIS_HOME/bin/artemis.cmd"
    
        2
  •  1
  •   nicholas79171    7 年前

    尝试:

    & $env:ARTEMIS_HOME\bin\artemis.cmd