

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

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

# 搭配使用 `SetSecurityGroups` 與 CLI
<a name="elastic-load-balancing-v2_example_elastic-load-balancing-v2_SetSecurityGroups_section"></a>

下列程式碼範例示範如何使用 `SetSecurityGroups`。

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

**AWS CLI**  
**無法使安全群組與負載平衡器相關聯**  
此範例會將指定的安全群組與指定的負載平衡器相關聯。  
命令：  

```
aws elbv2 set-security-groups --load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188 --security-groups sg-5943793c
```
輸出：  

```
{
  "SecurityGroupIds": [
      "sg-5943793c"
  ]
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [SetSecurityGroups](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/elbv2/set-security-groups.html)。

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

**Tools for PowerShell V4**  
**範例 1：此範例會將安全群組 'sg-07c3414abb8811cbd' 新增至指定的負載平衡器。**  

```
Set-ELB2SecurityGroup -LoadBalancerArn 'arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/test-alb/3651b4394dd9a24f' -SecurityGroup 'sg-07c3414abb8811cbd'
```
**輸出：**  

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

**Tools for PowerShell V5**  
**範例 1：此範例會將安全群組 'sg-07c3414abb8811cbd' 新增至指定的負載平衡器。**  

```
Set-ELB2SecurityGroup -LoadBalancerArn 'arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/test-alb/3651b4394dd9a24f' -SecurityGroup 'sg-07c3414abb8811cbd'
```
**輸出：**  

```
sg-07c3414abb8811cbd
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [SetSecurityGroups](https://docs.aws.amazon.com/powershell/v5/reference)。

------