Reputation: 34119
I have the following powershell script that runs via packer
utility while creating aws ami image.
This script downloads and installs aws cli and then immediately try to use it. The installation process will update windows PATH environment variable but I think it will not be available immediately in the same script. So I set the location to where cli is installed before using it.
$SETUP_DIR = "C:\Setup"
New-Item -Path $SETUP_DIR -ItemType Directory -Force -ErrorAction SilentlyContinue
Clear-Host
Set-Location -Path $SETUP_DIR -PassThru
# install AWS CLI
msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi /quiet
Write-Host "AWS CLI installation completed."
# Set location to AWS CLI
Set-Location -Path "C:\Program Files\Amazon\AWSCLIV2" -PassThru
# Download utility from AWS S3
aws s3 cp s3://tools/utility.exe
Write-Host "Utility download completed."
# switch the location back to c:\setup
Set-Location -Path $SETUP_DIR -PassThru
However when the script executed, it throws error as The term 'aws' is not recognized
- aws s3 cp s3://tools/utility.exe ... ==> amazon-ebs.windows_server: + ~~~ ==> amazon-ebs.windows_server: + CategoryInfo : ObjectNotFound: (aws:String) [], CommandNotFoundException ==> amazon-ebs.windows_server: + FullyQualifiedErrorId : CommandNotFoundException ==> amazon-ebs.windows_server: amazon-ebs.windows_server: ==> amazon-ebs.windows_server: aws : The term 'aws' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the ==> amazon-ebs.windows_server: spelling of the name, or if a path was included, verify that the path is correct and try again.
Upvotes: 1
Views: 2253
Reputation: 6615
When you run aws s3 ... system is trying to locate aws
in the system path it knows about. The fact that you change directory to the location of the aws binary does not do anything for you.
As you probably know, even when you are in the same directory as a binary, if you simply try to use it, it won't work if it is not in the path, and that's why you would use .\binary_name.exe That's a safety feature in PowerShell.
You could try updating the path as suggested in the commends by adding that location:
$env:path="$env:path;C:\Program Files\Amazon\AWSCLIV2"
or point to the binary directly. You could also create an alias like:
set-alias -name aws -value "c:\program files\Amazon\awscliv2\aws.cmd"
Then use aws and it should run the aws.cmd (check that folder to make sure you are calling the correct binary. It used to be aws.cmd for aws cli v1 but may have changed)
Upvotes: 1