Skip to content

Commit c6ec5e0

Browse files
author
Jason Shen (KATAL)
committed
Refine Portal GDPR command
1 parent fa726bc commit c6ec5e0

File tree

1 file changed

+230
-46
lines changed

1 file changed

+230
-46
lines changed

DatacenterIntegration/Portal/PortalUserDataGdprUtilities.psm1

Lines changed: 230 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66

77
$DefaultAdminSubscriptionName = "Default Provider Subscription"
88

9-
<#
10-
.Synopsis
11-
Clear the portal user data
12-
#>
13-
function Clear-AzsUserData
9+
function Initialize-UserDataClearEnv
1410
{
1511
param
1612
(
@@ -24,21 +20,13 @@ function Clear-AzsUserData
2420
[ValidateNotNullOrEmpty()]
2521
[Uri] $AzsAdminArmEndpoint,
2622

27-
# The user principal name of the account who's user data should be cleared.
28-
[Parameter(Mandatory=$true)]
29-
[ValidateNotNullOrEmpty()]
30-
[string] $UserPrincipalName,
23+
# Optional: A credential used to authenticate with Azure Stack. Must support a non-interactive authentication flow. If not provided, the script will prompt for user credentials.
24+
[pscredential] $AutomationCredential = $null,
3125

32-
# Optional: The directory tenant identifier of account who's user data should be cleared.
33-
# If it is not specified, it will delete all the
34-
[Parameter(Mandatory=$false)]
3526
[ValidateNotNullOrEmpty()]
36-
[string] $DirectoryTenantId,
37-
38-
# Optional: A credential used to authenticate with Azure Stack. Must support a non-interactive authentication flow. If not provided, the script will prompt for user credentials.
39-
[ValidateNotNull()]
40-
[pscredential] $AutomationCredential = $null
27+
[string] $UserPrincipalName
4128
)
29+
4230
#requires -Version 4.0
4331
#requires -Module "AzureRM.Profile"
4432
#requires -Module "Azs.Subscriptions.Admin"
@@ -71,58 +59,155 @@ function Clear-AzsUserData
7159
$refreshToken = Initialize-AzureRmUserRefreshToken @params
7260
Write-Verbose "Login into admin ARM and got the refresh token." -Verbose
7361

74-
$adminSubscriptionId = (Get-AzureRmSubscription -Verbose | where { $_.Name -ieq $DefaultAdminSubscriptionName }).Id
75-
Write-Verbose "Get default Admin subscription id $adminSubscriptionId." -Verbose
62+
$script:adminSubscriptionId = (Get-AzureRmSubscription -Verbose | where { $_.Name -ieq $DefaultAdminSubscriptionName }).Id
63+
Write-Verbose "Get default Admin subscription id $script:adminSubscriptionId." -Verbose
7664

77-
if ($DirectoryTenantId)
78-
{
79-
$directoryTenantIdsArray = [string[]]$DirectoryTenantId
80-
}
81-
else
82-
{
83-
Write-Verbose "Input parameter 'DirectoryTenantId' is empty. Retrieving all the registered tenant directory..." -Verbose
84-
$directoryTenantIdsArray = (Get-AzsDirectoryTenant -Verbose).TenantId
85-
}
86-
87-
Write-Host "Clearing the user data with input user principal name $UserPrincipalName and directory tenants '$DirectoryTenantIdsArray'..."
8865

89-
$clearUserDataResults = @() # key is directory Id, value is clear response
90-
91-
$initializeGraphEnvParams = @{
66+
$script:initializeGraphEnvParams = @{
9267
RefreshToken = $refreshToken
9368
}
9469
if ($adminArmEnv.EnableAdfsAuthentication)
9570
{
96-
$initializeGraphEnvParams.AdfsFqdn = (New-Object Uri $adminArmEnv.ActiveDirectoryAuthority).Host
97-
$initializeGraphEnvParams.GraphFqdn = (New-Object Uri $adminArmEnv.GraphUrl).Host
71+
$script:initializeGraphEnvParams.AdfsFqdn = (New-Object Uri $adminArmEnv.ActiveDirectoryAuthority).Host
72+
$script:initializeGraphEnvParams.GraphFqdn = (New-Object Uri $adminArmEnv.GraphUrl).Host
9873

99-
$QueryParameters = @{
74+
$script:queryParameters = @{
10075
'$filter' = "userPrincipalName eq '$($UserPrincipalName.ToLower())'"
10176
}
10277
}
10378
else
10479
{
10580
$graphEnvironment = Resolve-GraphEnvironment -AzureEnvironment $adminArmEnv
10681
Write-Verbose "Resolve the graph env as '$graphEnvironment '" -Verbose
107-
$initializeGraphEnvParams.Environment = $graphEnvironment
82+
$script:initializeGraphEnvParams.Environment = $graphEnvironment
10883

109-
$QueryParameters = @{
84+
$script:queryParameters = @{
11085
'$filter' = "userPrincipalName eq '$($UserPrincipalName.ToLower())' or startswith(userPrincipalName, '$($UserPrincipalName.Replace("@", "_").ToLower() + "#")')"
11186
}
11287
}
11388

114-
Write-Verbose "Retrieving access token..." -Verbose
115-
Initialize-GraphEnvironment @initializeGraphEnvParams -DirectoryTenantId $AzsAdminDirectoryTenantId
116-
$accessToken = (Get-GraphToken -Resource $adminArmEnv.ActiveDirectoryServiceEndpointResourceId -UseEnvironmentData).access_token
89+
Initialize-GraphEnvironment @script:initializeGraphEnvParams -DirectoryTenantId $AzsAdminDirectoryTenantId
90+
$script:adminArmAccessToken = (Get-GraphToken -Resource $adminArmEnv.ActiveDirectoryServiceEndpointResourceId -UseEnvironmentData).access_token
91+
}
92+
93+
<#
94+
.Synopsis
95+
Clear the portal user data
96+
#>
97+
function Clear-AzsUserDataWithUserPrincipalName
98+
{
99+
param
100+
(
101+
# The directory tenant identifier of Azure Stack Administrator.
102+
[Parameter(Mandatory=$true)]
103+
[ValidateNotNullOrEmpty()]
104+
[string] $AzsAdminDirectoryTenantId,
105+
106+
# The Azure Stack ARM endpoint URI.
107+
[Parameter(Mandatory=$true)]
108+
[ValidateNotNullOrEmpty()]
109+
[Uri] $AzsAdminArmEndpoint,
110+
111+
# The user principal name of the account whoes user data should be cleared.
112+
[Parameter(Mandatory=$true)]
113+
[ValidateNotNullOrEmpty()]
114+
[string] $UserPrincipalName,
115+
116+
# Optional: The directory tenant identifier of account whoes user data should be cleared.
117+
# If it is not specified, it will delete user with principal name under all regitered directory tenants
118+
[Parameter(Mandatory=$false)]
119+
[ValidateNotNullOrEmpty()]
120+
[string] $DirectoryTenantId,
121+
122+
# Optional: A credential used to authenticate with Azure Stack. Must support a non-interactive authentication flow. If not provided, the script will prompt for user credentials.
123+
[ValidateNotNull()]
124+
[pscredential] $AutomationCredential = $null
125+
)
126+
127+
$params = @{
128+
AzsAdminDirectoryTenantId = $AzsAdminDirectoryTenantId
129+
AzsAdminArmEndpoint = $AzsAdminArmEndpoint
130+
UserPrincipalName = $UserPrincipalName
131+
}
132+
133+
if ($DirectoryTenantId) {
134+
$params.DirectoryTenantId = $DirectoryTenantId
135+
}
136+
137+
if ($AutomationCredential) {
138+
$params.AutomationCredential = $AutomationCredential
139+
}
140+
141+
Clear-AzsUserData @params
142+
}
143+
144+
<#
145+
.Synopsis
146+
Deprecated: Clear the portal user data
147+
#>
148+
function Clear-AzsUserData
149+
{
150+
param
151+
(
152+
# The directory tenant identifier of Azure Stack Administrator.
153+
[Parameter(Mandatory=$true)]
154+
[ValidateNotNullOrEmpty()]
155+
[string] $AzsAdminDirectoryTenantId,
156+
157+
# The Azure Stack ARM endpoint URI.
158+
[Parameter(Mandatory=$true)]
159+
[ValidateNotNullOrEmpty()]
160+
[Uri] $AzsAdminArmEndpoint,
161+
162+
# The user principal name of the account whoes user data should be cleared.
163+
[Parameter(Mandatory=$true)]
164+
[ValidateNotNullOrEmpty()]
165+
[string] $UserPrincipalName,
166+
167+
# Optional: The directory tenant identifier of account whoes user data should be cleared.
168+
# If it is not specified, it will delete user with principal name under all regitered directory tenants
169+
[Parameter(Mandatory=$false)]
170+
[ValidateNotNullOrEmpty()]
171+
[string] $DirectoryTenantId,
172+
173+
# Optional: A credential used to authenticate with Azure Stack. Must support a non-interactive authentication flow. If not provided, the script will prompt for user credentials.
174+
[ValidateNotNull()]
175+
[pscredential] $AutomationCredential = $null
176+
)
177+
178+
$ErrorActionPreference = 'Stop'
179+
$VerbosePreference = 'Continue'
180+
181+
$params = @{
182+
AzsAdminDirectoryTenantId = $AzsAdminDirectoryTenantId
183+
AzsAdminArmEndpoint = $AzsAdminArmEndpoint
184+
AutomationCredential = $AutomationCredential
185+
UserPrincipalName = $UserPrincipalName
186+
}
187+
Initialize-UserDataClearEnv @params
188+
189+
if ($DirectoryTenantId)
190+
{
191+
$directoryTenantIdsArray = [string[]]$DirectoryTenantId
192+
}
193+
else
194+
{
195+
Write-Verbose "Input parameter 'DirectoryTenantId' is empty. Retrieving all the registered tenant directory..." -Verbose
196+
$directoryTenantIdsArray = (Get-AzsDirectoryTenant -Verbose).TenantId
197+
}
198+
199+
Write-Host "Clearing the user data with input user principal name $UserPrincipalName and directory tenants '$DirectoryTenantIdsArray'..."
200+
201+
$clearUserDataResults = @() # key is directory Id, value is clear response
117202

118203
foreach ($dirId in $directoryTenantIdsArray)
119204
{
120205
Write-Verbose "Intializing graph env..." -Verbose
121-
Initialize-GraphEnvironment @initializeGraphEnvParams -DirectoryTenantId $dirId
206+
Initialize-GraphEnvironment @script:initializeGraphEnvParams -DirectoryTenantId $dirId
122207
Write-Verbose "Intialized graph env" -Verbose
123208

124209
Write-Verbose "Querying all users..." -Verbose
125-
$usersResponse = Invoke-GraphApi -ApiPath "/users" -QueryParameters $QueryParameters
210+
$usersResponse = Invoke-GraphApi -ApiPath "/users" -QueryParameters $script:queryParameters
126211
Write-Verbose "Retrieved user object as $(ConvertTo-JSON $usersResponse.value)" -Verbose
127212

128213
$userObjectId = $usersResponse.value.objectId
@@ -150,10 +235,10 @@ function Clear-AzsUserData
150235
else
151236
{
152237
$params = @{
153-
AccessToken = $accessToken
238+
AccessToken = $script:adminArmAccessToken
154239
UserObjectId = $userObjectId
155240
DirectoryTenantId = $dirId
156-
AdminSubscriptionId = $adminSubscriptionId
241+
AdminSubscriptionId = $script:adminSubscriptionId
157242
AzsAdminArmEndpoint = $AzsAdminArmEndpoint
158243
}
159244
$curResult = Clear-SinglePortalUserData @params
@@ -164,6 +249,102 @@ function Clear-AzsUserData
164249
return $clearUserDataResult
165250
}
166251

252+
<#
253+
.Synopsis
254+
Clear the portal user data
255+
#>
256+
function Clear-AzsUserDataWithUserObjectId
257+
{
258+
param
259+
(
260+
# The directory tenant identifier of Azure Stack Administrator.
261+
[Parameter(Mandatory=$true)]
262+
[ValidateNotNullOrEmpty()]
263+
[string] $AzsAdminDirectoryTenantId,
264+
265+
# The Azure Stack ARM endpoint URI.
266+
[Parameter(Mandatory=$true)]
267+
[ValidateNotNullOrEmpty()]
268+
[Uri] $AzsAdminArmEndpoint,
269+
270+
# The user object Id of the account whoes user data should be cleared.
271+
[Parameter(Mandatory=$true)]
272+
[ValidateNotNullOrEmpty()]
273+
[string] $UserObjectId,
274+
275+
# The directory tenant identifier of account whoes user data should be cleared.
276+
[Parameter(Mandatory=$true)]
277+
[ValidateNotNullOrEmpty()]
278+
[string] $DirectoryTenantId,
279+
280+
# Optional: A credential used to authenticate with Azure Stack. Must support a non-interactive authentication flow. If not provided, the script will prompt for user credentials.
281+
[ValidateNotNull()]
282+
[pscredential] $AutomationCredential = $null
283+
)
284+
285+
$ErrorActionPreference = 'Stop'
286+
$VerbosePreference = 'Continue'
287+
288+
$params = @{
289+
AzsAdminDirectoryTenantId = $AzsAdminDirectoryTenantId
290+
AzsAdminArmEndpoint = $AzsAdminArmEndpoint
291+
AutomationCredential = $AutomationCredential
292+
}
293+
Initialize-UserDataClearEnv @params
294+
295+
$params = @{
296+
AccessToken = $script:adminArmAccessToken
297+
UserObjectId = $UserObjectId
298+
DirectoryTenantId = $DirectoryTenantId
299+
AdminSubscriptionId = $script:adminSubscriptionId
300+
AzsAdminArmEndpoint = $AzsAdminArmEndpoint
301+
}
302+
Clear-SinglePortalUserData @params
303+
}
304+
305+
function Get-UserObjectId
306+
{
307+
param
308+
(
309+
# The directory tenant identifier of user account
310+
[Parameter(Mandatory=$true)]
311+
[ValidateNotNullOrEmpty()]
312+
[string] $DirectoryTenantId,
313+
314+
# The Azure Stack ARM endpoint URI.
315+
[Parameter(Mandatory=$true)]
316+
[ValidateNotNullOrEmpty()]
317+
[Uri] $AzsArmEndpoint,
318+
319+
# The user principal name of the account whoes user data should be cleared.
320+
[Parameter(Mandatory=$true)]
321+
[ValidateNotNullOrEmpty()]
322+
[string] $UserPrincipalName,
323+
324+
# Optional: A credential used to authenticate with Azure Stack. Must support a non-interactive authentication flow. If not provided, the script will prompt for user credentials.
325+
[ValidateNotNull()]
326+
[pscredential] $AutomationCredential = $null
327+
)
328+
329+
$params = @{
330+
AzsAdminDirectoryTenantId = $DirectoryTenantId
331+
AzsAdminArmEndpoint = $AzsArmEndpoint
332+
AutomationCredential = $AutomationCredential
333+
UserPrincipalName = $UserPrincipalName
334+
}
335+
Initialize-UserDataClearEnv @params
336+
337+
Write-Verbose "Intializing graph env..." -Verbose
338+
Initialize-GraphEnvironment @script:initializeGraphEnvParams -DirectoryTenantId $DirectoryTenantId
339+
Write-Verbose "Intialized graph env" -Verbose
340+
341+
Write-Verbose "Querying all users..." -Verbose
342+
$usersResponse = Invoke-GraphApi -ApiPath "/users" -QueryParameters $script:queryParameters
343+
Write-Verbose "Retrieved user object as $(ConvertTo-JSON $usersResponse.value)" -Verbose
344+
345+
return $usersResponse.value.objectId
346+
}
347+
167348
function Clear-SinglePortalUserData
168349
{
169350
param
@@ -236,4 +417,7 @@ function Clear-SinglePortalUserData
236417
}
237418
}
238419

239-
Export-ModuleMember -Function Clear-AzsUserData
420+
Export-ModuleMember -Function Get-UserObjectId
421+
Export-ModuleMember -Function Clear-AzsUserData
422+
Export-ModuleMember -Function Clear-AzsUserDataWithUserPrincipalName
423+
Export-ModuleMember -Function Clear-AzsUserDataWithUserObjectId

0 commit comments

Comments
 (0)