

文档 AWS SDK 示例 GitHub 存储库中还有更多 [S AWS DK 示例](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 ]

**适用于 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 参考 (V* 4) [SetSecurityGroups](https://docs.aws.amazon.com/powershell/v4/reference)中的。

**适用于 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 参考 (V* 5) [SetSecurityGroups](https://docs.aws.amazon.com/powershell/v5/reference)中的。

------