PowerShell 无法识别安装在同一脚本中的 AWS CLI

mel*_*ous 3 powershell amazon-web-services

我已经使用 powershell 脚本安装了 aws cli

 $command = "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12"
 Invoke-Expression $command
 Invoke-WebRequest -Uri "https://awscli.amazonaws.com/AWSCLIV2.msi" -Outfile C:\AWSCLIV2.msi
 $arguments = "/i `"C:\AWSCLIV2.msi`" /quiet"
 Start-Process msiexec.exe -ArgumentList $arguments -Wait
 aws --version
Run Code Online (Sandbox Code Playgroud)

当我尝试打印aws --version它时会出现以下错误。

aws : The term 'aws' 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
+ aws
+ ~~~
Run Code Online (Sandbox Code Playgroud)

mel*_*ous 7

在安装 aws cli 后,我能够通过添加以下行来解决此问题:

$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
Run Code Online (Sandbox Code Playgroud)

完整代码:

$command = "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12"
Invoke-Expression $command
Invoke-WebRequest -Uri "https://awscli.amazonaws.com/AWSCLIV2.msi" -Outfile C:\AWSCLIV2.msi
$arguments = "/i `"C:\AWSCLIV2.msi`" /quiet"
Start-Process msiexec.exe -ArgumentList $arguments -Wait
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
aws --version
aws s3 ls
Run Code Online (Sandbox Code Playgroud)