繁体   English   中英

适用于 AWS EC2 多账户的 Powershell 脚本

[英]Powershell Scripts for AWS EC2 Multiaccounts

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

感谢致敬。

我假设您需要为每个帐户提供不同的凭据。 对于每个账户,您需要创建一个 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 {

        }

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM