aws / aws-tools-for-powershell

The AWS Tools for PowerShell lets developers and administrators manage their AWS services from the PowerShell scripting environment.
Apache License 2.0
239 stars 79 forks source link

Set-AWSCredential cannot be used within a function #106

Closed yurpcraft closed 4 years ago

yurpcraft commented 4 years ago

Provide a general summary of the issue in the Title above:

When trying to utilize Set-AWSCredential inside of a function, the process fails. The following error is returned: No credentials specified or obtained from persisted/shell defaults. This occurs despite calling the "Set-AWSCredential" inside of a function.

Expected Behavior

When using Set-AWSCredential inside of a function (module, cmdlet, etc), it should set the aws credentials for the rest of that session.

Current Behavior

If describing a bug, tell us what happens instead of the expected behavior. Include full errors, uncaught exceptions, stack traces, and relevant logs. If service responses are relevant, please include Fiddler-captured wirelogs. If suggesting a change/improvement, explain the difference from current behavior.

Possible Solution

I found this forum post where the OP is experiencing the same bug as I am: https://forums.aws.amazon.com/thread.jspa?threadID=259761 "steve@aws" makes it seem like it's a variable scoping issue for the function.

Steps to Reproduce (for bugs)

Run this PowerShell script: https://gist.github.com/mcdonaldbm/6b9d389deeb60c8ec562520ad377938c

Context

We're trying to standardize our "preamble" for logging and setting credentials for our scripts within Octopus Deploy. When we moved our preamble (which included Set-AWSCredential) to the Set-Preamble cmdlet (in-house) we started receiving the error. We've popped it out of the cmdlet in the meantime, but would like to have it set standard across our scripts.

Your Environment

Include as many relevant details about the environment where the bug was discovered.

AWSPowerShell, AWSPowerShell.NETCore Get-AWSPowerShellVersion 4.0.1.1 Name Value


PSVersion 5.1.17134.858 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.17134.858 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1

matteo-prosperi commented 4 years ago

Hello, did you try Set-AWSCredential -Scope Global? Because the credentials are stored in a PowerShell variable, they have to be set in the global scope to be visible outside of the function. See https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes.