

文档 AWS SDK 示例 GitHub 存储库中还有更多 [S AWS DK 示例](https://github.com/awsdocs/aws-doc-sdk-examples)。

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

# 将 `EnableMfaDevice` 与 CLI 配合使用
<a name="iam_example_iam_EnableMfaDevice_section"></a>

以下代码示例演示如何使用 `EnableMfaDevice`。

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

**AWS CLI**  
**启用 MFA 设备**  
使用 `create-virtual-mfa-device` 命令创建新的虚拟 MFA 设备后，您可以将 MFA 设备分配给用户。以下 `enable-mfa-device` 示例将序列号为 `arn:aws:iam::210987654321:mfa/BobsMFADevice` 的 MFA 设备分配给用户 `Bob`。该命令还 AWS 通过按顺序包含虚拟 MFA 设备中的前两个代码来与设备同步。  

```
aws iam enable-mfa-device \
    --user-name Bob \
    --serial-number arn:aws:iam::210987654321:mfa/BobsMFADevice \
    --authentication-code1 123456 \
    --authentication-code2 789012
```
此命令不生成任何输出。  
有关更多信息，请参阅《AWS IAM 用户指南》**中的[启用虚拟多重身份验证（MFA）设备](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_mfa_enable_virtual.html)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[EnableMfaDevice](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iam/enable-mfa-device.html)*中的。

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

**适用于 PowerShell V4 的工具**  
**示例 1：此命令启用序列号为 `987654321098` 的硬件 MFA 设备，并将该设备与用户 `Bob` 关联。它包含设备中按顺序排列的前两个代码。**  

```
Enable-IAMMFADevice -UserName "Bob" -SerialNumber "987654321098" -AuthenticationCode1 "12345678" -AuthenticationCode2 "87654321"
```
**示例 2：此示例创建并启用虚拟 MFA 设备。第一条命令创建虚拟设备并在变量 `$MFADevice` 中返回设备的对象表示形式。您可以使用 `.Base32StringSeed` 或 `QRCodePng` 属性来配置用户的软件应用程序。最后一条命令将该设备分配给用户 `David`，通过其序列号识别设备。该命令还 AWS 通过按顺序包含虚拟 MFA 设备中的前两个代码来与设备同步。**  

```
$MFADevice = New-IAMVirtualMFADevice -VirtualMFADeviceName "MyMFADevice"
# see example for New-IAMVirtualMFADevice to see how to configure the software program with PNG or base32 seed code
Enable-IAMMFADevice -UserName "David" -SerialNumber -SerialNumber $MFADevice.SerialNumber -AuthenticationCode1 "24681357" -AuthenticationCode2 "13572468"
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 4) [EnableMfaDevice](https://docs.aws.amazon.com/powershell/v4/reference)中的。

**适用于 PowerShell V5 的工具**  
**示例 1：此命令启用序列号为 `987654321098` 的硬件 MFA 设备，并将该设备与用户 `Bob` 关联。它包含设备中按顺序排列的前两个代码。**  

```
Enable-IAMMFADevice -UserName "Bob" -SerialNumber "987654321098" -AuthenticationCode1 "12345678" -AuthenticationCode2 "87654321"
```
**示例 2：此示例创建并启用虚拟 MFA 设备。第一条命令创建虚拟设备并在变量 `$MFADevice` 中返回设备的对象表示形式。您可以使用 `.Base32StringSeed` 或 `QRCodePng` 属性来配置用户的软件应用程序。最后一条命令将该设备分配给用户 `David`，通过其序列号识别设备。该命令还 AWS 通过按顺序包含虚拟 MFA 设备中的前两个代码来与设备同步。**  

```
$MFADevice = New-IAMVirtualMFADevice -VirtualMFADeviceName "MyMFADevice"
# see example for New-IAMVirtualMFADevice to see how to configure the software program with PNG or base32 seed code
Enable-IAMMFADevice -UserName "David" -SerialNumber -SerialNumber $MFADevice.SerialNumber -AuthenticationCode1 "24681357" -AuthenticationCode2 "13572468"
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 5) [EnableMfaDevice](https://docs.aws.amazon.com/powershell/v5/reference)中的。

------