-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGather-CdmInformation.ps1
More file actions
448 lines (361 loc) · 16.8 KB
/
Gather-CdmInformation.ps1
File metadata and controls
448 lines (361 loc) · 16.8 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<# Gather-CdmInformation.ps1
##
## Version: 1.1.00 BN 002 (04/19/2021) Added recursive zone processing
## 1.0.00 BN 001 (10/29/2020) Initial Release
##
#>
<#
.SYNOPSIS
This script collects Centrify Role Assignment information from a Centrify Zone.
.DESCRIPTION
This script collections all 3 different types of Role Assignments within a Centrify Zone:
Zone-Wide Assignments
Computer Role Assignments
Computer-specific Assignments
In addition, it bundles information about the relevant Role Definition used with that
Role Assignment, including the Command Rights used as part of that Role Definition.
The end result is a variable (or .xml file) that can be used to search for Role
Assignment information more effectively.
This script requires PowerShell version 5.1 and the Centrify DirectControl PowerShell
module.
.PARAMETER Zone
This is the simple name (e.g. "Global") of the zone to collection Role Assignment information.
A Distinguished Name can be used instead for the Zone object in the event there are multiple
zones with the same name.
.PARAMETER Version
Show version of this script.
.PARAMETER Help
Show usage for this script.
.INPUTS
None. You can't redirect or pipe input to this script.
.OUTPUTS
This script output two files in the same directory that the script is executed.
.EXAMPLE
C:\PS> .\Gather-CdmInformation.ps1 -Zone Global
This script collects Role Assignments, Role Defintions, and Command Right information from
the Global zone.
.EXAMPLE
C:\PS> .\Gather-CdmInformation.ps1 - Zone "CN=Unix,CN=Zones,OU=Centrify,DC=domain,DC=com"
This script collects Role Assignments, Role Defintions, and Command Right information from
the Unix zone. The Distinguished Name is used here to be explicit which zone is to be used.
This is for situations when where there are multiple zones that have the same name.
.EXAMPLE
C:\PS> .\Gather-CdmInformation.ps1 -Version
Displays the current version of the script.
.EXAMPLE
C:\PS> .\Gather-CdmInformation.ps1 -Help
Displays what you are seeing now.
#>
#######################################
#region ### PARAMETERS ################
#######################################
[CmdletBinding(DefaultParameterSetName="Default")]
Param
(
#region ### General Parameters ###
### Validators ###
#[ValidateScript({If (-Not (Test-Path -Path ($_))) {Throw "The specified file does not exist. Please enter the name of the file."} Else { $true } })]
#[ValidateNotNullOrEmpty()]
[Parameter(Mandatory = $true, HelpMessage ="The target Centrify Zone to begin analyzing.")]
[ValidateNotNullOrEmpty()]
[System.String]$Zone,
[Parameter(Mandatory = $true, HelpMessage = "Display the version of the script.", ParameterSetName="Version")]
[Alias("v")]
[Switch]$Version,
[Parameter(Mandatory = $true, HelpMessage = "Display extra help.", ParameterSetName="Help")]
[Alias("?")]
[Alias("h")]
[Switch]$Help
#endregion
)# Param
#######################################
#endregion ############################
#######################################
#######################################
#region ### CONFIG VARIABLES ##########
#######################################
#######################################
#endregion ############################
#######################################
#######################################
#region ### VERSION NUMBER and HELP ###
#######################################
[System.String]$VersionNumber = (Get-Content ($MyInvocation.MyCommand).Name)[2]
# print the version number if -Version was used and exit
if ($Version.IsPresent)
{
Write-Host ("{0} ({1})`n" -f ($MyInvocation.MyCommand).Name,$VersionNumber)
Exit 0 # EXITCODE 0 : Successful execution
}
#
if ($Help.IsPresent)
{
Invoke-Expression -Command ("Get-Help .\{0} -Full" -f ($MyInvocation.MyCommand).Name)
Exit 0 # EXITCODE 0 : Successful execution
}
#######################################
#endregion ############################
#######################################
#######################################
#region ### FUNCTIONS #################
#######################################
###########
#region ### Check-Module # Checks for and imports the necessary PowerShell modules.
function Check-Module
{
Param
(
[Parameter(Position = 0, HelpMessage="The module to check.")]
[System.String]$Module
)# Param
Write-Verbose ("-------------------")
Write-Verbose ("Check-Module called")
Write-Verbose ("-------------------")
# if the module hasn't already been imported
if (-Not (Get-Module -ListAvailable).Name.Contains($Module))
{
# if the module is available
if ((Get-Module -ListAvailable).Name.Contains($Module))
{
# try importing it
Try
{
Import-Module -Name $Module
}
Catch
{
Write-Error ($_.Exception)
Exit 2 # EXITCODE 2 : Unknown error while importing required module.
}
}# f ((Get-Module -ListAvailable).Name.Contains($Module))
else
{
Write-Error ("Required Module [{0}] was not found." -f $Module)
Exit 1 # EXITCODE 1 : Required module not found.
}
}# if (-Not (Get-Module).Contains($Module))
}# function Check-Module
#endregion
###########
###########
#region ### TEMPLATE # TEMPLATE
#function TEMPLATE
#{
#}# function TEMPLATE
#endregion
###########
#######################################
#endregion ############################
#######################################
#######################################
#region ### PREPIFY ###################
#######################################
# Add the required modules
Check-Module -Module Centrify.DirectControl.PowerShell
Check-Module -Module ActiveDirectory
# including the library.ps1 file
. .\library.ps1
# checking to ensure the zone exists
# if the Zone is using the Distinguished Name format
if ($Zone -like "CN=*")
{
# if the Zone is not found using the Distinguished Name format
if (-Not ($TargetZone = Get-CdmZone -Dn $Zone))
{
Write-Host ("The Target Zone was not found using the Distinguished Name.")
Write-Host ("Zone : [{0}]" -f $Zone)
Exit 3 # EXITCODE 3 : Target Zone not found.
}
}# if ($Zone -like "CN=*")
else # the Zone is using the common name
{
if ($TargetZone = Get-CdmZone -Name $Zone)
{
# if more than one Zone was found
if ($TargetZone.Count -gt 1)
{
Write-Warning ("Multiple Zones exist with the Target Name [{0}]." -f $Zone)
Write-Host ("Use the Distinguished Name format instead.")
foreach ($z in $TargetZone)
{
Write-Host ("Zone : [{0}]" -f $z.Name)
Write-Host ("DN : [{0}]" -f $z.DistinguishedName)
}
Exit 4 # EXITCODE 4 : Multiple Target Zones found.
}# if ($TargetZone.Count -gt 1)
}# if ($TargetZone = Get-CdmZone -Name $Zone)
else
{
#Zone not found
Write-Host ("The Target Zone was not found using the common name.")
Write-Host ("Zone : [{0}]" -f $Zone)
Exit 3 # EXITCODE 3 : Target Zone not found.
}
}# else # the Zone is using the common name
# now getting all the zones that need to be processed
$Zones = Get-CdmZone | Where-Object {$_.Name -eq $TargetZone.Name -or $_.Parent -like ("*{0}" -f $TargetZone.DistinguishedName)} | Sort-Object CanonicalName
#######################################
#endregion ############################
#######################################
#######################################
#region ### MAIN ######################
#######################################
# ArrayLists for storing all our information
[System.Collections.ArrayList] $AllCentrifyRoles = @()
[System.Collections.ArrayList] $AllCentrifyAssignments = @()
foreach ($TargetZone in $Zones)
{
### ZONE ROLES ###
Write-Host ("Gathering Roles in Centrify Zone [{0}]" -f $TargetZone.Name) -ForegroundColor Yellow
# getting all the roles in the target zone
$Roles = Get-CdmRole -Zone $TargetZone
# making a copy of those CdmRole objects to add new properties
$CentrifyRoles = $Roles.PSObject.Copy()
# adding additional property fields, no data yet though
$CentrifyRoles | Add-Member -MemberType NoteProperty -Name CdmCommandRight -Value CdmCommandRight
$CentrifyRoles | Add-Member -MemberType NoteProperty -Name CdmPamRight -Value CdmPamRight
$CentrifyRoles | Add-Member -MemberType NoteProperty -Name CdmApplicationRight -Value CdmApplicationRight
# for each role we gathered
foreach ($role in $CentrifyRoles)
{
# add the command rights and pam rights to our custom role object
$role.CdmCommandRight = Get-CdmCommandRight -Role $role
$role.CdmPamRight = Get-CdmPamRight -Role $role
$role.CdmApplicationRight = Get-CdmApplicationRight -Role $role
$AllCentrifyRoles.Add($role) | Out-Null
}# foreach ($role in $CentrifyRoles)
### ZONE ROLES ASSIGNMENTS ###
Write-Host ("Working on Zone-Wide Role Assignments for Zone [{0}]" -f $TargetZone.Name) -ForegroundColor Yellow
# Getting all zone-wide role assignments
$ZoneAssignments = Get-CdmRoleAssignment -Zone $TargetZone
if ($ZoneAssignments.Count -gt 0)
{
# making a copy of those CdmRoleAssignment objects to add new properties
$TmpAssignments = $ZoneAssignments.PSObject.Copy()
# adding additional property fields, no data yet though except for the Zone name
$TmpAssignments | Add-Member -MemberType NoteProperty -Name CdmRoleAssignmentType -Value CdmRoleAssignmentType
$TmpAssignments | Add-Member -MemberType NoteProperty -Name CdmRoleAssignment -Value CdmRoleAssignment
$TmpAssignments | Add-Member -MemberType NoteProperty -Name TrusteeName -Value TrusteeName
$TmpAssignments | Add-Member -MemberType NoteProperty -Name AssignmentTarget -Value $TargetZone.Name
# for each assignment we found
foreach ($assignment in $TmpAssignments)
{
# setting the role name and the distinguished name of the zone
$rolename = $assignment.Role.Name
$rolezone = $assignment.Role.Zone.DistinguishedName
# our CdmRoleAssignmentType is Zone
$assignment.CdmRoleAssignmentType = "Zone"
# setting the CdmRole object into our custom object
$assignment.CdmRoleAssignment = $CentrifyRoles | Where-Object {$_.Name -eq $rolename -and $_.Zone.DistinguishedName -eq $rolezone}
# setting our TrusteeName based on which trustee is used
if ($assignment.AdTrustee -eq $null)
{
$assignment.TrusteeName = $assignment.LocalTrustee
}
else
{
$assignment.TrusteeName = $assignment.AdTrustee
}
# adding it to our assignments
$AllCentrifyAssignments.Add($assignment) | Out-Null
}# foreach ($assignment in $TmpAssignments)
}# if ($ZoneAssignments.Count -gt 0)
else
{
Write-Host ("No zone wide assignments found in Zone [{0}]" -f $TargetZone.Name) -ForegroundColor Yellow
}
### COMPUTER ROLES ###
Write-Host ("Working on Computer-Role Role Assignments in Zone [{0}]" -f $TargetZone.Name) -ForegroundColor Yellow
# Getting all computer roles
$ComputerRoles = Get-CdmComputerRole -Zone $TargetZone
if ($ComputerRoles.Count -gt 0)
{
# for each computer role we found
foreach ($computerrole in $ComputerRoles)
{
# for each role assignment we found in each computer role
foreach ($crroleassignment in (Get-CdmRoleAssignment -ComputerRole $computerrole))
{
# setting additional properties
$rolename = $crroleassignment.Role.Name
$rolezone = $crroleassignment.Role.Zone.DistinguishedName
$cdmrole = $CentrifyRoles | Where-Object {$_.Name -eq $rolename -and $_.Zone.DistinguishedName -eq $rolezone}
$computerrolename = $tmproleassignment.ComputerRole.Name
# making a copy of each CdmRoleAssignment object to add new properties
$tmproleassignment = $crroleassignment.PSObject.Copy()
$tmproleassignment | Add-Member -MemberType NoteProperty -Name CdmRoleAssignmentType -Value "ComputerRole"
$tmproleassignment | Add-Member -MemberType NoteProperty -Name CdmRoleAssignment -Value $cdmrole
$tmproleassignment | Add-Member -MemberType NoteProperty -Name TrusteeName -Value TrusteeName
$tmproleassignment | Add-Member -MemberType NoteProperty -Name AssignmentTarget -Value $computerrolename
# setting the TrusteeName based on which one was used
if ($tmproleassignment.AdTrustee -eq $null)
{
$tmproleassignment.TrusteeName = $tmproleassignment.LocalTrustee
}
else
{
$tmproleassignment.TrusteeName = $tmproleassignment.AdTrustee
}
# adding it to our assignments
$AllCentrifyAssignments.Add($tmproleassignment) | Out-Null
}# foreach ($crroleassignment in (Get-CdmRoleAssignment -ComputerRole $computerrole))
}# foreach ($computerrole in $ComputerRoles)
}# if ($ComputerRoles.Count -gt 0)
else
{
Write-Host ("No computer roles found in Zone [{0}]" -f $TargetZone.Name) -ForegroundColor Yellow
}
### COMPUTERS ###
Write-Host ("Working on Computer-Specific Role Assignments in Zone [{0}]" -f $TargetZone.Name) -ForegroundColor Yellow
# Getting all computer joined to our Target Zone
$Computers = Get-CdmManagedComputer -Zone $TargetZone
if ($Computers.Count -gt 0)
{
# for each computer we found
foreach ($computer in $Computers)
{
# and for each role assignment found on that computer
foreach ($cmproleassignment in (Get-CdmRoleAssignment -Computer $computer))
{
# setting additional properties
$rolename = $cmproleassignment.Role.Name
$rolezone = $cmproleassignment.Role.Zone.DistinguishedName
$cdmrole = $CentrifyRoles | Where-Object {$_.Name -eq $rolename -and $_.Zone.DistinguishedName -eq $rolezone}
$computername = $tmpcmproleassignment.Computer.Name
# making a copy of each CdmRoleAssignment object to add new properties
$tmpcmproleassignment = $cmproleassignment.PSObject.Copy()
$tmpcmproleassignment | Add-Member -MemberType NoteProperty -Name CdmRoleAssignmentType -Value "Computer"
$tmpcmproleassignment | Add-Member -MemberType NoteProperty -Name CdmRoleAssignment -Value $cdmrole
$tmpcmproleassignment | Add-Member -MemberType NoteProperty -Name TrusteeName -Value TrusteeName
$tmpcmproleassignment | Add-Member -MemberType NoteProperty -Name AssignmentTarget -Value $computername
# setting the TrusteeName based on which one was used
if ($tmpcmproleassignment.AdTrustee -eq $null)
{
$tmpcmproleassignment.TrusteeName = $tmpcmproleassignment.LocalTrustee
}
else
{
$tmpcmproleassignment.TrusteeName = $tmpcmproleassignment.AdTrustee
}
# adding it to our assignments
$AllCentrifyAssignments.Add($tmpcmproleassignment) | Out-Null
}# foreach ($cmproleassignment in (Get-CdmRoleAssignment -Computer $computer))
}# foreach ($computer in $Computers)
}# if ($Computers.Count -gt 0)
else
{
Write-Host ("No computers found in Zone [{0}]" -f $TargetZone.Name) -ForegroundColor Yellow
}
}# foreach ($TargetZone in $Zones)
# export the custom role objects as an .xml file that can be used later
$AllCentrifyRoles | Export-Clixml .\AllCentrifyRoles.xml
# setting this as a global variable
$global:AllCentrifyRoles = $AllCentrifyRoles
# export the custom role assignment objects as an .xml file that can be used later
$AllCentrifyAssignments | Export-Clixml .\AllCentrifyAssignments.xml
# setting this as a global variable
$global:AllCentrifyAssignments = $AllCentrifyAssignments
Exit 0 # EXITCODE 0 : Successful execution
#######################################
#endregion ############################
#######################################