forked from StartAutomating/PSDevOps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-ADOIdentity.ps1
170 lines (149 loc) · 5.55 KB
/
Get-ADOIdentity.ps1
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
function Get-ADOIdentity
{
<#
.Synopsis
Gets Azure DevOps Identities
.Description
Gets Identities from Azure Devops. Identities can be either users or groups.
.Link
Get-ADOUser
.Link
Get-ADOTeam
.Example
Get-ADOIdentity -Organization StartAutomating -Filter 'GitHub'
.Link
https://docs.microsoft.com/en-us/rest/api/azure/devops/ims/identities/read%20identities
#>
[CmdletBinding(DefaultParameterSetName='identities')]
[OutputType('PSDevOps.Team','PSDevOps.TeamMember')]
param(
# The Organization.
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
[Alias('Org')]
[string]
$Organization,
# A dictionary of Access Control Entries
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('AcesDictionary')]
[PSObject]
$AceDictionary,
# A list of descriptors
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('Members')]
[string[]]
$Descriptors,
# The maximum number of specific descriptors to request in one batch.
[int]
$DescriptorBatchSize = 25,
# If set, will get membership information.
[switch]
$Membership,
# If set, will recursively expand any group memberships discovered.
[switch]
$Recurse,
# The filter used for a query
[string]
$Filter,
# The search type. Can be: AccountName, DisplayName, MailAddress, General, LocalGroupName
[ValidateSet('AccountName','DisplayName','MailAddress','General','LocalGroupName')]
[string]
$SearchType = 'General',
# The api version. By default, 6.0.
# This API does not exist in TFS.
[string]
$ApiVersion = "6.0"
)
dynamicParam { Invoke-ADORestApi -DynamicParameter }
begin {
#region Copy Invoke-ADORestAPI parameters
$invokeParams = Invoke-ADORestApi -MapParameter $PSBoundParameters
#endregion Copy Invoke-ADORestAPI parameters
}
process {
$psParameterSet = $psCmdlet.ParameterSetName
$server = "https://vssps.dev.azure.com"
$uri = # The URI is comprised of:
@(
$server # the Server (minus any trailing slashes),
$Organization # the Organization,
'_apis' # the API Root ('_apis'),
(. $ReplaceRouteParameter $psParameterSet)
# and any parameterized URLs in this parameter set.
) -as [string[]] -ne '' -join '/'
if ($Recurse) { $Membership = $true }
$uri += '?' # The URI has a query string containing:
# We want to decorate our return value. .
$typename = 'Identity' # We just need to drop the 's'
$typeNames = @(
"$organization.$typename"
"PSDevOps.$typename"
)
$invokeParams.Uri = $uri
$invokeParams.PSTypeName = $typeNames
$invokeParams.Property = @{Organization=$Organization}
if ($AceDictionary) {
$Descriptors += $AceDictionary.psobject.Properties | Select-Object -ExpandProperty Name
$null = $psBoundParameters.Remove('AceDictionary')
}
if (-not $Descriptors -and -not $Filter) {
Write-Warning "Get-ADOIdentity must be provided with -Descriptors or -Filter"
return
}
$invokeParamsList = @(
if ($Descriptors -and $Descriptors.Length -gt $DescriptorBatchSize) {
for ($i = 0; $i -lt $Descriptors.Length; $i+=$DescriptorBatchSize) {
$descriptorBatch = $Descriptors[$i..($i + $DescriptorBatchSize - 1)]
}
$invokeCopy = @{} + $invokeParams
$invokeCopy.Uri = $uri + (@(
if ($Descriptors) {
"descriptors=$($descriptorBatch -join ',')"
}
if ($Membership) { "queryMembership=Direct" }
if ($ApiVersion) { # the api-version
"api-version=$apiVersion"
}
) -join '&')
$invokeCopy
} else {
$uri += @(
if ($AceDictionary) {
$Descriptors += $AceDictionary.psobject.Properties | Select-Object -ExpandProperty Name
$null = $psBoundParameters.Remove('AceDictionary')
}
if ($Descriptors) {
"descriptors=$($Descriptors -join ',')"
}
if ($Filter) {
"searchFilter=$SearchType"
"filterValue=$([Web.HttpUtility]::UrlEncode($Filter).Replace('+','%20'))"
}
if ($Membership) {
"queryMembership=Direct"
}
if ($ApiVersion) { # the api-version
"api-version=$apiVersion"
}
) -join '&'
$invokeParams.Uri = $uri
$invokeParams
})
foreach ($ip in $invokeParamsList) {
if (-not $recurse) {
Invoke-ADORestAPI @ip
} else {
$ip.Cache = $true
Invoke-ADORestAPI @ip|
Foreach-Object {
$_
if ($_.Members -and $_.Members.Count) {
$paramCopy = @{} + $psBoundParameters
$paramCopy['Descriptors'] = $_.members
$paramCopy.Remove('Filter')
Get-ADOIdentity @paramCopy
}
}
}
}
}
}