

のバージョン 5 (V5) AWS Tools for PowerShell がリリースされました。

重要な変更とアプリケーションの移行については、[「移行トピック](https://docs.aws.amazon.com/powershell/v5/userguide/migrating-v5.html)」を参照してください。

 [https://docs.aws.amazon.com/powershell/v5/userguide/migrating-v5.html](https://docs.aws.amazon.com/powershell/v5/userguide/migrating-v5.html)

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Tools for PowerShell V5 を使用した CloudWatch の例
<a name="powershell_cloudwatch_code_examples"></a>

次のコード例は、CloudWatch で AWS Tools for PowerShell V5 を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています。

*アクション*はより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、コンテキスト内のアクションは、関連するシナリオで確認できます。

各例には完全なソースコードへのリンクが含まれており、コードの設定方法と実行方法に関する手順を確認できます。

**Topics**
+ [アクション](#actions)

## アクション
<a name="actions"></a>

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

次のコード例は、`Get-CWDashboard` を使用する方法を示しています。

**Tools for PowerShell V5**  
**例 1: 指定されたダッシュボードの本体である arn を返します。**  

```
Get-CWDashboard -DashboardName Dashboard1
```
**出力:**  

```
DashboardArn                                          DashboardBody
------------                                          -------------
arn:aws:cloudwatch::123456789012:dashboard/Dashboard1 {...
```
+  API の詳細については、*AWS Tools for PowerShell コマンドレットリファレンス (V5)* の「[GetDashboard](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

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

次のコード例は、`Get-CWDashboardList` を使用する方法を示しています。

**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 の詳細については、*AWS Tools for PowerShell コマンドレットリファレンス (V5)* の「[ListDashboards](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

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

次のコード例は、`Remove-CWDashboard` を使用する方法を示しています。

**Tools for PowerShell V5**  
**例 1: 指定したダッシュボードを削除し、次に進む前に確認を促します。確認を省略するには、-Force スイッチをコマンドに追加します。**  

```
Remove-CWDashboard -DashboardName Dashboard1
```
+  API の詳細については、*AWS Tools for PowerShell コマンドレットリファレンス (V5)* の「[DeleteDashboards](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

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

次のコード例は、`Write-CWDashboard` を使用する方法を示しています。

**Tools for PowerShell V5**  
**例 1: 'Dashboard1' という名前のダッシュボードを作成または更新して、2 つのメトリクスウィジェットを並べて表示します。**  

```
$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: ダッシュボードを作成または更新し、ダッシュボードを説明するコンテンツをコマンドレットにパイプします。**  

```
$dashBody = @"
{
...
}
"@
        
$dashBody | Write-CWDashboard -DashboardName Dashboard1
```
+  API の詳細については、*AWS Tools for PowerShell コマンドレットリファレンス (V5)* の「[PutDashboard](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。

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

次のコード例は、`Write-CWMetricData` を使用する方法を示しています。

**Tools for PowerShell V5**  
**例 1: 新しい MetricDatum オブジェクトを作成し、Amazon Web Services の CloudWatch メトリクスに書き込みます。**  

```
### 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 コマンドレットリファレンス (V5)* の「[PutMetricData](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。