【发布时间】:2021-10-08 15:28:37
【问题描述】:
有人知道如何使用 AWS Powershell 获取 AWS 帐号吗?似乎没有可用的 API。
【问题讨论】:
标签: powershell amazon-web-services aws-powershell
有人知道如何使用 AWS Powershell 获取 AWS 帐号吗?似乎没有可用的 API。
【问题讨论】:
标签: powershell amazon-web-services aws-powershell
不直接。但是,您的帐户 ID 是您创建的资源的 Arn 的一部分……以及自动为您创建的资源。一些资源还会将您列为 OwnerId。
Default Security Group 是在每个地区自动为您创建的,无法删除。这使它成为检索我们的帐户 ID 的可靠候选者。
例子:
PS C:/> $accountId = @(get-ec2securitygroup -GroupNames "default")[0].OwnerId
PS C:/> $accountId
000011112222
【讨论】:
漂亮又简单
(Get-STSCallerIdentity).Account
...或
(Get-STSCallerIdentity -Region [your_aws_region]).Account
【讨论】:
我无法对提供的其他答案发表评论,因此我必须提供自己的解决方案作为轻微修改。
我确实相信所有组上的 OwnerId 都是帐户 ID。但是,您可能没有“默认”组。我建议省略 -GroupNames “默认”。另外,我正在使用 SAML 令牌展示我的示例,因为这是我们使用 AD 授权的情况。
$awsAccountNumber = (get-ec2securitygroup -ProfileName saml -Region us-west-2)[0].OwnerId
希望这会有一些用处。
【讨论】:
我使用 Get-STSCallerIdentity。
这是我使用的代码 sn-p:
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
}
【讨论】: