Skip to content

Commit c3a76fc

Browse files
stevemutungiKenitoInc
authored andcommitted
Add WhatIf scenario when creating an app
1 parent 17e316f commit c3a76fc

File tree

1 file changed

+48
-36
lines changed

1 file changed

+48
-36
lines changed

module/Entra/Microsoft.Entra/Governance/Set-EntraAppRoleToApplicationUser.ps1

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -115,42 +115,49 @@ function Set-EntraAppRoleToApplicationUser {
115115

116116
function CreateApplicationIfNotExists {
117117
param ([string]$DisplayName)
118-
118+
119119
try {
120120
# Check if application exists
121-
122121
$existingApp = Get-EntraApplication -Filter "displayName eq '$DisplayName'" -ErrorAction SilentlyContinue
123-
122+
124123
if (-not $existingApp) {
125-
# Create new application
126-
$appParams = @{
127-
DisplayName = $DisplayName
128-
SignInAudience = "AzureADMyOrg"
129-
Web = @{
130-
RedirectUris = @("https://localhost")
131-
}
132-
}
133-
134124
if ($PSCmdlet.ShouldProcess("Application '$DisplayName'", "Create")) {
125+
$appParams = @{
126+
DisplayName = $DisplayName
127+
SignInAudience = "AzureADMyOrg"
128+
Web = @{ RedirectUris = @("https://localhost") }
129+
}
135130
$newApp = New-EntraApplication @appParams
131+
Write-ColoredVerbose "Created new application: $DisplayName"
136132
}
137-
138-
Write-ColoredVerbose "Created new application: $DisplayName"
139-
140-
# Create service principal for the application
141-
$spParams = @{
142-
AppId = $newApp.AppId
143-
DisplayName = $DisplayName
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
139+
}
140+
Write-ColoredVerbose "WhatIf: Simulating creation of application: $DisplayName"
144141
}
145-
146-
147-
142+
148143
if ($PSCmdlet.ShouldProcess("Service principal '$DisplayName'", "Create")) {
144+
$spParams = @{
145+
AppId = $newApp.AppId
146+
DisplayName = $DisplayName
147+
}
149148
$newSp = New-EntraServicePrincipal @spParams
149+
Write-ColoredVerbose "Created new service principal for application: $DisplayName"
150150
}
151-
Write-ColoredVerbose "Created new service principal for application: $DisplayName"
152-
153-
[PSCustomObject]@{
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"
158+
}
159+
160+
return [PSCustomObject]@{
154161
ApplicationId = $newApp.Id
155162
ApplicationDisplayName = $newApp.DisplayName
156163
ServicePrincipalId = $newSp.Id
@@ -160,27 +167,31 @@ function Set-EntraAppRoleToApplicationUser {
160167
}
161168
}
162169
else {
163-
# Get existing service principal
164170
$existingSp = Get-EntraServicePrincipal -Filter "appId eq '$($existingApp.AppId)'" -ErrorAction SilentlyContinue
165-
171+
166172
if (-not $existingSp) {
167-
# Create service principal if it doesn't exist
168-
$spParams = @{
169-
AppId = $existingApp.AppId
170-
DisplayName = $DisplayName
171-
}
172-
173173
if ($PSCmdlet.ShouldProcess("Service principal '$DisplayName'", "Create")) {
174+
$spParams = @{
175+
AppId = $existingApp.AppId
176+
DisplayName = $DisplayName
177+
}
174178
$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"
175187
}
176-
Write-ColoredVerbose "Created new service principal for existing application: $DisplayName"
177188
}
178189
else {
179190
$newSp = $existingSp
180191
Write-ColoredVerbose "Service principal already exists for application: $DisplayName"
181192
}
182-
183-
[PSCustomObject]@{
193+
194+
return [PSCustomObject]@{
184195
ApplicationId = $existingApp.Id
185196
ApplicationDisplayName = $existingApp.DisplayName
186197
ServicePrincipalId = $newSp.Id
@@ -195,6 +206,7 @@ function Set-EntraAppRoleToApplicationUser {
195206
return $null
196207
}
197208
}
209+
198210

199211
function AssignAppServicePrincipalRoleAssignmentIfNotExists {
200212

0 commit comments

Comments
 (0)