【问题标题】:Powershell Scripts for AWS EC2 Multiaccounts适用于 AWS EC2 多账户的 Powershell 脚本
【发布时间】:2020-02-04 03:51:23
【问题描述】:

我一直在寻找一个示例 powershell 脚本,该脚本将遍历 AWS 中的多个账户,并仅打印每个账户的 EC2 实例信息。我一直在使用 Python/Boto3 来完成相同的任务,但想学习 Powershell 的方法。我已经搜索但找不到任何多帐户示例,特别是如何为每个帐户获取 sts 令牌并承担角色等。如果有人可以请我指出任何这样的示例脚本(如果有),那就太好了。

感谢和问候。

【问题讨论】:

标签: python amazon-web-services powershell boto3 aws-powershell


【解决方案1】:

我假设您需要为每个帐户提供不同的凭据。对于每个账户,您都需要创建一个 AWS 凭证。有关如何执行此操作的示例,请参阅:https://timharkin.com/managing-aws-credentials-with-powershell/

保存凭据后,您可以使用三个嵌套循环来获取您要查找的信息。这些循环将是 Credential->Region-->EC2 Instance。代码如下:

$AccountCreds = "Credential1","Credential2"
$Regions="ap-northeast-1","ap-northeast-2","ap-south-1","ap-southeast-1","ap-southeast-2","ca-central-1","eu-central-1","eu-north-1","eu-west-1","eu-west-2","eu-west-3","sa-east-1","us-east-1","us-east-2","us-west-1","us-west-2"

foreach ($credential in $AccountCreds){
    Set-AWSCredential -ProfileName $credential

    foreach($region in $Regions){

        Set-DefaultAWSRegion $region
        try {

            (Get-EC2Instance  ).Instances |Format-Table  -AutoSize | Out-String  -Width 512

        }
        catch {

        }

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 2012-08-04
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 2014-12-18
    相关资源
    最近更新 更多