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
|
# aws-orgviz.ps1 ==============================================================
# Visualize AWS Organizations structure.
#
# Usage:
# aws-orgviz.ps1 Show-Policies
# aws-orgviz.ps1 Show-Accounts
# aws-orgviz.ps1 Show-OUs
# aws-orgviz.ps1 Show-OrgTree
#
param(
[Parameter(Position = 0)]
[ValidateSet("Show-Policies", "Show-Accounts", "Show-OUs", "Show-OrgTree")]
[string]$Command
)
# Constants ===================================================================
$C = @{
TopLeft = [char]0x256D
VLine = [char]0x2502
TBranch = [char]0x251C
LBranch = [char]0x2570
HLine = [char]0x2500
DirMark = [char]0x25B8
FileMark = [char]0x2022
}
$ROOT_ID = "r-yh7s"
# Helpers =====================================================================
function Write-Prefix {
param([bool[]]$IsLastStack)
foreach ($last in $IsLastStack) {
if ($last) { Write-Host " " -NoNewline }
else { Write-Host "$($C.VLine) " -NoNewline -ForegroundColor DarkGray }
}
}
function Write-Branch {
param([bool[]]$IsLastStack, [bool]$IsLast, [string]$Label, [string]$Sub)
Write-Prefix -IsLastStack $IsLastStack
$Branch = if ($IsLast) { "$($C.LBranch)$($C.HLine) " } else { "$($C.TBranch)$($C.HLine) " }
Write-Host $Branch -NoNewline -ForegroundColor DarkGray
Write-Host "$($C.DirMark) " -NoNewline
Write-Host "$Label $Sub" -ForegroundColor Green
}
function Write-Leaf {
param([bool[]]$IsLastStack, [bool]$IsLast, [string]$Label, [string]$Sub)
Write-Prefix -IsLastStack $IsLastStack
$Branch = if ($IsLast) { "$($C.LBranch)$($C.HLine) " } else { "$($C.TBranch)$($C.HLine) " }
Write-Host $Branch -NoNewline -ForegroundColor DarkGray
Write-Host "$($C.FileMark) " -NoNewline
Write-Host "$Label $Sub"
}
# Data fetching ===============================================================
function Get-OUs {
param([string]$ParentId)
aws organizations list-organizational-units-for-parent --parent-id $ParentId |
ConvertFrom-Json |
Select-Object -ExpandProperty OrganizationalUnits
}
function Get-AccountsForParent {
param([string]$ParentId)
aws organizations list-accounts-for-parent --parent-id $ParentId |
ConvertFrom-Json |
Select-Object -ExpandProperty Accounts
}
function Build-OrgTree {
param([string]$ParentId)
$nodes = @()
$ous = @(Get-OUs -ParentId $ParentId)
$accts = @(Get-AccountsForParent -ParentId $ParentId)
foreach ($ou in $ous) {
$nodes += [PSCustomObject]@{
Type = "OU"
Name = $ou.Name
Id = $ou.Id
Children = Build-OrgTree -ParentId $ou.Id
}
}
foreach ($acct in $accts) {
$nodes += [PSCustomObject]@{
Type = "Account"
Name = $acct.Name
Id = $acct.Id
Children = @()
}
}
return $nodes
}
# Tree rendering ==============================================================
function Render-Node {
param([PSCustomObject]$Node, [bool[]]$IsLastStack, [bool]$IsLast)
if ($Node.Type -eq "OU") {
Write-Branch -IsLastStack $IsLastStack -IsLast $IsLast -Label $Node.Name -Sub "[$($Node.Id)]"
$children = @($Node.Children)
for ($i = 0; $i -lt $children.Count; $i++) {
Render-Node -Node $children[$i] -IsLastStack ($IsLastStack + $IsLast) -IsLast ($i -eq ($children.Count - 1))
}
}
else {
Write-Leaf -IsLastStack $IsLastStack -IsLast $IsLast -Label $Node.Name -Sub "[$($Node.Id)]"
}
}
# Commands ====================================================================
function Show-Policies {
aws organizations list-policies --filter SERVICE_CONTROL_POLICY |
ConvertFrom-Json |
Select-Object -ExpandProperty Policies |
Where-Object { -not $_.AwsManaged } |
Format-List
}
function Show-Accounts {
aws organizations list-accounts |
ConvertFrom-Json |
Select-Object -ExpandProperty Accounts |
Format-List
}
function Show-OUs {
$l1 = @(Get-OUs -ParentId $ROOT_ID)
foreach ($ou in $l1) {
$ou | Format-List
$l2 = @(Get-OUs -ParentId $ou.Id)
foreach ($child in $l2) {
$child | Format-List
}
}
}
function Show-OrgTree {
Write-Host "Fetching organization data..." -ForegroundColor DarkGray
$tree = @(Build-OrgTree -ParentId $ROOT_ID)
[Console]::SetCursorPosition(0, [Console]::CursorTop - 1)
[Console]::Write(" " * [Console]::WindowWidth)
[Console]::SetCursorPosition(0, [Console]::CursorTop)
Write-Host "$($C.TopLeft)$($C.HLine) " -NoNewline -ForegroundColor DarkGray
Write-Host "AWS Organization " -NoNewline -ForegroundColor Blue
Write-Host "[$ROOT_ID]"
Write-Host $C.VLine -ForegroundColor DarkGray
for ($i = 0; $i -lt $tree.Count; $i++) {
Render-Node -Node $tree[$i] -IsLastStack @() -IsLast ($i -eq ($tree.Count - 1))
}
}
# Dispatch ====================================================================
if (-not $Command) {
Write-Host "Usage: aws-orgviz.ps1 <command>" -ForegroundColor DarkGray
Write-Host ""
Write-Host "Available commands:"
Write-Host " Show-Policies Prints a list of all policies"
Write-Host " Show-Accounts Prints a list of all accounts"
Write-Host " Show-OUs Prints a list of all organizational units"
Write-Host " Show-OrgTree Prints a tree view of the organizational structure"
Write-Host ""
Write-Host "Example: aws-orgviz.ps1 Show-Policies"
Write-Host ""
return
}
switch ($Command) {
"Show-Policies" { Show-Policies }
"Show-Accounts" { Show-Accounts }
"Show-OUs" { Show-OUs }
"Show-OrgTree" { Show-OrgTree }
}
|