

文档 AWS SDK 示例 GitHub 存储库中还有更多 [S AWS DK 示例](https://github.com/awsdocs/aws-doc-sdk-examples)。

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

# CloudWatch 使用 PowerShell V4 工具的示例
<a name="powershell_4_cloudwatch_code_examples"></a>

以下代码示例向您展示了如何使用带 CloudWatch的 AWS Tools for PowerShell V4 来执行操作和实现常见场景。

*操作*是大型程序的代码摘录，必须在上下文中运行。您可以通过操作了解如何调用单个服务函数，还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接，您可以从中找到有关如何在上下文中设置和运行代码的说明。

**Topics**
+ [操作](#actions)

## 操作
<a name="actions"></a>

### `Get-CWAlarm`
<a name="cloudwatch_DescribeAlarms_powershell_4_topic"></a>

以下代码示例演示了如何使用 `Get-CWAlarm`。

**适用于 PowerShell V4 的工具**  
**示例 1：返回所有警报，包括来自的复合警报和指标警报 CloudWatch。**  

```
Get-CWAlarm -MaxRecords 1
```
**输出**：  

```
CompositeAlarms MetricAlarms         NextToken
--------------- ------------         ---------
                {MetricAlarms-01}    NextToken-01
                {MetricAlarms-02}    NextToken-02
                {MetricAlarms-03}    NextToken-03
```
**示例 2：仅返回将AlarmType 参数设置为 CloudWatch 后的复合警报数据 CompositeAlarms。**  

```
Get-CWAlarm -AlarmType 'CompositeAlarms'
```
**输出**：  

```
CompositeAlarms        MetricAlarms NextToken
---------------        ------------ ---------
{CompositeAlarms-01}
{CompositeAlarms-02}
{CompositeAlarms-03}
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 4) [DescribeAlarms](https://docs.aws.amazon.com/powershell/v4/reference)中的。

### `Get-CWDashboard`
<a name="cloudwatch_GetDashboard_powershell_4_topic"></a>

以下代码示例演示了如何使用 `Get-CWDashboard`。

**适用于 PowerShell V4 的工具**  
**示例 1：返回指定控制面板的正文 arn。**  

```
Get-CWDashboard -DashboardName Dashboard1
```
**输出**：  

```
DashboardArn                                          DashboardBody
------------                                          -------------
arn:aws:cloudwatch::123456789012:dashboard/Dashboard1 {...
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 4) [GetDashboard](https://docs.aws.amazon.com/powershell/v4/reference)中的。

### `Get-CWDashboardList`
<a name="cloudwatch_ListDashboards_powershell_4_topic"></a>

以下代码示例演示了如何使用 `Get-CWDashboardList`。

**适用于 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 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 4) [ListDashboards](https://docs.aws.amazon.com/powershell/v4/reference)中的。

### `Remove-CWDashboard`
<a name="cloudwatch_DeleteDashboards_powershell_4_topic"></a>

以下代码示例演示了如何使用 `Remove-CWDashboard`。

**适用于 PowerShell V4 的工具**  
**示例 1：删除指定的控制面板，继续操作前提示确认。要绕过确认，请在命令中添加 -Force 开关。**  

```
Remove-CWDashboard -DashboardName Dashboard1
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 4) [DeleteDashboards](https://docs.aws.amazon.com/powershell/v4/reference)中的。

### `Write-CWDashboard`
<a name="cloudwatch_PutDashboard_powershell_4_topic"></a>

以下代码示例演示了如何使用 `Write-CWDashboard`。

**适用于 PowerShell V4 的工具**  
**示例 1：创建或更新名为“Dashboard1”的控制面板，以包含两个并排的指标小部件。**  

```
$dashBody = @"
{
    "widgets":[
        {
             "type":"metric",
             "x":0,
             "y":0,
             "width":12,
             "height":6,
             "properties":{
                "metrics":[
                   [
                      "AWS/EC2",
                      "CPUUtilization",
                      "InstanceId",
                      "i-012345"
                   ]
                ],
                "period":300,
                "stat":"Average",
                "region":"us-east-1",
                "title":"EC2 Instance CPU"
             }
        },
        {
             "type":"metric",
             "x":12,
             "y":0,
             "width":12,
             "height":6,
             "properties":{
                "metrics":[
                   [
                      "AWS/S3",
                      "BucketSizeBytes",
                      "BucketName",
                      "amzn-s3-demo-bucket"
                   ]
                ],
                "period":86400,
                "stat":"Maximum",
                "region":"us-east-1",
                "title":"amzn-s3-demo-bucket bytes"
            }
        }
    ]
}
"@

Write-CWDashboard -DashboardName Dashboard1 -DashboardBody $dashBody
```
**示例 2：创建或更新控制面板，将描述控制面板的内容通过管道传输到 cmdlet 中。**  

```
$dashBody = @"
{
...
}
"@
        
$dashBody | Write-CWDashboard -DashboardName Dashboard1
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 4) [PutDashboard](https://docs.aws.amazon.com/powershell/v4/reference)中的。

### `Write-CWMetricData`
<a name="cloudwatch_PutMetricData_powershell_4_topic"></a>

以下代码示例演示了如何使用 `Write-CWMetricData`。

**适用于 PowerShell V4 的工具**  
**示例 1：创建新 MetricDatum 对象并将其写入 Amazon Web Serv CloudWatch ices 指标。**  

```
### Create a MetricDatum .NET object
$Metric = New-Object -TypeName Amazon.CloudWatch.Model.MetricDatum
$Metric.Timestamp = [DateTime]::UtcNow
$Metric.MetricName = 'CPU'
$Metric.Value = 50

### Write the metric data to the CloudWatch service
Write-CWMetricData -Namespace instance1 -MetricData $Metric
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 4) [PutMetricData](https://docs.aws.amazon.com/powershell/v4/reference)中的。