

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

# 使用 PowerShell 匯出 AWS IAM Identity Center 身分及其指派的報告
<a name="export-a-report-of-aws-iam-identity-center-identities-and-their-assignments-by-using-powershell"></a>

*Jorge Pava、Frank Allotta、Manideep Reddy Gillela 和 ChadMiles、Amazon Web Services*

## 總結
<a name="export-a-report-of-aws-iam-identity-center-identities-and-their-assignments-by-using-powershell-summary"></a>

當您使用 AWS IAM Identity Center (AWS Single Sign-On 的後繼者） 集中管理所有 Amazon Web Services (AWS) 帳戶和雲端應用程式的單一登入 (SSO) 存取時，透過 AWS 管理主控台報告和稽核這些指派可能會很繁瑣且耗時。如果您報告使用者或群組在數十個或數百個 AWS 帳戶中的許可，尤其如此。

對於許多使用者而言，檢視此資訊的理想工具會位於試算表應用程式中，例如 Microsoft Excel。這可協助您篩選、搜尋和視覺化整個組織的資料，並由 AWS Organizations 管理。

此模式說明如何使用 AWS Tools for PowerShell 在 IAM Identity Center 中產生 SSO 身分組態的報告。報告會格式化為 CSV 檔案，其中包含身分名稱 （委託人）、身分類型 （使用者或群組）、身分可存取的帳戶，以及許可集。產生此報告後，您可以在偏好的應用程式中開啟報告，視需要搜尋、篩選和稽核資料。下圖顯示試算表應用程式中的範例資料。

![\[在試算表應用程式中檢視的 PowerShell 指令碼結果。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/46c7dc7f-c726-4754-b590-2f09d657b167/images/bbc21d8b-fc5d-4b5d-b159-81197a89823e.png)



| 
| 
| 重要：由於此報告包含敏感資訊，因此強烈建議您安全地存放它，並僅在need-to-know的基礎上共用它。 | 
| --- |

## 先決條件和限制
<a name="export-a-report-of-aws-iam-identity-center-identities-and-their-assignments-by-using-powershell-prereqs"></a>

**先決條件**
+ IAM Identity Center 和 AWS Organizations，已設定並啟用。
+ PowerShell，已安裝並設定。如需詳細資訊，請參閱[安裝 PowerShell](https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell?view=powershell-7.2) (Microsoft 文件）。
+ 已安裝和設定的 AWS Tools for PowerShell。基於效能考量，強烈建議您安裝名為 的 AWS Tools for PowerShell 模組化版本`AWS.Tools`。每個 AWS 服務都由其個別的小型模組支援。在 PowerShell shell 中，輸入下列命令來安裝此模式所需的模組：`AWS.Tools.Installer`、`SSOAdmin`、 `Organizations`和 `IdentityStore`。

  ```
  Install-Module AWS.Tools.Installer
  Install-AWSToolsModule -Name Organizations, SSOAdmin, IdentityStore
  ```

  如需詳細資訊，請參閱[在 Windows 上安裝 AWS.Tools](https://docs.aws.amazon.com/powershell/latest/userguide/pstools-getting-set-up-windows.html#ps-installing-awstools) 或[在 Linux 或 macOS 上安裝 AWS.Tools ](https://docs.aws.amazon.com/powershell/latest/userguide/pstools-getting-set-up-linux-mac.html#install-aws.tools-on-linux-macos)(AWS Tools for PowerShell 文件）。如果您在安裝模組時收到錯誤，請參閱此模式的[故障診斷](#export-a-report-of-aws-iam-identity-center-identities-and-their-assignments-by-using-powershell-troubleshooting)一節。
+ AWS 命令列界面 (AWS CLI) 或 AWS 開發套件先前必須使用工作登入資料進行設定，方法為執行下列其中一項：
  + 使用 AWS CLI `aws configure` 如需詳細資訊，請參閱[快速組態](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html#cli-configure-quickstart-config) (AWS CLI 文件）。
  + 設定 AWS CLI 或 AWS 雲端開發套件 (AWS CDK)，透過 AWS Identity and Access Management (IAM) 角色取得暫時存取權。如需詳細資訊，請參閱[取得 CLI 存取的 IAM 角色登入](https://docs.aws.amazon.com/singlesignon/latest/userguide/howtogetcredentials.html)資料 (IAM Identity Center 文件）。
+ AWS CLI 的具名設定檔，其已儲存 IAM 主體的登入資料：
  + 可存取 AWS Organizations 管理帳戶或 IAM Identity Center 的委派管理員帳戶
  + `AWSSSOReadOnly` 和 `AWSSSODirectoryReadOnly` AWS 受管政策是否已套用

  如需詳細資訊，請參閱[使用具名設定檔](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html#cli-configure-files-using-profiles) (AWS CLI 文件） 和 [AWS 受管政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_managed-vs-inline.html#aws-managed-policies) (IAM 文件）。

**限制**
+ 目標 AWS 帳戶必須以 AWS Organizations 中的組織管理。

**產品版本**
+ 對於所有作業系統，建議您使用 [PowerShell 7.0 版或更新版本。](https://github.com/powershell/powershell)

## Architecture
<a name="export-a-report-of-aws-iam-identity-center-identities-and-their-assignments-by-using-powershell-architecture"></a>

**目標架構**

![\[使用 AWS CLI 命名設定檔的指令碼，在 IAM Identity Center 中建立 SSO 身分的報告。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/46c7dc7f-c726-4754-b590-2f09d657b167/images/ae5189aa-8197-4a05-88df-7c0294a679a1.png)


1. 使用者在 PowerShell 命令列中執行指令碼。

1. 指令碼會假設 AWS CLI 的具名設定檔。這會授予 IAM Identity Center 的存取權。

1. 指令碼會從 IAM Identity Center 擷取 SSO 身分組態。

1. 指令碼會在儲存指令碼的本機工作站上的相同目錄中產生 CSV 檔案。

## 工具
<a name="export-a-report-of-aws-iam-identity-center-identities-and-their-assignments-by-using-powershell-tools"></a>

**AWS 服務**
+ [AWS Command Line Interface (AWS CLI)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) 是一種開放原始碼工具，可協助您透過命令列 shell 中的命令與 AWS 服務互動。
+ [AWS IAM Identity Center](https://docs.aws.amazon.com/singlesignon/latest/userguide/what-is.html) 可協助您集中管理所有 AWS 帳戶和雲端應用程式的單一登入 (SSO) 存取。
+ [AWS Tools for PowerShell](https://docs.aws.amazon.com/powershell/latest/userguide/pstools-welcome.html) 是一組 PowerShell 模組，可協助您從 PowerShell 命令列對 AWS 資源執行指令碼操作。

**其他工具**
+ [PowerShell](https://learn.microsoft.com/en-us/powershell/) 是在 Windows、Linux 和 macOS 上執行的 Microsoft 自動化和組態管理程式。

## 史詩
<a name="export-a-report-of-aws-iam-identity-center-identities-and-their-assignments-by-using-powershell-epics"></a>

### 產生報告
<a name="generate-the-report"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 準備指令碼。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/export-a-report-of-aws-iam-identity-center-identities-and-their-assignments-by-using-powershell.html) | 雲端管理員 | 
| 執行指令碼。 | 建議您使用下列命令，在 PowerShell shell 中執行自訂指令碼。<pre>.\SSO-Report.ps1</pre>或者，您也可以輸入下列命令，從另一個 shell 執行指令碼。<pre>pwsh .\SSO-Report.ps1</pre>指令碼會在與指令碼檔案相同的目錄中產生 CSV 檔案。 | 雲端管理員 | 
| 分析報告資料。 | 輸出 CSV 檔案具有 **AccountName**、 **PermissionSet**、 **Principal** 和 **Type** 標頭。在您偏好的試算表應用程式中開啟此檔案。您可以建立資料表來篩選和排序輸出。 | 雲端管理員 | 

## 疑難排解
<a name="export-a-report-of-aws-iam-identity-center-identities-and-their-assignments-by-using-powershell-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| `The term ‘Get-<parameter>’ is not recognized as the name of a cmdlet, function, script file, or operable program.` 錯誤 | 未安裝 AWS Tools for PowerShell 或其模組。在 PowerShell shell 中，輸入下列命令來安裝適用於 PowerShell 的 AWS 工具，以及此模式所需的模組：`AWS.Tools.Installer`、`SSOAdmin`、 `Organizations`和 `IdentityStore`。<pre>Install-Module AWS.Tools.Installer<br />Install-AWSToolsModule -Name Organizations, SSOAdmin, IdentityStore</pre> | 
| `No credentials specified or obtained from persisted/shell defaults` 錯誤 | 在 [Epics](#export-a-report-of-aws-iam-identity-center-identities-and-their-assignments-by-using-powershell-epics) 區段的*準備指令碼*中，確認您已正確輸入 `ProfileName`和 `Region`變數。確定具名設定檔中的設定和登入資料有足夠的許可來管理 IAM Identity Center。 | 
| `Authenticode Issuer …` 安裝 AWS.Tools 模組時發生錯誤 | 將 `-SkipPublisherCheck` 參數新增至 `Install-AWSToolsModule` 命令的結尾。 | 
| `Get-ORGAccountList : Assembly AWSSDK.SSO could not be found or loaded.` 錯誤 | 指定具名 AWS CLI 設定檔、將 AWS CLI 設定為使用 IAM Identity Center 驗證使用者，並將 AWS CLI 設定為自動擷取重新整理的身分驗證字符時，可能會發生此錯誤。若要解決此錯誤，請執行下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/export-a-report-of-aws-iam-identity-center-identities-and-their-assignments-by-using-powershell.html) | 

## 相關資源
<a name="export-a-report-of-aws-iam-identity-center-identities-and-their-assignments-by-using-powershell-resources"></a>
+ [組態設定存放在哪裡？](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html#cli-configure-files-where) (AWS CLI 文件）
+ [設定 AWS CLI 以使用 AWS IAM Identity Center](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-sso.html) (AWS CLI 文件）
+ [使用具名設定檔](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html#cli-configure-files-using-profiles) (AWS CLI 文件）

## 其他資訊
<a name="export-a-report-of-aws-iam-identity-center-identities-and-their-assignments-by-using-powershell-additional"></a>

在下列指令碼中，判斷是否需要更新下列參數的值：
+ 如果您在 AWS CLI 中使用具名設定檔來存取已設定 IAM Identity Center 的帳戶，請更新 `$ProfileName`值。
+ 如果 IAM Identity Center 部署在與您的 AWS CLI 或 AWS SDK 組態預設區域不同的 AWS 區域中，請更新 `$Region`值以使用部署 IAM Identity Center 的區域。
+ 如果這兩種情況都不適用，則不需要更新指令碼。

```
param (
    # The name of the output CSV file
    [String] $OutputFile  = "SSO-Assignments.csv",
    # The AWS CLI named profile
    [String] $ProfileName = "",
    # The AWS Region in which IAM Identity Center is configured
    [String] $Region      = ""
)
$Start = Get-Date; $OrgParams = @{}
If ($Region){ $OrgParams.Region = $Region}
if ($ProfileName){$OrgParams.ProfileName = $ProfileName}
$SSOParams   = $OrgParams.Clone(); $IdsParams = $OrgParams.Clone()
$AccountList = Get-ORGAccountList @OrgParams | Select-Object Id, Name
$SSOinstance = Get-SSOADMNInstanceList @OrgParams
$SSOParams['InstanceArn']       = $SSOinstance.InstanceArn
$IdsParams['IdentityStoreId']   = $SSOinstance.IdentityStoreId
$PSsets       = @{}; $Principals   = @{}
$Assignments  = @(); $AccountCount = 1; Write-Host ""
foreach ($Account in $AccountList) {
    $Duration = New-Timespan -Start $Start -End (Get-Date) | ForEach-Object {[Timespan]::New($_.Days, $_.Hours, $_.Minutes, $_.Seconds)}
    Write-Host "`r$Duration - Account $AccountCount of $($AccountList.Count) (Assignments:$($Assignments.Count))        " -NoNewline
    $AccountCount++
    foreach ($PS in Get-SSOADMNPermissionSetsProvisionedToAccountList -AccountId $Account.Id @SSOParams) {
        if (-not $PSsets[$PS]) {$PSsets[$PS] = (Get-SSOADMNPermissionSet @SSOParams -PermissionSetArn $PS).Name;$APICalls++}
        $AssignmentsResponse = Get-SSOADMNAccountAssignmentList @SSOParams -PermissionSetArn $PS -AccountId $Account.Id
        if ($AssignmentsResponse.NextToken) {$AccountAssignments = $AssignmentsResponse.AccountAssignments}
        else {$AccountAssignments = $AssignmentsResponse}
        While ($AssignmentsResponse.NextToken) {
            $AssignmentsResponse = Get-SSOADMNAccountAssignmentList @SSOParams -PermissionSetArn $PS -AccountId $Account.Id -NextToken $AssignmentsResponse.NextToken
            $AccountAssignments += $AssignmentsResponse.AccountAssignments}
        foreach ($Assignment in $AccountAssignments) {
            if (-not $Principals[$Assignment.PrincipalId]) {
                $AssignmentType = $Assignment.PrincipalType.Value
                $Expression     = "Get-IDS"+$AssignmentType+" @IdsParams -"+$AssignmentType+"Id "+$Assignment.PrincipalId
                $Principal      = Invoke-Expression $Expression
                if ($Assignment.PrincipalType.Value -eq "GROUP") { $Principals[$Assignment.PrincipalId] = $Principal.DisplayName } 
                else { $Principals[$Assignment.PrincipalId] = $Principal.UserName }
            }
            $Assignments += [PSCustomObject]@{
                AccountName     = $Account.Name
                PermissionSet   = $PSsets[$PS]
                Principal       = $Principals[$Assignment.PrincipalId]
                Type            = $Assignment.PrincipalType.Value}
        }
    }
}
$Duration = New-Timespan -Start $Start -End (Get-Date) | ForEach-Object {[Timespan]::New($_.Days, $_.Hours, $_.Minutes, $_.Seconds)}
Write-Host "`r$($AccountList.Count) accounts done in $Duration. Outputting result to $OutputFile"
$Assignments | Sort-Object Account | Export-CSV -Path $OutputFile -Force
```