1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
# Walks AWS Organizations tree (Root -> OUs -> Accounts) and dumps SCP attachments.
# Uses whatever AWS CLI credentials/profile are currently active in your shell.
# Read-only. Run from the management account (or Organizations delegated admin).
[CmdletBinding()]
param(
[string]$OutputDir = "."
)
$ErrorActionPreference = "Stop"
function Invoke-AwsJson {
param([string[]]$CliArgs)
$raw = & aws @CliArgs 2>&1
if ($LASTEXITCODE -ne 0) {
throw "aws $($CliArgs -join ' ') failed:`n$raw"
}
return $raw | ConvertFrom-Json
}
function Get-AttachedScps {
param([string]$TargetId)
$result = Invoke-AwsJson -CliArgs @(
"organizations", "list-policies-for-target",
"--target-id", $TargetId,
"--filter", "SERVICE_CONTROL_POLICY",
"--output", "json"
)
$policies = @($result.Policies | ForEach-Object {
[PSCustomObject]@{
Id = $_.Id
Arn = $_.Arn
Name = $_.Name
Description = $_.Description
AwsManaged = $_.AwsManaged
}
})
return , $policies
}
function Get-OuNode {
param([string]$ParentId, [string]$OuId, [string]$OuName)
Write-Host "Scanning OU: $OuName ($OuId)" -ForegroundColor Cyan
$scps = Get-AttachedScps -TargetId $OuId
$accountsRaw = Invoke-AwsJson -CliArgs @(
"organizations", "list-accounts-for-parent",
"--parent-id", $OuId, "--output", "json"
)
$accounts = @()
foreach ($acct in $accountsRaw.Accounts) {
Write-Host " Scanning Account: $($acct.Name) ($($acct.Id))" -ForegroundColor DarkCyan
$acctScps = Get-AttachedScps -TargetId $acct.Id
$accounts += [PSCustomObject]@{
Id = $acct.Id
Name = $acct.Name
Email = $acct.Email
Status = $acct.Status
AttachedSCPs = $acctScps
SCPCount = @($acctScps).Count
}
}
$childOusRaw = Invoke-AwsJson -CliArgs @(
"organizations", "list-organizational-units-for-parent",
"--parent-id", $OuId, "--output", "json"
)
$childOus = @()
foreach ($childOu in $childOusRaw.OrganizationalUnits) {
$childOus += Get-OuNode -ParentId $OuId -OuId $childOu.Id -OuName $childOu.Name
}
return [PSCustomObject]@{
Id = $OuId
Name = $OuName
ParentId = $ParentId
AttachedSCPs = $scps
SCPCount = @($scps).Count
Accounts = $accounts
ChildOUs = $childOus
}
}
# ---- main ----
Write-Host "`nFetching AWS Organizations root..." -ForegroundColor Yellow
$rootsRaw = Invoke-AwsJson -CliArgs @("organizations", "list-roots", "--output", "json")
$root = $rootsRaw.Roots[0]
Write-Host "Root: $($root.Name) ($($root.Id))" -ForegroundColor Green
$rootScps = Get-AttachedScps -TargetId $root.Id
$rootAccountsRaw = Invoke-AwsJson -CliArgs @(
"organizations", "list-accounts-for-parent",
"--parent-id", $root.Id, "--output", "json"
)
$rootAccounts = @()
foreach ($acct in $rootAccountsRaw.Accounts) {
Write-Host " Scanning Account (direct under root): $($acct.Name) ($($acct.Id))" -ForegroundColor DarkCyan
$acctScps = Get-AttachedScps -TargetId $acct.Id
$rootAccounts += [PSCustomObject]@{
Id = $acct.Id
Name = $acct.Name
Email = $acct.Email
Status = $acct.Status
AttachedSCPs = $acctScps
SCPCount = @($acctScps).Count
}
}
$topLevelOusRaw = Invoke-AwsJson -CliArgs @(
"organizations", "list-organizational-units-for-parent",
"--parent-id", $root.Id, "--output", "json"
)
$childOus = @()
foreach ($ou in $topLevelOusRaw.OrganizationalUnits) {
$childOus += Get-OuNode -ParentId $root.Id -OuId $ou.Id -OuName $ou.Name
}
Write-Host "`nFetching full SCP catalog (attached + unattached)..." -ForegroundColor Yellow
$allScpsRaw = Invoke-AwsJson -CliArgs @(
"organizations", "list-policies",
"--filter", "SERVICE_CONTROL_POLICY", "--output", "json"
)
$allScps = @($allScpsRaw.Policies | ForEach-Object {
[PSCustomObject]@{
Id = $_.Id
Arn = $_.Arn
Name = $_.Name
Description = $_.Description
AwsManaged = $_.AwsManaged
}
})
$tree = [PSCustomObject]@{
GeneratedAtUtc = (Get-Date).ToUniversalTime().ToString("o")
Root = [PSCustomObject]@{
Id = $root.Id
Name = $root.Name
AttachedSCPs = $rootScps
SCPCount = @($rootScps).Count
Accounts = $rootAccounts
ChildOUs = $childOus
}
AllOrgScps = $allScps
AllOrgScpCount = @($allScps).Count
}
$timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
if (-not (Test-Path $OutputDir)) { New-Item -ItemType Directory -Path $OutputDir | Out-Null }
$jsonPath = Join-Path $OutputDir "org-structure-$timestamp.json"
$tree | ConvertTo-Json -Depth 20 | Out-File -FilePath $jsonPath -Encoding utf8
Write-Host "`nJSON written to: $jsonPath" -ForegroundColor Green
$txtPath = Join-Path $OutputDir "org-structure-$timestamp.txt"
$lines = [System.Collections.Generic.List[string]]::new()
function Add-OuLines {
param($Node, [int]$Indent)
$pad = " " * $Indent
$lines.Add("$pad OU: $($Node.Name) [$($Node.Id)] -- SCPs attached: $($Node.SCPCount)/10")
foreach ($scp in $Node.AttachedSCPs) {
$tag = if ($scp.AwsManaged) { "AWS-managed" } else { "custom" }
$lines.Add("$pad - $($scp.Name) [$($scp.Id)] ($tag)")
}
foreach ($acct in $Node.Accounts) {
$lines.Add("$pad Account: $($acct.Name) [$($acct.Id)] ($($acct.Status)) -- SCPs attached: $($acct.SCPCount)/10")
foreach ($scp in $acct.AttachedSCPs) {
$tag = if ($scp.AwsManaged) { "AWS-managed" } else { "custom" }
$lines.Add("$pad - $($scp.Name) [$($scp.Id)] ($tag)")
}
}
foreach ($child in $Node.ChildOUs) {
Add-OuLines -Node $child -Indent ($Indent + 1)
}
}
$lines.Add("AWS Organization Structure & SCP Attachments")
$lines.Add("Generated: $($tree.GeneratedAtUtc)")
$lines.Add("=" * 60)
$lines.Add("")
$lines.Add("ROOT: $($tree.Root.Name) [$($tree.Root.Id)] -- SCPs attached: $($tree.Root.SCPCount)/10")
foreach ($scp in $tree.Root.AttachedSCPs) {
$tag = if ($scp.AwsManaged) { "AWS-managed" } else { "custom" }
$lines.Add(" - $($scp.Name) [$($scp.Id)] ($tag)")
}
foreach ($acct in $tree.Root.Accounts) {
$lines.Add(" Account (direct under root): $($acct.Name) [$($acct.Id)] ($($acct.Status)) -- SCPs attached: $($acct.SCPCount)/10")
foreach ($scp in $acct.AttachedSCPs) {
$tag = if ($scp.AwsManaged) { "AWS-managed" } else { "custom" }
$lines.Add(" - $($scp.Name) [$($scp.Id)] ($tag)")
}
}
$lines.Add("")
foreach ($ou in $tree.Root.ChildOUs) {
Add-OuLines -Node $ou -Indent 1
$lines.Add("")
}
$lines.Add("=" * 60)
$lines.Add("ALL SCPs IN ORG (attached or not): $($tree.AllOrgScpCount)")
foreach ($scp in $tree.AllOrgScps) {
$tag = if ($scp.AwsManaged) { "AWS-managed" } else { "custom" }
$lines.Add(" - $($scp.Name) [$($scp.Id)] ($tag) :: $($scp.Description)")
}
$lines | Out-File -FilePath $txtPath -Encoding utf8
Write-Host "Text tree written to: $txtPath" -ForegroundColor Green
|