

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 [AWS SDK 範例](https://github.com/awsdocs/aws-doc-sdk-examples)。

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

# 使用 Tools for PowerShell V4 的 Amazon ECS 範例
<a name="powershell_4_ecs_code_examples"></a>

下列程式碼範例示範如何使用 AWS Tools for PowerShell V4 搭配 Amazon ECS 來執行動作和實作常見案例。

*Actions* 是大型程式的程式碼摘錄，必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數，但您可以在其相關情境中查看內容中的動作。

每個範例均包含完整原始碼的連結，您可在連結中找到如何設定和執行內容中程式碼的相關指示。

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

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

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

以下程式碼範例顯示如何使用 `Get-ECSClusterDetail`。

**Tools for PowerShell V4**  
**範例 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 參考 (V4)》**中的 [DescribeClusters](https://docs.aws.amazon.com/powershell/v4/reference)。

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

以下程式碼範例顯示如何使用 `Get-ECSClusterList`。

**Tools for PowerShell V4**  
**範例 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 參考 (V4)》**中的 [ListClusters](https://docs.aws.amazon.com/powershell/v4/reference)。

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

以下程式碼範例顯示如何使用 `Get-ECSClusterService`。

**Tools for PowerShell V4**  
**範例 1：此範例列出在預設叢集中執行的所有服務。**  

```
Get-ECSClusterService
```
**範例 2：此範例列出在指定叢集中執行的所有服務。**  

```
Get-ECSClusterService -Cluster myCluster
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [ListServices](https://docs.aws.amazon.com/powershell/v4/reference)。

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

以下程式碼範例顯示如何使用 `Get-ECSService`。

**Tools for PowerShell V4**  
**範例 1：此範例示範如何從預設叢集擷取特定服務的詳細資訊。**  

```
Get-ECSService -Service my-hhtp-service
```
**範例 2：此範例示範如何在具名叢集中，擷取執行之特定服務的詳細資訊。**  

```
Get-ECSService -Cluster myCluster -Service my-hhtp-service
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DescribeServices](https://docs.aws.amazon.com/powershell/v4/reference)。

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

以下程式碼範例顯示如何使用 `New-ECSCluster`。

**Tools for PowerShell V4**  
**範例 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 參考 (V4)》**中的 [CreateCluster](https://docs.aws.amazon.com/powershell/v4/reference)。

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

以下程式碼範例顯示如何使用 `New-ECSService`。

**Tools for PowerShell V4**  
**範例 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 參考 (V4)》**中的 [CreateService](https://docs.aws.amazon.com/powershell/v4/reference)。

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

以下程式碼範例顯示如何使用 `Remove-ECSCluster`。

**Tools for PowerShell V4**  
**範例 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 參考 (V4)》**中的 [DeleteCluster](https://docs.aws.amazon.com/powershell/v4/reference)。

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

以下程式碼範例顯示如何使用 `Remove-ECSService`。

**Tools for PowerShell V4**  
**範例 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 參考 (V4)》**中的 [DeleteService](https://docs.aws.amazon.com/powershell/v4/reference)。

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

以下程式碼範例顯示如何使用 `Update-ECSClusterSetting`。

**Tools for PowerShell V4**  
**範例 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 參考 (V4)》**中的 [UpdateClusterSettings](https://docs.aws.amazon.com/powershell/v4/reference)。

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

以下程式碼範例顯示如何使用 `Update-ECSService`。

**Tools for PowerShell V4**  
**範例 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 參考 (V4)》**中的 [UpdateService](https://docs.aws.amazon.com/powershell/v4/reference)。