我对powershell还是个新手。我不知道为什么在成功安装AWS CLI后,我间歇性地返回aws
命令不可识别错误。我进入睡眠状态,想着可能会在后台设置一些环境变量。需要帮助弄清楚我需要做什么才能成功执行$putItem命令。
我遵循这里的说明https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-windows.html
请注意:整个事情必须是自动化的,所以我不能手动登录到一个主机和修复的东西,因为这个相同的脚本必须在100+主机上运行
Write-Output "Checking if AWS CLI support exists..."
cmd.exe /c "aws --version"
if ($LASTEXITCODE -eq 0){
Write-Output "AWS CLI installed already"
} else {
Write-Output "Installing AWS CLI V2"
cmd.exe /c "msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi /qn"
if ($LASTEXITCODE -eq 0){
Write-Output "AWS CLI installed successfully"
Start-Sleep -s 5
} else {
Write-Output "Could not install AWS CLI"
exit 1
}
}
$putItem = 'aws dynamodb put-item --table-name ' + $instanceStatusDDBTable + ' --item "{"HostName" : {"S" : "' + $instanceName + '"}, "Modules" : {"M" : {}}, "DAGName" : {"S" : "' + $dagName +'"}}"'
Write-Output "Executing DB put item query $putItem"
cmd.exe /c $putItem
if ($LASTEXITCODE -eq 0){
Write-Output "Created entry for $instanceName in $instanceStatusDDBTable DDB table"
} else {
Write-Output "Could not complete put Item operation for $instanceName"
exit 1
}
下面是输出
Checking if AWS CLI support exists...
Installing AWS CLI V2
AWS CLI installed successfully
Executing DB put item query aws dynamodb put-item --table-name Ex2019-HostStatusTable --item "{"HostName" : {"S" : "Host1"}, "Modules" : {"M" : {}}, "DAGName" : {"S" : "USW-D01"}}"
Could not complete put Item operation for Host1
错误输出-
'aws' is not recognized as an internal or external command,
operable program or batch file.
尝试添加以下代码以在检查$LASTEXITCODE变量后刷新环境变量。shell会话必须重新收集安装程序刚刚添加的更新的环境变量。
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
你也可以考虑使用Start-Process使用-wait
和-passthru
参数来调用安装程序,因为cmd可能不会等待足够长的时间来完成应用程序的安装。你可以在这里阅读。我同意David的观点,您可以通过运行aws --version
来检查它是否安装,然后读取版本号或在try catch块中捕获错误。