

第 5 版 (V5) AWS Tools for PowerShell 已發行！

如需有關中斷變更和遷移應用程式的資訊，請參閱[遷移主題](https://docs.aws.amazon.com/powershell/v5/userguide/migrating-v5.html)。

 [https://docs.aws.amazon.com/powershell/v5/userguide/migrating-v5.html](https://docs.aws.amazon.com/powershell/v5/userguide/migrating-v5.html)

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

# 中的共用登入資料 AWS Tools for PowerShell
<a name="shared-credentials-in-aws-powershell"></a>

適用於 Windows PowerShell 的工具支援使用 AWS 共用登入資料檔案，類似於 AWS CLI 和其他 AWS SDKs。適用於 Windows PowerShell 的工具現在支援讀取和寫入 `basic`、 `session`和 `assume role`登入資料設定檔到 .NET 登入資料檔案和 AWS 共用登入資料檔案。此功能透過新的 `Amazon.Runtime.CredentialManagement` 命名空間來啟用。

**警告**  
為避免安全風險，在開發專用軟體或使用真實資料時，請勿使用 IAM 使用者進行身分驗證。相反地，搭配使用聯合功能和身分提供者，例如 [AWS IAM Identity Center](https://docs.aws.amazon.com/singlesignon/latest/userguide/what-is.html)。

**注意**  
本主題中的資訊適用於您需要手動取得及管理短期或長期憑證的情況。有關短期和長期憑證的其他資訊，請參閱 *AWS SDK 和工具參考指南*中的[其他驗證方法](https://docs.aws.amazon.com/sdkref/latest/guide/access-users.html)。  
如需最佳實務，請使用 AWS IAM Identity Center，如 中所述[使用 驗證 AWS](creds-idc.md)。

已新增至憑證相關 cmdlet、[Initiicize-AWSDefaultConfiguration](https://docs.aws.amazon.com/powershell/v5/reference/items/Initialize-AWSDefaultConfiguration.html)、[New-AWSCredential](https://docs.aws.amazon.com/powershell/v5/reference/items/New-AWSCredential.html) 和 [Set-AWSCredential](https://docs.aws.amazon.com/powershell/v5/reference/items/Set-AWSCredential.html) 的下列參數支援共用 AWS 憑證檔案的新設定檔類型和存取權。在服務 cmdlet 中，您可以新增通用參數 `-ProfileName` 來參考您的描述檔。

## 搭配 使用 IAM 角色 AWS Tools for PowerShell
<a name="shared-credentials-assume-role"></a>

 AWS 共用的登入資料檔案可啟用其他類型的存取。例如，您可以使用 IAM 角色來存取 AWS 資源，而不是 IAM 使用者的長期登入資料。若要執行此作業，您的標準描述檔必須擁有可擔任此角色的許可。當您告知 AWS Tools for PowerShell 使用指定角色的設定檔時， AWS Tools for PowerShell 會查詢 參數識別的設定檔`SourceProfile`。這些憑證可用來請求 `RoleArn` 參數所指定角色的暫時憑證。當第三方擔任角色時，您可以選擇性地要求使用 Multi-Factor Authentication (MFA) 裝置或 `ExternalId` 代碼。


****  

| 參數名稱 | Description | 
| --- | --- | 
|  ExternalId  |  使用者定義的外部 ID，以用於擔任角色 (如果角色要求)。通常只有在您將帳戶的存取權委派給第三方時才需要這麼做。假設指派的角色時，第三方必須包含 ExternalId 做為參數。如需詳細資訊，請參閱《*IAM 使用者指南*》中的[如何在將 AWS 資源的存取權授予第三方時使用外部 ID](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-user_externalid.html)。  | 
|  MfaSerial  |  MFA 序號，以用於擔任角色 (如果角色要求)。如需詳細資訊，請參閱*《IAM 使用者指南》*中的[在 AWS中使用多重要素驗證 (MFA)](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_mfa.html)。  | 
|  RoleArn  |  採用角色憑證時擔任的角色 ARN。如需建立和使用角色的詳細資訊，請參閱*《IAM 使用者指南》*中的 [IAM 角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles.html)。  | 
|  SourceProfile  |  採用角色憑證所用的來源描述檔名稱。在此描述檔中找到的憑證會用來擔任 `RoleArn` 參數指定的角色。  | 

### 設定擔任角色的描述檔
<a name="setup"></a>

以下範例示範如何設定可直接擔任 IAM 角色的來源描述檔。

第一個命令會建立角色描述檔參考的來源描述檔。第二個命令會建立要擔任哪個角色的角色描述檔。第三個命令會顯示角色描述檔的憑證。

```
PS > Set-AWSCredential -StoreAs my_source_profile -AccessKey access_key_id -SecretKey secret_key
PS > Set-AWSCredential -StoreAs my_role_profile -SourceProfile my_source_profile -RoleArn arn:aws:iam::123456789012:role/role-i-want-to-assume
PS > Get-AWSCredential -ProfileName my_role_profile

SourceCredentials                  RoleArn                                              RoleSessionName                           Options
-----------------                  -------                                              ---------------                           -------
Amazon.Runtime.BasicAWSCredentials arn:aws:iam::123456789012:role/role-i-want-to-assume aws-dotnet-sdk-session-636238288466144357 Amazon.Runtime.AssumeRoleAWSCredentialsOptions
```

若要搭配 Tools for Windows PowerShell 服務 cmdlet 使用此角色描述檔，請將 `-ProfileName` 通用參數新增至命令以參考角色描述檔。下列範例使用上一個範例中定義的角色描述檔來存取 [Get-S3Bucket](https://docs.aws.amazon.com/powershell/v5/reference/items/Get-S3Bucket.html) cmdlet. AWS Tools for PowerShell look 中的登入資料`my_source_profile`、使用這些登入資料來`AssumeRole`代表使用者呼叫 ，然後使用這些臨時角色登入資料來呼叫 `Get-S3Bucket`。

```
PS > Get-S3Bucket -ProfileName my_role_profile

CreationDate           BucketName
------------           ----------
2/27/2017 8:57:53 AM   4ba3578c-f88f-4d8b-b95f-92a8858dac58-bucket1
2/27/2017 10:44:37 AM  2091a504-66a9-4d69-8981-aaef812a02c3-bucket2
```

## 使用憑證描述檔類型
<a name="using-the-credential-profile-types"></a>

若要設定憑證描述檔類型，請先了解哪些參數提供描述檔類型所需的資訊。


****  

| 憑證類型 | 您必須使用的參數 | 
| --- | --- | 
|  **基本** 這些是 IAM 使用者的長期憑證  |  `-AccessKey`  `-SecretKey`  | 
|  **工作階段**： 這些是您手動擷取的 IAM 角色短期憑證，例如直接呼叫 [Use-STSRole](https://docs.aws.amazon.com/powershell/v5/reference/items/Use-STSRole.html) cmdlet。  |  `-AccessKey`  `-SecretKey` `-SessionToken`  | 
|  **角色**： 這些是 AWS Tools for PowerShell 為您擷取的 IAM 角色短期憑證。  |  `-SourceProfile` `-RoleArn`  選用：`-ExternalId` 選用：`-MfaSerial`  | 

## `ProfileLocation` 通用參數
<a name="the-profileslocation-common-parameter"></a>

您可以使用 `-ProfileLocation` 寫入共用憑證檔案，以及指示 cmdlet 讀取憑證檔案。加入 `-ProfileLocation` 參數來控制 Tools for Windows PowerShell 是使用共用憑證檔案或是 .NET 憑證檔案。下表說明參數在 Tools for Windows PowerShell 中的運作方式。


****  

| 描述檔位置值 | 描述檔解析行為 | 
| --- | --- | 
|  null (未設定) 或為空  |  首先，搜尋 .NET 憑證檔案中是否有指定名稱的描述檔。如果找不到設定檔，請在 搜尋 AWS 共用的登入資料檔案`(user's home directory)\.aws\credentials`。  | 
|   AWS 共用登入資料檔案格式的檔案路徑  |  只搜尋指定的檔案中是否有指定名稱的描述檔。  | 

### 將憑證儲存到憑證檔案
<a name="save-credentials-to-a-credentials-file"></a>

若要將憑證寫入並儲存在其中一個憑證檔案，請執行 `Set-AWSCredential` cmdlet。下列範例示範其做法。第一個命令會使用 `Set-AWSCredential` 和 `-ProfileLocation`，將存取金鑰和秘密金鑰新增至 `-ProfileName` 參數指定的描述檔。在第二行，執行 [Get-Content](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-content) cmdlet 以顯示憑證檔案的內容。

```
PS > Set-AWSCredential -ProfileLocation C:\Users\auser\.aws\credentials -ProfileName basic_profile -AccessKey access_key2 -SecretKey secret_key2
PS > Get-Content C:\Users\auser\.aws\credentials

aws_access_key_id=access_key2
aws_secret_access_key=secret_key2
```

## 顯示您的憑證描述檔
<a name="showing-credential-profiles"></a>

執行 [Get-AWSCredential](https://docs.aws.amazon.com/powershell/v5/reference/items/Get-AWSCredential.html) cmdlet 並新增 `-ListProfileDetail` 參數以傳回憑證檔案類型和位置，以及描述檔名稱清單。

```
PS > Get-AWSCredential -ListProfileDetail

ProfileName                     StoreTypeName         ProfileLocation
-----------                     -------------         ---------------
source_profile                  NetSDKCredentialsFile
assume_role_profile             NetSDKCredentialsFile
basic_profile                   SharedCredentialsFile C:\Users\auser\.aws\credentials
```

## 移除憑證描述檔
<a name="removing-credential-profiles"></a>

若要移除憑證描述檔，請執行新的 [Remove-AWSCredentialProfile](https://docs.aws.amazon.com/powershell/v5/reference/items/Remove-AWSCredentialProfile.html) cmdlet。[Clear-AWSCredential](https://docs.aws.amazon.com/powershell/v5/reference/items/Clear-AWSCredential.html) 已移除，但仍適用於回溯相容性。

## 重要說明
<a name="important-notes"></a>

只有 [Initialize-AWSDefaultConfiguration](https://docs.aws.amazon.com/powershell/v5/reference/items/Initialize-AWSDefaultConfiguration.html)、[New-AWSCredential](https://docs.aws.amazon.com/powershell/v5/reference/items/New-AWSCredential.html) 和 [Set-AWSCredential](https://docs.aws.amazon.com/powershell/v5/reference/items/Set-AWSCredential.html) 支援角色描述檔的參數。您無法直接在 `Get-S3Bucket -SourceProfile source_profile_name -RoleArn arn:aws:iam::999999999999:role/role_name` 等命令中指定角色參數。這不起作用，因為服務 cmdlet 不直接支援 `SourceProfile` 或 `RoleArn` 參數。您反倒必須將這些參數存放在描述檔中，然後使用 `-ProfileName` 參數呼叫命令。