【问题标题】:powershell not able to find software installed in the same scriptpowershell 找不到安装在同一脚本中的软件
【发布时间】:2018-03-09 11:21:10
【问题描述】:

我在 AWS windows EC2 堆栈创建 cloudformation 脚本的 userdata 部分编写了以下 powershell 脚本。我可以使用脚本安装 AWSCLI,但安装 cmdlet 后脚本无法识别“aws s3 cp”命令。 当我登录到 EC2 实例时,我能够从 powershell 提示符成功运行 aws s3 cp 命令。 谁能告诉我做错了什么。

{
    "contents":[
        "<powershell> ","\n",
        "$awscliurl = \"https://s3.amazonaws.com/aws-cli/AWSCLI64.msi\"  \n",
        "$awsclidownloadloc = \"C:/Windows/Temp/AWSCLI64.msi\"  \n",
        "(New-Object System.Net.WebClient).DownloadFile($awscliurl, $awsclidownloadloc)   \n",
        "$argstr =  \"/I  C:\\Windows\\Temp\\AWSCLI64.msi  /quiet /L*v   ./awscli-output.log\"  \n",
        "Start-Process msiexec.exe -Wait -ArgumentList $argstr   \n",
        "Start-Process powershell.exe  -RedirectStandardOutput  c:\\s3out.txt  -RedirectStandardError c:\\s3err.out  -Wait -ArgumentList \"aws s3 cp s3://mybucket/myfile.exe  c:\\myfile.exe\"  \n",
        "</powershell>"
    ]
}

当我查看 s3err.out 时,我看到以下内容

aws :术语“aws”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查 名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。 在行:1 字符:1 + aws s3 cp s3://mybucket/myfile.exe c:\myfile.exe + ~~~ + CategoryInfo : ObjectNotFound: (aws:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

【问题讨论】:

    标签: powershell amazon-web-services amazon-s3


    【解决方案1】:

    “AWS”是一个可执行文件,位于“C:\Program Files\Amazon\AWSCLI\aws.exe”。系统 PATH 环境变量不包括用户数据脚本启动时的 AWS CLI 目录,因此即使安装后也找不到它。当您登录时,您将获得包含 cli 的更新路径,因此当您手动登录和测试时它可以工作。

    在您的用户数据中,您应该能够将“aws”替换为绝对路径,但您需要确保其正确转义。

    以下内容未经测试,但我认为应该可以。使用 PowerShell ` 转义字符,您可以在双引号字符串中包含双引号...

    -ArgumentList "`"C:\Program Files\Amazon\AWSCLI\aws.exe`" cp ..."
    

    为了使这个 JSON 友好,我们需要进一步转义双引号,这样整行就会变成:

    "Start-Process powershell.exe  -RedirectStandardOutput  c:\\s3out.txt  -RedirectStandardError c:\\s3err.out  -Wait -ArgumentList \"`\"C:\Program Files\Amazon\AWSCLI\aws.exe`\" s3 cp s3://mybucket/myfile.exe  c:\\myfile.exe\"  \n",
    

    另一种方法是使用 S3 的 PowerShell cmdlet:

    Read-S3Object -BucketName mybucket -Key myfile.exe -File c:\\myfile.exe
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,在安装 aws cli 后添加以下行,对我有用。

      $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") 
      

      【讨论】:

        猜你喜欢
        • 2018-01-27
        • 2021-10-12
        • 1970-01-01
        • 2017-05-26
        • 1970-01-01
        • 2021-10-13
        • 2018-06-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多