

# 将 `ListDashboards` 与 Amazon SDK 或 CLI 配合使用
<a name="example_cloudwatch_ListDashboards_section"></a>

以下代码示例演示如何使用 `ListDashboards`。

------
#### [ .NET ]

**适用于 .NET 的 Amazon SDK (v4)**  
 查看 GitHub，了解更多信息。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/dotnetv4/CloudWatch#code-examples)中查找完整示例，了解如何进行设置和运行。

```
    /// <summary>
    /// Get a list of dashboards.
    /// </summary>
    /// <returns>A list of DashboardEntry objects.</returns>
    public async Task<List<DashboardEntry>> ListDashboards()
    {
        var results = new List<DashboardEntry>();
        var paginateDashboards = _amazonCloudWatch.Paginators.ListDashboards(
            new ListDashboardsRequest());
        // Get the entire list using the paginator.
        await foreach (var data in paginateDashboards.DashboardEntries)
        {
            results.Add(data);
        }

        return results;
    }
```
+  有关 API 详细信息，请参阅《适用于 .NET 的 Amazon SDK API Reference》**中的 [ListDashboards](https://docs.amazonaws.cn/goto/DotNetSDKV4/monitoring-2010-08-01/ListDashboards)。

------
#### [ CLI ]

**Amazon CLI**  
**检索控制面板的列表**  
以下 `list-dashboards` 示例列出了指定账户中的所有控制面板。  

```
aws cloudwatch list-dashboards
```
输出：  

```
{
    "DashboardEntries": [
        {
            "DashboardName": "Dashboard-A",
            "DashboardArn": "arn:aws:cloudwatch::123456789012:dashboard/Dashboard-A",
            "LastModified": "2024-10-11T18:40:11+00:00",
            "Size": 271
        },
        {
            "DashboardName": "Dashboard-B",
            "DashboardArn": "arn:aws:cloudwatch::123456789012:dashboard/Dashboard-B",
            "LastModified": "2024-10-11T18:44:41+00:00",
            "Size": 522
        }
    ]
}
```
有关更多信息，请参阅《Amazon CloudWatch 用户指南》中的[使用 Amazon CloudWatch 控制面板](https://docs.amazonaws.cn/AmazonCloudWatch/latest/monitoring/CloudWatch_Dashboards.html)**。  
+  有关 API 详细信息，请参阅《Amazon CLI Command Reference》**中的 [ListDashboards](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudwatch/list-dashboards.html)。

------
#### [ Java ]

**适用于 Java 的 SDK 2.x**  
 查看 GitHub，了解更多信息。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/cloudwatch#code-examples)中查找完整示例，了解如何进行设置和运行。

```
    /**
     * Lists the available dashboards.
     *
     * @return a {@link CompletableFuture} that completes when the operation is finished.
     * The future will complete exceptionally if an error occurs while listing the dashboards.
     */
    public CompletableFuture<Void> listDashboardsAsync() {
        ListDashboardsRequest listDashboardsRequest = ListDashboardsRequest.builder().build();
        ListDashboardsPublisher paginator = getAsyncClient().listDashboardsPaginator(listDashboardsRequest);
        return paginator.subscribe(response -> {
            response.dashboardEntries().forEach(entry -> {
                logger.info("Dashboard name is: {} ", entry.dashboardName());
                logger.info("Dashboard ARN is: {} ", entry.dashboardArn());
            });
        }).exceptionally(ex -> {
            logger.info("Failed to list dashboards: {} ", ex.getMessage());
            throw new RuntimeException("Error occurred while listing dashboards", ex);
        });
    }
```
+  有关 API 详细信息，请参阅《Amazon SDK for Java 2.x API Reference》**中的 [ListDashboards](https://docs.amazonaws.cn/goto/SdkForJavaV2/monitoring-2010-08-01/ListDashboards)。

------
#### [ Kotlin ]

**适用于 Kotlin 的 SDK**  
 查看 GitHub，了解更多信息。在 [Amazon 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/cloudwatch#code-examples)中查找完整示例，了解如何进行设置和运行。

```
suspend fun listDashboards() {
    CloudWatchClient { region = "us-east-1" }.use { cwClient ->
        cwClient
            .listDashboardsPaginated({})
            .transform { it.dashboardEntries?.forEach { obj -> emit(obj) } }
            .collect { obj ->
                println("Name is ${obj.dashboardName}")
                println("Dashboard ARN is ${obj.dashboardArn}")
            }
    }
}
```
+  有关 API 详细信息，请参阅《Amazon SDK for Kotlin API Reference》**中的 [ListDashboards](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

------
#### [ PowerShell ]

**适用于 PowerShell V4 的工具**  
**示例 1：返回您账户的控制面板集合。**  

```
Get-CWDashboardList
```
**输出**：  

```
DashboardArn DashboardName LastModified        Size
------------ ------------- ------------        ----
arn:...      Dashboard1    7/6/2017 8:14:15 PM 252
```
**示例 2：返回名称以前缀“dev”开头的账户的控制面板集合。**  

```
Get-CWDashboardList -DashboardNamePrefix dev
```
+  有关 API 详细信息，请参阅《*Amazon Tools for PowerShell Cmdlet Reference (V4)*》中的 [ListDashboards](https://docs.amazonaws.cn/powershell/v4/reference)。

**Tools for PowerShell V5**  
**示例 1：返回您账户的控制面板集合。**  

```
Get-CWDashboardList
```
**输出**：  

```
DashboardArn DashboardName LastModified        Size
------------ ------------- ------------        ----
arn:...      Dashboard1    7/6/2017 8:14:15 PM 252
```
**示例 2：返回名称以前缀“dev”开头的账户的控制面板集合。**  

```
Get-CWDashboardList -DashboardNamePrefix dev
```
+  有关 API 详细信息，请参阅《Amazon Tools for PowerShell Cmdlet Reference (V5)》**中的 [ListMfaDevices](https://docs.amazonaws.cn/powershell/v5/reference)。

------

有关 Amazon SDK 开发人员指南和代码示例的完整列表，请参阅 [将 CloudWatch 与 Amazon SDK 结合使用](sdk-general-information-section.md) 本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。