

的版本 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)

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

# 使用适用于 PowerShell V5 的工具的 Amazon ECS 示例
<a name="powershell_ecs_code_examples"></a>

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

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

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

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

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

### `Get-ECSClusterDetail`
<a name="ecs_DescribeClusters_powershell_topic"></a>

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

**适用于 PowerShell V5 的工具**  
**示例 1：此 cmdlet 描述一个或多个 ECS 集群。**  

```
Get-ECSClusterDetail -Cluster "LAB-ECS-CL" -Include SETTINGS | Select-Object *
```
**输出**：  

```
LoggedAt         : 12/27/2019 9:27:41 PM
Clusters         : {LAB-ECS-CL}
Failures         : {}
ResponseMetadata : Amazon.Runtime.ResponseMetadata
ContentLength    : 396
HttpStatusCode   : OK
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 5) [DescribeClusters](https://docs.aws.amazon.com/powershell/v5/reference)中的。

### `Get-ECSClusterList`
<a name="ecs_ListClusters_powershell_topic"></a>

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

**适用于 PowerShell V5 的工具**  
**示例 1：此 cmdlet 返回现有 ECS 集群的列表。**  

```
Get-ECSClusterList
```
**输出**：  

```
arn:aws:ecs:us-west-2:012345678912:cluster/LAB-ECS-CL
arn:aws:ecs:us-west-2:012345678912:cluster/LAB-ECS
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 5) [ListClusters](https://docs.aws.amazon.com/powershell/v5/reference)中的。

### `Get-ECSClusterService`
<a name="ecs_ListServices_powershell_topic"></a>

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

**适用于 PowerShell V5 的工具**  
**示例 1：此示例列出在默认集群中运行的所有服务。**  

```
Get-ECSClusterService
```
**示例 2：此示例列出在指定集群中运行的所有服务。**  

```
Get-ECSClusterService -Cluster myCluster
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 5) [ListServices](https://docs.aws.amazon.com/powershell/v5/reference)中的。

### `Get-ECSService`
<a name="ecs_DescribeServices_powershell_topic"></a>

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

**适用于 PowerShell V5 的工具**  
**示例 1：此示例演示如何从默认集群中检索特定服务的详细信息。**  

```
Get-ECSService -Service my-hhtp-service
```
**示例 2：此示例演示如何检索在指定集群中运行的特定服务的详细信息。**  

```
Get-ECSService -Cluster myCluster -Service my-hhtp-service
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 5) [DescribeServices](https://docs.aws.amazon.com/powershell/v5/reference)中的。

### `New-ECSCluster`
<a name="ecs_CreateCluster_powershell_topic"></a>

以下代码示例演示了如何使用 `New-ECSCluster`。

**适用于 PowerShell V5 的工具**  
**示例 1：此 cmdlet 创建一个新的 Amazon ECS 集群。**  

```
New-ECSCluster -ClusterName "LAB-ECS-CL" -Setting @{Name="containerInsights"; Value="enabled"}
```
**输出**：  

```
ActiveServicesCount               : 0
Attachments                       : {}
AttachmentsStatus                 :
CapacityProviders                 : {}
ClusterArn                        : arn:aws:ecs:us-west-2:012345678912:cluster/LAB-ECS-CL
ClusterName                       : LAB-ECS-CL
DefaultCapacityProviderStrategy   : {}
PendingTasksCount                 : 0
RegisteredContainerInstancesCount : 0
RunningTasksCount                 : 0
Settings                          : {containerInsights}
Statistics                        : {}
Status                            : ACTIVE
Tags                              : {}
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 5) [CreateCluster](https://docs.aws.amazon.com/powershell/v5/reference)中的。

### `New-ECSService`
<a name="ecs_CreateService_powershell_topic"></a>

以下代码示例演示了如何使用 `New-ECSService`。

**适用于 PowerShell V5 的工具**  
**示例 1：此示例命令在您的默认集群中创建了一个名为 ecs-simple-service `的服务。该服务使用“ecs-demo”任务定义，并且它维护该任务的 10 个实例化。**  

```
New-ECSService -ServiceName ecs-simple-service -TaskDefinition ecs-demo -DesiredCount 10
```
**示例 2：此示例命令在默认集群中的负载均衡器后面创建了一个名为 ecs-simple-service `的服务。该服务使用“ecs-demo”任务定义，并且它维护该任务的 10 个实例化。**  

```
$lb = @{
    LoadBalancerName = "EC2Contai-EcsElast-S06278JGSJCM"
    ContainerName = "simple-demo"
    ContainerPort = 80
}        
New-ECSService -ServiceName ecs-simple-service -TaskDefinition ecs-demo -DesiredCount 10 -LoadBalancer $lb
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 5) [CreateService](https://docs.aws.amazon.com/powershell/v5/reference)中的。

### `Remove-ECSCluster`
<a name="ecs_DeleteCluster_powershell_topic"></a>

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

**适用于 PowerShell V5 的工具**  
**示例 1：此 cmdlet 删除指定的 ECS 集群。必须先从该集群中取消注册所有容器实例，然后才能删除此集群。**  

```
Remove-ECSCluster -Cluster "LAB-ECS"
```
**输出**：  

```
Confirm
Are you sure you want to perform this action?
Performing the operation "Remove-ECSCluster (DeleteCluster)" on target "LAB-ECS".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): Y
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 5) [DeleteCluster](https://docs.aws.amazon.com/powershell/v5/reference)中的。

### `Remove-ECSService`
<a name="ecs_DeleteService_powershell_topic"></a>

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

**适用于 PowerShell V5 的工具**  
**示例 1：删除默认集群中名为 my-http-service “” 的服务。在删除服务之前，必须将所需的计数和运行计数设置为 0。在命令继续执行之前，系统会提示您进行确认。要绕过确认提示，请添加 -Force 开关。**  

```
Remove-ECSService -Service my-http-service
```
**示例 2：删除指定集群中名为 my-http-service “” 的服务。**  

```
Remove-ECSService -Cluster myCluster -Service my-http-service
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 5) [DeleteService](https://docs.aws.amazon.com/powershell/v5/reference)中的。

### `Update-ECSClusterSetting`
<a name="ecs_UpdateClusterSettings_powershell_topic"></a>

以下代码示例演示了如何使用 `Update-ECSClusterSetting`。

**适用于 PowerShell V5 的工具**  
**示例 1：此 cmdlet 修改要用于 ECS 集群的设置。**  

```
Update-ECSClusterSetting -Cluster "LAB-ECS-CL" -Setting @{Name="containerInsights"; Value="disabled"}
```
**输出**：  

```
ActiveServicesCount               : 0
Attachments                       : {}
AttachmentsStatus                 :
CapacityProviders                 : {}
ClusterArn                        : arn:aws:ecs:us-west-2:012345678912:cluster/LAB-ECS-CL
ClusterName                       : LAB-ECS-CL
DefaultCapacityProviderStrategy   : {}
PendingTasksCount                 : 0
RegisteredContainerInstancesCount : 0
RunningTasksCount                 : 0
Settings                          : {containerInsights}
Statistics                        : {}
Status                            : ACTIVE
Tags                              : {}
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 5) [UpdateClusterSettings](https://docs.aws.amazon.com/powershell/v5/reference)中的。

### `Update-ECSService`
<a name="ecs_UpdateService_powershell_topic"></a>

以下代码示例演示了如何使用 `Update-ECSService`。

**适用于 PowerShell V5 的工具**  
**示例 1：此示例命令更新 my-http-service ``服务以使用` amazon-ecs-sample `任务定义。**  

```
Update-ECSService -Service my-http-service -TaskDefinition amazon-ecs-sample
```
**示例 2：此示例命令将 my-http-service `服务的所需计数更新为 10。**  

```
Update-ECSService -Service my-http-service -DesiredCount 10
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 5) [UpdateService](https://docs.aws.amazon.com/powershell/v5/reference)中的。