33# Licensed under the MIT License. See License in the project root for license information.
44# ------------------------------------------------------------------------------
55function Set-EntraAppRoleToApplicationUser {
6- [CmdletBinding (SupportsShouldProcess = $true , DefaultParameterSetName = ' Default' )]
6+ [CmdletBinding (DefaultParameterSetName = ' Default' )]
77 param (
88 [Parameter (Mandatory = $true ,
99 HelpMessage = " Specify the data source type: 'DatabaseorDirectory', 'SAPCloudIdentity', or 'Generic' which determines the column attribute mapping." ,
@@ -94,9 +94,7 @@ function Set-EntraAppRoleToApplicationUser {
9494 MailNickName = $MailNickname
9595 }
9696
97- if ($PSCmdlet.ShouldProcess (" User '$UserPrincipalName '" , " Create" )) {
98- $newUser = New-EntraUser @userParams
99- }
97+ $newUser = New-EntraUser @userParams
10098 Write-ColoredVerbose - Message " Created new user: $UserPrincipalName " - Color " Green"
10199
102100 return [PSCustomObject ]@ {
@@ -115,49 +113,35 @@ function Set-EntraAppRoleToApplicationUser {
115113
116114 function CreateApplicationIfNotExists {
117115 param ([string ]$DisplayName )
118-
116+
119117 try {
120118 # Check if application exists
119+
121120 $existingApp = Get-EntraApplication - Filter " displayName eq '$DisplayName '" - ErrorAction SilentlyContinue
122-
121+
123122 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" )
139129 }
140- Write-ColoredVerbose " WhatIf: Simulating creation of application: $DisplayName "
141130 }
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
158139 }
159-
160- return [PSCustomObject ]@ {
140+
141+ $newSp = New-EntraServicePrincipal @spParams
142+ Write-ColoredVerbose " Created new service principal for application: $DisplayName "
143+
144+ [PSCustomObject ]@ {
161145 ApplicationId = $newApp.Id
162146 ApplicationDisplayName = $newApp.DisplayName
163147 ServicePrincipalId = $newSp.Id
@@ -167,31 +151,25 @@ function Set-EntraAppRoleToApplicationUser {
167151 }
168152 }
169153 else {
154+ # Get existing service principal
170155 $existingSp = Get-EntraServicePrincipal - Filter " appId eq '$ ( $existingApp.AppId ) '" - ErrorAction SilentlyContinue
171-
156+
172157 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
187162 }
163+
164+ $newSp = New-EntraServicePrincipal @spParams
165+ Write-ColoredVerbose " Created new service principal for existing application: $DisplayName "
188166 }
189167 else {
190168 $newSp = $existingSp
191169 Write-ColoredVerbose " Service principal already exists for application: $DisplayName "
192170 }
193-
194- return [PSCustomObject ]@ {
171+
172+ [PSCustomObject ]@ {
195173 ApplicationId = $existingApp.Id
196174 ApplicationDisplayName = $existingApp.DisplayName
197175 ServicePrincipalId = $newSp.Id
@@ -206,7 +184,6 @@ function Set-EntraAppRoleToApplicationUser {
206184 return $null
207185 }
208186 }
209-
210187
211188 function AssignAppServicePrincipalRoleAssignmentIfNotExists {
212189
@@ -239,10 +216,7 @@ function Set-EntraAppRoleToApplicationUser {
239216 }
240217
241218 # 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
246220 Write-ColoredVerbose " Created new role assignment for user '$UserId ' - AppName: '$ApplicationName ' with role '$RoleDisplayName '" - Color " Green"
247221
248222 return [PSCustomObject ]@ {
@@ -308,7 +282,6 @@ function Set-EntraAppRoleToApplicationUser {
308282 # Add to the typed list
309283 $appRolesList.Add ($appRole )
310284 [void ]$createdRoles.Add ($appRole )
311-
312285 Write-ColoredVerbose " Created new role definition for '$roleName '" - Color " Green"
313286 }
314287
@@ -320,10 +293,7 @@ function Set-EntraAppRoleToApplicationUser {
320293 Tags = @ (" WindowsAzureActiveDirectoryIntegratedApp" )
321294 }
322295
323-
324- if ($PSCmdlet.ShouldProcess (" Update application '$DisplayName ' with AppRole list - '$appRolesList '" , " Update" )) {
325- Update-MgApplication @params
326- }
296+ Update-MgApplication @params
327297 Write-ColoredVerbose " Updated application with $ ( $createdRoles.Count ) new roles" - Color " Green"
328298
329299 return $createdRoles | ForEach-Object {
0 commit comments