

第 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 CLI 進行入口網站登入
<a name="creds-idc-cli"></a>

從 Tools for PowerShell 4.1.538 版開始，設定 SSO 憑證和啟動 AWS 存取入口網站工作階段的建議方法是使用 [https://docs.aws.amazon.com/powershell/v5/reference/index.html?page=Initialize-AWSSSOConfiguration.html&tocid=Initialize-AWSSSOConfiguration](https://docs.aws.amazon.com/powershell/v5/reference/index.html?page=Initialize-AWSSSOConfiguration.html&tocid=Initialize-AWSSSOConfiguration)和 [https://docs.aws.amazon.com/powershell/v5/reference/index.html?page=Invoke-AWSSSOLogin.html&tocid=Invoke-AWSSSOLogin](https://docs.aws.amazon.com/powershell/v5/reference/index.html?page=Invoke-AWSSSOLogin.html&tocid=Invoke-AWSSSOLogin) cmdlet，如中所述[AWS Tools for PowerShell 使用 驗證 AWS](creds-idc.md)。如果您無法存取該版本的 Tools for PowerShell （或更新版本），或無法使用這些 cmdlet，您仍然可以使用 執行這些任務 AWS CLI。

## 設定 Tools for PowerShell 透過 使用 IAM Identity Center AWS CLI。
<a name="idc-config-sdk-cli"></a>

如果您尚未這麼做，請務必在繼續之前[啟用和設定 IAM Identity Center](creds-idc.md#idc-config-sso)。

有關如何透過 設定 Tools for PowerShell 以使用 IAM Identity Center 的資訊 AWS CLI ，請參閱 *AWS SDKs 和 Tools 參考指南*中的 [IAM Identity Center 身分驗證](https://docs.aws.amazon.com/sdkref/latest/guide/access-sso.html)主題中的**步驟 2**。完成此組態之後，您的系統應該包含下列元素：
+ 在執行應用程式之前 AWS CLI，您用來啟動 AWS 存取入口網站工作階段的 。
+ 包含[`[default]`設定檔](https://docs.aws.amazon.com/sdkref/latest/guide/file-format.html#file-format-profile)的共用 AWS `config`檔案，其中包含一組組態值，可從 Tools for PowerShell 參考。若要尋找此檔案的位置，請參閱 *AWS SDK 和工具參考指南*中的[共用檔案位置](https://docs.aws.amazon.com/sdkref/latest/guide/file-location.html)。Tools for PowerShell 會使用設定檔的 SSO 權杖提供者，在傳送要求至 AWS之前取得憑證。此`sso_role_name`值是連接至 IAM Identity Center 許可集的 IAM 角色，應允許存取您應用程式中 AWS 服務 使用的 。

  下列範例`config`檔案顯示使用 SSO 字符提供者設定的`[default]`設定檔。設定檔的 `sso_session` 設定是指已命名的 `sso-session` 區段。`sso-session` 區段包含啟動 AWS 存取入口網站工作階段的設定。

  ```
  [default]
  sso_session = my-sso
  sso_account_id = 111122223333
  sso_role_name = SampleRole
  region = us-east-1
  output = json
  
  [sso-session my-sso]
  sso_region = us-east-1
  sso_start_url = https://provided-domain.awsapps.com/start
  sso_registration_scopes = sso:account:access
  ```

**重要**  
您的 PowerShell 工作階段必須安裝並匯入下列模組，以便 SSO 解析能夠運作：  
`AWS.Tools.SSO`
`AWS.Tools.SSOOIDC`
如果您使用的是舊版的 Tools for PowerShell，但沒有這些模組，您會收到類似下列的錯誤：「找不到組件 AWSSDK.SSOOIDC...」。

## 啟動 AWS 存取入口網站工作階段
<a name="idc-start-portal-cli"></a>

在執行 存取的命令之前 AWS 服務，您需要作用中的 AWS 存取入口網站工作階段，以便 Tools for Windows PowerShell 可以使用 IAM Identity Center 身分驗證來解析登入資料。視您設定的工作階段長度而定，您的存取最終會過期，而 Tools for Windows PowerShell 會遇到驗證錯誤。若要登入 AWS 存取入口網站，請在 中執行下列命令 AWS CLI。

```
aws sso login
```

由於您使用的是 `[default]`設定檔，因此您不需要使用 `--profile`選項呼叫 命令。如果您的 SSO 權杖提供者組態使用具名設定檔，則命令會`aws sso login --profile named-profile`改為 。如需具名設定檔的詳細資訊，請參閱 *AWS SDKs 和工具參考指南*中的[設定檔](https://docs.aws.amazon.com/sdkref/latest/guide/file-format.html#file-format-profile)一節。

若要測試您是否已經有作用中的工作階段，請執行下列 AWS CLI 命令 （具有與具名設定檔相同的考量）：

```
aws sts get-caller-identity
```

對此命令的回應，應報告共用 `config` 檔案中設定的 IAM Identity Center 帳戶和許可集合。

**注意**  
如果您已有作用中的 AWS 存取入口網站工作階段並執行 `aws sso login`，則不需要提供登入資料。  
登入程序可能會提示您允許 AWS CLI 存取您的資料。由於 AWS CLI 建置在適用於 Python 的 SDK 之上，因此許可訊息可能包含`botocore`名稱的變化。

## 範例
<a name="idc-short-example-cli"></a>

以下是如何將 IAM Identity Center 與 Tools for PowerShell 搭配使用的範例。假設如下：
+ 您已啟用 IAM Identity Center，並依照本主題先前所述進行設定。SSO 屬性位於 `[default]` 設定檔中。
+ 當您 AWS CLI 使用 透過 登入時`aws sso login`，該使用者至少具有 Amazon S3 的唯讀許可。
+ 部分 S3 儲存貯體可供該使用者檢視。

使用下列 PowerShell 命令來顯示 S3 儲存貯體的清單：

```
Install-Module AWS.Tools.Installer
Install-AWSToolsModule S3
# And if using an older version of the AWS Tools for PowerShell:
Install-AWSToolsModule SSO, SSOOIDC

# In older versions of the AWS Tools for PowerShell, we're not invoking a cmdlet from these modules directly, 
# so we must import them explicitly:
Import-Module AWS.Tools.SSO
Import-Module AWS.Tools.SSOOIDC

# Older versions of the AWS Tools for PowerShell don't support the SSO login flow, so login with the CLI
aws sso login

# Now we can invoke cmdlets using the SSO profile
Get-S3Bucket
```

如上所述，由於您正在使用 `[default]`設定檔，因此您不需要使用 `-ProfileName`選項呼叫 `Get-S3Bucket` cmdlet。如果您的 SSO 權杖提供者組態使用已命名的設定檔，則命令為 `Get-S3Bucket -ProfileName named-profile`。如需具名設定檔的詳細資訊，請參閱 *AWS SDKs 和工具參考指南*中的[設定檔](https://docs.aws.amazon.com/sdkref/latest/guide/file-format.html#file-format-profile)一節。

## 其他資訊
<a name="idc-additional-info-cli"></a>
+ 有關 Tools for PowerShell 驗證的更多選項，例如使用設定檔和環境變量，請參閱 *AWS SDK 和工具參考指南*中的[組態](https://docs.aws.amazon.com/sdkref/latest/guide/creds-config-files.html)章節。
+ 有些命令需要指定 AWS 區域。有多種方法可以執行此操作，包括 `-Region` cmdlet 選項、`[default]`設定檔和`AWS_REGION`環境變數。如需詳細資訊，請參閱本指南[指定 AWS 的區域 AWS Tools for PowerShell](pstools-installing-specifying-region.md)中的 和 *AWS SDKs和工具參考指南*中的[AWS 區域](https://docs.aws.amazon.com/sdkref/latest/guide/feature-region.html)。
+ 如需了解有關最佳實務的資訊，請參閱 *IAM 使用者指南*中的 [IAM 安全最佳實務](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)。
+ 若要建立短期 AWS 登入資料，請參閱《*IAM 使用者指南*》中的[暫時安全登入](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp.html)資料。
+ 若要瞭解其他憑證提供者，請參閱 *AWS SDK 和工具參考指南*中的[標準化憑證提供者](https://docs.aws.amazon.com/sdkref/latest/guide/standardized-credentials.html)。