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

powershell无法找到安装在同一脚本中的软件,powershell,amazon-web-services,amazon-s3,Powershell,Amazon Web Services,Amazon S3,我已经在AWS windows EC2堆栈创建脚本的userdata部分编写了下面的powershell脚本。我可以使用脚本安装AWSCLI,但在安装cmdlet后,脚本无法识别“aws s3 cp”命令。 当我登录到EC2实例时,我能够从powershell提示符成功运行aws s3 cp命令。 谁知道我做错了什么 { "contents":[ "<powershell> ","\n", "$awscliurl = \"https://s3.

我已经在AWS windows EC2堆栈创建脚本的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>"
    ]
}
{
“内容”:[
“”,“\n”,
“$awscliurl=\”https://s3.amazonaws.com/aws-cli/AWSCLI64.msi\“\n”,
“$awsclidownloadloc=\”C:/Windows/Temp/AWSCLI64.msi\”\n“,
“(新对象系统.Net.WebClient).下载文件($awscliurl,$awsclidownloadloc)\n”,
“$argstr=\”/ic:\\Windows\\Temp\\AWSCLI64.msi/quiet/L*v./awscli output.log\“\n”,
“启动进程msiexec.exe-等待-参数列表$argstr\n”,
“启动进程powershell.exe-重定向标准输出c:\\s3out.txt-重定向标准错误c:\\s3err.out-等待-参数列表\”aws s3 cp s3://mybucket/myfile.exe c:\\myfile.exe \“\n”,
""
]
}
当我看到s3err.out时,我看到了下面的内容

aws:术语“aws”不能识别为cmdlet、函数、脚本文件或可操作程序的名称。检查 名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。 第1行字符: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”的可执行文件。当用户数据脚本启动时,系统路径环境变量不包括AWS CLI目录,因此即使安装了该脚本,也无法找到该目录。当您登录时,您将获得包含cli的更新路径,因此,当您手动登录和测试时,它会工作

在userdata中,您应该能够用“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

我也遇到过同样的问题,在安装aws cli后添加了下面的一行,这对我很有用

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