搭ListPolicies配 AWS SDK或使用 CLI - AWS Organizations

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

ListPolicies配 AWS SDK或使用 CLI

下列程式碼範例會示範如何使用ListPolicies

.NET
AWS SDK for .NET
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

using System; using System.Threading.Tasks; using Amazon.Organizations; using Amazon.Organizations.Model; /// <summary> /// Shows how to list the AWS Organizations policies associated with an /// organization. /// </summary> public class ListPolicies { /// <summary> /// Initializes an Organizations client object, and then calls its /// ListPoliciesAsync method. /// </summary> public static async Task Main() { // Create the client object using the default account. IAmazonOrganizations client = new AmazonOrganizationsClient(); // The value for the Filter parameter is required and must must be // one of the following: // AISERVICES_OPT_OUT_POLICY // BACKUP_POLICY // SERVICE_CONTROL_POLICY // TAG_POLICY var request = new ListPoliciesRequest { Filter = "SERVICE_CONTROL_POLICY", MaxResults = 5, }; var response = new ListPoliciesResponse(); try { do { response = await client.ListPoliciesAsync(request); response.Policies.ForEach(p => DisplayPolicies(p)); 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 the Organizations policies associated /// with an organization. /// </summary> /// <param name="policy">An Organizations policy summary to display /// information on the console.</param> private static void DisplayPolicies(PolicySummary policy) { string policyInfo = $"{policy.Id} {policy.Name}\t{policy.Description}"; Console.WriteLine(policyInfo); } }
  • 如需詳API細資訊,請參閱AWS SDK for .NET API參考ListPolicies中的。

CLI
AWS CLI

若要擷取特定類型組織中所有策略的清單

下列範例會示範如何取得的清單SCPs,如 filter 參數所指定:

aws organizations list-policies --filter SERVICE_CONTROL_POLICY

輸出包括包含摘要資訊的策略清單:

{ "Policies": [ { "Type": "SERVICE_CONTROL_POLICY", "Name": "AllowAllS3Actions", "AwsManaged": false, "Id": "p-examplepolicyid111", "Arn": "arn:aws:organizations::111111111111:policy/service_control_policy/p-examplepolicyid111", "Description": "Enables account admins to delegate permissions for any S3 actions to users and roles in their accounts." }, { "Type": "SERVICE_CONTROL_POLICY", "Name": "AllowAllEC2Actions", "AwsManaged": false, "Id": "p-examplepolicyid222", "Arn": "arn:aws:organizations::111111111111:policy/service_control_policy/p-examplepolicyid222", "Description": "Enables account admins to delegate permissions for any EC2 actions to users and roles in their accounts." }, { "AwsManaged": true, "Description": "Allows access to every operation", "Type": "SERVICE_CONTROL_POLICY", "Id": "p-FullAWSAccess", "Arn": "arn:aws:organizations::aws:policy/service_control_policy/p-FullAWSAccess", "Name": "FullAWSAccess" } ] }
  • 如需詳API細資訊,請參閱AWS CLI 指令參考ListPolicies中的。

Python
SDK對於 Python(肉毒桿菌 3)
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

def list_policies(policy_filter, orgs_client): """ Lists the policies for the account, limited to the specified filter. :param policy_filter: The kind of policies to return. :param orgs_client: The Boto3 Organizations client. :return: The list of policies found. """ try: response = orgs_client.list_policies(Filter=policy_filter) policies = response["Policies"] logger.info("Found %s %s policies.", len(policies), policy_filter) except ClientError: logger.exception("Couldn't get %s policies.", policy_filter) raise else: return policies
  • 如需詳API細資訊,請參閱ListPoliciesAWS SDK的〈〉以取得 Python (Boto3) API 參考資料。

如需 AWS SDK開發人員指南和程式碼範例的完整清單,請參閱使用 AWS Organizations 用一個 AWS SDK。本主題也包含有關入門的資訊以及舊SDK版的詳細資訊。