3
3
# Licensed under the MIT License. See License in the project root for license information.
4
4
# ------------------------------------------------------------------------------
5
5
function Set-EntraAppRoleToApplicationUser {
6
- [CmdletBinding (SupportsShouldProcess = $true , DefaultParameterSetName = ' Default' )]
6
+ [CmdletBinding (DefaultParameterSetName = ' Default' )]
7
7
param (
8
8
[Parameter (Mandatory = $true ,
9
9
HelpMessage = " Specify the data source type: 'DatabaseorDirectory', 'SAPCloudIdentity', or 'Generic' which determines the column attribute mapping." ,
@@ -94,9 +94,7 @@ function Set-EntraAppRoleToApplicationUser {
94
94
MailNickName = $MailNickname
95
95
}
96
96
97
- if ($PSCmdlet.ShouldProcess (" User '$UserPrincipalName '" , " Create" )) {
98
- $newUser = New-EntraUser @userParams
99
- }
97
+ $newUser = New-EntraUser @userParams
100
98
Write-ColoredVerbose - Message " Created new user: $UserPrincipalName " - Color " Green"
101
99
102
100
return [PSCustomObject ]@ {
@@ -115,49 +113,35 @@ function Set-EntraAppRoleToApplicationUser {
115
113
116
114
function CreateApplicationIfNotExists {
117
115
param ([string ]$DisplayName )
118
-
116
+
119
117
try {
120
118
# Check if application exists
119
+
121
120
$existingApp = Get-EntraApplication - Filter " displayName eq '$DisplayName '" - ErrorAction SilentlyContinue
122
-
121
+
123
122
if (-not $existingApp ) {
124
- if ($PSCmdlet.ShouldProcess (" Application '$DisplayName '" , " Create" )) {
125
- $appParams = @ {
126
- DisplayName = $DisplayName
127
- SignInAudience = " AzureADMyOrg"
128
- Web = @ { RedirectUris = @ (" https://localhost" ) }
129
- }
130
- $newApp = New-EntraApplication @appParams
131
- Write-ColoredVerbose " Created new application: $DisplayName "
132
- }
133
- else {
134
- # Handle -WhatIf scenario by returning a mock object
135
- $newApp = [PSCustomObject ]@ {
136
- Id = " WhatIf-AppId"
137
- AppId = " WhatIf-AppId"
138
- DisplayName = $DisplayName
123
+ # Create new application
124
+ $appParams = @ {
125
+ DisplayName = $DisplayName
126
+ SignInAudience = " AzureADMyOrg"
127
+ Web = @ {
128
+ RedirectUris = @ (" https://localhost" )
139
129
}
140
- Write-ColoredVerbose " WhatIf: Simulating creation of application: $DisplayName "
141
130
}
142
-
143
- if ($PSCmdlet.ShouldProcess (" Service principal '$DisplayName '" , " Create" )) {
144
- $spParams = @ {
145
- AppId = $newApp.AppId
146
- DisplayName = $DisplayName
147
- }
148
- $newSp = New-EntraServicePrincipal @spParams
149
- Write-ColoredVerbose " Created new service principal for application: $DisplayName "
150
- }
151
- else {
152
- # Handle -WhatIf scenario
153
- $newSp = [PSCustomObject ]@ {
154
- Id = " WhatIf-ServicePrincipalId"
155
- DisplayName = $DisplayName
156
- }
157
- Write-ColoredVerbose " WhatIf: Simulating creation of service principal for application: $DisplayName "
131
+
132
+ $newApp = New-EntraApplication @appParams
133
+ Write-ColoredVerbose " Created new application: $DisplayName "
134
+
135
+ # Create service principal for the application
136
+ $spParams = @ {
137
+ AppId = $newApp.AppId
138
+ DisplayName = $DisplayName
158
139
}
159
-
160
- return [PSCustomObject ]@ {
140
+
141
+ $newSp = New-EntraServicePrincipal @spParams
142
+ Write-ColoredVerbose " Created new service principal for application: $DisplayName "
143
+
144
+ [PSCustomObject ]@ {
161
145
ApplicationId = $newApp.Id
162
146
ApplicationDisplayName = $newApp.DisplayName
163
147
ServicePrincipalId = $newSp.Id
@@ -167,31 +151,25 @@ function Set-EntraAppRoleToApplicationUser {
167
151
}
168
152
}
169
153
else {
154
+ # Get existing service principal
170
155
$existingSp = Get-EntraServicePrincipal - Filter " appId eq '$ ( $existingApp.AppId ) '" - ErrorAction SilentlyContinue
171
-
156
+
172
157
if (-not $existingSp ) {
173
- if ($PSCmdlet.ShouldProcess (" Service principal '$DisplayName '" , " Create" )) {
174
- $spParams = @ {
175
- AppId = $existingApp.AppId
176
- DisplayName = $DisplayName
177
- }
178
- $newSp = New-EntraServicePrincipal @spParams
179
- Write-ColoredVerbose " Created new service principal for existing application: $DisplayName "
180
- }
181
- else {
182
- $newSp = [PSCustomObject ]@ {
183
- Id = " WhatIf-ServicePrincipalId"
184
- DisplayName = $DisplayName
185
- }
186
- Write-ColoredVerbose " WhatIf: Simulating creation of service principal for existing application: $DisplayName "
158
+ # Create service principal if it doesn't exist
159
+ $spParams = @ {
160
+ AppId = $existingApp.AppId
161
+ DisplayName = $DisplayName
187
162
}
163
+
164
+ $newSp = New-EntraServicePrincipal @spParams
165
+ Write-ColoredVerbose " Created new service principal for existing application: $DisplayName "
188
166
}
189
167
else {
190
168
$newSp = $existingSp
191
169
Write-ColoredVerbose " Service principal already exists for application: $DisplayName "
192
170
}
193
-
194
- return [PSCustomObject ]@ {
171
+
172
+ [PSCustomObject ]@ {
195
173
ApplicationId = $existingApp.Id
196
174
ApplicationDisplayName = $existingApp.DisplayName
197
175
ServicePrincipalId = $newSp.Id
@@ -206,7 +184,6 @@ function Set-EntraAppRoleToApplicationUser {
206
184
return $null
207
185
}
208
186
}
209
-
210
187
211
188
function AssignAppServicePrincipalRoleAssignmentIfNotExists {
212
189
@@ -239,10 +216,7 @@ function Set-EntraAppRoleToApplicationUser {
239
216
}
240
217
241
218
# Create new assignment
242
- if ($PSCmdlet.ShouldProcess (" Service Principal App Role assignment: AppRole - '$appRoleId ' | UserId - '$UserId ' | Service Principal - '$servicePrincipalObject .Id'" , " Create" )) {
243
- $newAssignment = New-EntraServicePrincipalAppRoleAssignment - ServicePrincipalId $servicePrincipalObject.Id - ResourceId $servicePrincipalObject.Id - Id $appRoleId - PrincipalId $UserId
244
- }
245
-
219
+ $newAssignment = New-EntraServicePrincipalAppRoleAssignment - ServicePrincipalId $servicePrincipalObject.Id - ResourceId $servicePrincipalObject.Id - Id $appRoleId - PrincipalId $UserId
246
220
Write-ColoredVerbose " Created new role assignment for user '$UserId ' - AppName: '$ApplicationName ' with role '$RoleDisplayName '" - Color " Green"
247
221
248
222
return [PSCustomObject ]@ {
@@ -308,7 +282,6 @@ function Set-EntraAppRoleToApplicationUser {
308
282
# Add to the typed list
309
283
$appRolesList.Add ($appRole )
310
284
[void ]$createdRoles.Add ($appRole )
311
-
312
285
Write-ColoredVerbose " Created new role definition for '$roleName '" - Color " Green"
313
286
}
314
287
@@ -320,10 +293,7 @@ function Set-EntraAppRoleToApplicationUser {
320
293
Tags = @ (" WindowsAzureActiveDirectoryIntegratedApp" )
321
294
}
322
295
323
-
324
- if ($PSCmdlet.ShouldProcess (" Update application '$DisplayName ' with AppRole list - '$appRolesList '" , " Update" )) {
325
- Update-MgApplication @params
326
- }
296
+ Update-MgApplication @params
327
297
Write-ColoredVerbose " Updated application with $ ( $createdRoles.Count ) new roles" - Color " Green"
328
298
329
299
return $createdRoles | ForEach-Object {
0 commit comments