

# 将 `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`。该命令还通过按顺序包含虚拟 MFA 设备中的前两个代码，将设备与 AWS 同步。  

```
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`，通过其序列号识别设备。该命令还通过按顺序包含虚拟 MFA 设备中的前两个代码，将设备与 AWS 同步。**  

```
$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 Reference (V4)》**中的 [EnableMfaDevice](https://docs.aws.amazon.com/powershell/v4/reference)。

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

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

```
$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 参考 (V5)*》中的 [EnableMfaDevice](https://docs.aws.amazon.com/powershell/v5/reference)。

------

有关 AWS SDK 开发人员指南和代码示例的完整列表，请参阅 [将此服务与 AWS SDK 结合使用](sdk-general-information-section.md) 本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。