与 AWS SDK或ListAccounts一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

与 AWS SDK或ListAccounts一起使用 CLI

以下代码示例演示如何使用 ListAccounts

.NET
AWS SDK for .NET
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

using System; using System.Threading.Tasks; using Amazon.Organizations; using Amazon.Organizations.Model; /// <summary> /// Uses the AWS Organizations service to list the accounts associated /// with the default account. /// </summary> public class ListAccounts { /// <summary> /// Creates the Organizations client and then calls its /// ListAccountsAsync method. /// </summary> public static async Task Main() { // Create the client object using the default account. IAmazonOrganizations client = new AmazonOrganizationsClient(); var request = new ListAccountsRequest { MaxResults = 5, }; var response = new ListAccountsResponse(); try { do { response = await client.ListAccountsAsync(request); response.Accounts.ForEach(a => DisplayAccounts(a)); if (response.NextToken is not null) { request.NextToken = response.NextToken; } } while (response.NextToken is not null); } catch (AWSOrganizationsNotInUseException ex) { Console.WriteLine(ex.Message); } } /// <summary> /// Displays information about an Organizations account. /// </summary> /// <param name="account">An Organizations account for which to display /// information on the console.</param> private static void DisplayAccounts(Account account) { string accountInfo = $"{account.Id} {account.Name}\t{account.Status}"; Console.WriteLine(accountInfo); } }
  • 有关API详细信息,请参阅 “AWS SDK for .NET API参考 ListAccounts” 中的。

CLI
AWS CLI

检索组织中所有账户的列表

以下示例演示了如何请求组织中的账户列表:

aws organizations list-accounts

输出包含账户摘要对象的列表。

{ "Accounts": [ { "Arn": "arn:aws:organizations::111111111111:account/o-exampleorgid/111111111111", "JoinedMethod": "INVITED", "JoinedTimestamp": 1481830215.45, "Id": "111111111111", "Name": "Master Account", "Email": "bill@example.com", "Status": "ACTIVE" }, { "Arn": "arn:aws:organizations::111111111111:account/o-exampleorgid/222222222222", "JoinedMethod": "INVITED", "JoinedTimestamp": 1481835741.044, "Id": "222222222222", "Name": "Production Account", "Email": "alice@example.com", "Status": "ACTIVE" }, { "Arn": "arn:aws:organizations::111111111111:account/o-exampleorgid/333333333333", "JoinedMethod": "INVITED", "JoinedTimestamp": 1481835795.536, "Id": "333333333333", "Name": "Development Account", "Email": "juan@example.com", "Status": "ACTIVE" }, { "Arn": "arn:aws:organizations::111111111111:account/o-exampleorgid/444444444444", "JoinedMethod": "INVITED", "JoinedTimestamp": 1481835812.143, "Id": "444444444444", "Name": "Test Account", "Email": "anika@example.com", "Status": "ACTIVE" } ] }
  • 有关API详细信息,请参阅AWS CLI 命令参考ListAccounts中的。