繁体   English   中英

powershell 无法找到安装在同一脚本中的软件

[英]powershell not able to find software installed in the same script

我在 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 char:1 + aws s3 cp s3://mybucket/myfile.exe c:\\myfile.exe + ~~~ + CategoryInfo : ObjectNotFound: (aws:String) [], CommandNotFoundException +fullyQualifiedErrorId : CommandNotFoundException

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

在您的用户数据中,您应该能够将“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",

另一种方法是将 PowerShell cmdlet 用于 S3:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM