13

有谁知道如何使用 AWS Powershell 获取 AWS 帐号?看起来没有可用的 API。

4

4 回答 4

11

不是直接的。但是,您的帐户 ID 是您创建的资源Arn的一部分......以及为您自动创建的资源。一些资源还会将您列为 OwnerId。

默认安全组是在每个区域为您自动创建的,并且无法删除。这使它成为检索我们的帐户 ID 的可靠候选者。

例子:

PS C:/> $accountId = @(get-ec2securitygroup -GroupNames "default")[0].OwnerId

PS C:/> $accountId
000011112222
于 2015-06-05T01:18:07.923 回答
8

漂亮又简单

(Get-STSCallerIdentity).Account

...或者

(Get-STSCallerIdentity -Region [your_aws_region]).Account

于 2019-11-19T13:58:31.373 回答
1

我无法对提供的其他答案发表评论,因此我必须提供自己的解决方案作为轻微修改。

我确实相信所有组上的 OwnerId 都将是 Account Id。但是,您可能没有“默认”组。我建议省略 -GroupNames “默认”。此外,我使用 SAML 令牌展示了我的示例,因为这是我们使用 AD 授权的情况。

$awsAccountNumber = (get-ec2securitygroup -ProfileName saml -Region us-west-2)[0].OwnerId

希望这会有一些用处。

于 2018-03-08T00:22:13.523 回答
0

我使用 Get-STSCallerIdentity。

这是我使用的代码片段:

    foreach ($ProfileName in $Profiles){
    if ($ProfileName -match 'gov') { 
        Initialize-AWSDefaultConfiguration -ProfileName $ProfileName -Region us-gov-east-1
        $STSCallerIdentity = Get-STSCallerIdentity
    }
    else { 
        Initialize-AWSDefaultConfiguration -ProfileName $ProfileName -Region us-east-1 
        $STSCallerIdentity = Get-STSCallerIdentity
    }
于 2019-08-20T12:27:57.207 回答