-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate-DeveloperSecurityRole.ps1
More file actions
573 lines (486 loc) · 27.5 KB
/
Create-DeveloperSecurityRole.ps1
File metadata and controls
573 lines (486 loc) · 27.5 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# Create Custom Developer Security Role for Power Platform
# This script creates a security role definition with restricted permissions for developers
# Role creation via Dataverse Web API with fallback to manual steps
param(
[Parameter(Mandatory=$false)]
[string]$EnvironmentId,
[Parameter(Mandatory=$false)]
[string]$EnvironmentUrl,
[Parameter(Mandatory=$false)]
[string]$RoleName = "Developer",
[Parameter(Mandatory=$false)]
[string]$RoleDescription = "Custom role for developers - can create apps/flows/agents but not custom tables",
# Service Principal Authentication Parameters
[Parameter(Mandatory=$false)]
[string]$TenantId,
[Parameter(Mandatory=$false)]
[string]$ClientId,
[Parameter(Mandatory=$false)]
[string]$ClientSecret,
[Parameter(Mandatory=$false)]
[switch]$UseServicePrincipal
)
# Validate parameters - at least one must be provided
if (-not $EnvironmentId -and -not $EnvironmentUrl) {
Write-Host "ERROR: Either EnvironmentId or EnvironmentUrl (or both) must be provided." -ForegroundColor Red
Write-Host "Usage examples:" -ForegroundColor Yellow
Write-Host " .\Create-DeveloperSecurityRole.ps1 -EnvironmentId 'your-env-id'" -ForegroundColor Gray
Write-Host " .\Create-DeveloperSecurityRole.ps1 -EnvironmentUrl 'your-env-url'" -ForegroundColor Gray
Write-Host " .\Create-DeveloperSecurityRole.ps1 -EnvironmentId 'your-env-id' -EnvironmentUrl 'your-env-url'" -ForegroundColor Gray
Write-Host " .\Create-DeveloperSecurityRole.ps1 -UseServicePrincipal -TenantId 'your-tenant-id' -ClientId 'your-client-id' -ClientSecret 'your-secret' -EnvironmentId 'your-env-id'" -ForegroundColor Gray
exit 1
}
# Validate Service Principal parameters if using Service Principal
if ($UseServicePrincipal) {
if (-not $TenantId -or -not $ClientId -or -not $ClientSecret) {
Write-Host "ERROR: When using -UseServicePrincipal, TenantId, ClientId, and ClientSecret are required." -ForegroundColor Red
Write-Host "Usage: .\Create-DeveloperSecurityRole.ps1 -UseServicePrincipal -TenantId 'your-tenant-id' -ClientId 'your-client-id' -ClientSecret 'your-secret' -EnvironmentId 'env-id'" -ForegroundColor Yellow
exit 1
}
}
Write-Host "Checking authentication..." -ForegroundColor Yellow
# Check authentication method
if ($UseServicePrincipal) {
Write-Host "Using Service Principal authentication..." -ForegroundColor Cyan
try {
# Connect using Service Principal
$secureSecret = ConvertTo-SecureString $ClientSecret -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($ClientId, $secureSecret)
Connect-AzAccount -ServicePrincipal -Credential $credential -Tenant $TenantId -Force | Out-Null
$context = Get-AzContext
if ($context) {
Write-Host "SUCCESS: Service Principal authenticated: $($context.Account.Id)" -ForegroundColor Green
Write-Host "Tenant: $($context.Tenant.Id)" -ForegroundColor White
} else {
Write-Host "ERROR: Service Principal authentication failed." -ForegroundColor Red
exit 1
}
} catch {
Write-Host "ERROR: Service Principal authentication failed: $_" -ForegroundColor Red
Write-Host "Please verify your TenantId, ClientId, and ClientSecret." -ForegroundColor Yellow
exit 1
}
} else {
# Check if user is authenticated to Azure
try {
$context = Get-AzContext
if (-not $context) {
Write-Host "ERROR: Not authenticated to Azure. Please run 'Connect-AzAccount' first." -ForegroundColor Red
Write-Host "Or use Service Principal: -UseServicePrincipal -TenantId 'your-tenant' -ClientId 'your-client-id' -ClientSecret 'your-secret'" -ForegroundColor Yellow
exit 1
} else {
Write-Host "SUCCESS: Azure authentication found: $($context.Account.Id)" -ForegroundColor Green
}
} catch {
Write-Host "ERROR: Azure PowerShell module not available. Please install: Install-Module Az" -ForegroundColor Red
Write-Host "And run: Connect-AzAccount" -ForegroundColor Yellow
exit 1
}
}
# Import required modules
Import-Module Microsoft.PowerApps.Administration.PowerShell -Force
Import-Module Microsoft.PowerApps.PowerShell -Force
# Add required assemblies for URL encoding
Add-Type -AssemblyName System.Web
# Function to get access token for Dataverse API
function Get-DataverseAccessToken {
param(
[string]$EnvironmentUrl,
[bool]$UseServicePrincipal = $false,
[string]$TenantId = $null,
[string]$ClientId = $null,
[string]$ClientSecret = $null
)
try {
if ($UseServicePrincipal -and $TenantId -and $ClientId -and $ClientSecret) {
Write-Host "Getting Service Principal access token for Dataverse..." -ForegroundColor Yellow
# Use client credentials flow for Service Principal
$tokenEndpoint = "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token"
$body = @{
client_id = $ClientId
client_secret = $ClientSecret
scope = "$EnvironmentUrl/.default"
grant_type = "client_credentials"
}
$response = Invoke-RestMethod -Uri $tokenEndpoint -Method POST -Body $body -ContentType "application/x-www-form-urlencoded"
if ($response.access_token) {
Write-Host "SUCCESS: Service Principal token obtained" -ForegroundColor Green
return $response.access_token
} else {
Write-Host "ERROR: Failed to get Service Principal token" -ForegroundColor Red
return $null
}
} else {
# Use user authentication
Write-Host "Getting user access token for Dataverse..." -ForegroundColor Yellow
$token = (Get-AzAccessToken -ResourceUrl $EnvironmentUrl).Token
if ($token) {
Write-Host "SUCCESS: User token obtained" -ForegroundColor Green
return $token
} else {
Write-Host "ERROR: Failed to get user token" -ForegroundColor Red
return $null
}
}
} catch {
Write-Host "ERROR: Token acquisition failed: $_" -ForegroundColor Red
return $null
}
}
# Function to create security role via Dataverse Web API
function New-DataverseSecurityRole {
param(
[string]$EnvironmentUrl,
[string]$RoleName,
[string]$RoleDescription,
[array]$Privileges
)
try {
# Get access token using the appropriate method
$token = Get-DataverseAccessToken -EnvironmentUrl $EnvironmentUrl -UseServicePrincipal $UseServicePrincipal -TenantId $TenantId -ClientId $ClientId -ClientSecret $ClientSecret
if (-not $token) {
Write-Host "ERROR: Could not obtain access token" -ForegroundColor Red
return $null
}
# Headers
$headers = @{
'Authorization' = "Bearer $token"
'Content-Type' = 'application/json'
'OData-MaxVersion' = '4.0'
'OData-Version' = '4.0'
}
# First, get the root business unit ID
Write-Host "Getting business unit information..." -ForegroundColor Yellow
try {
$businessUnitsEndpoint = "$EnvironmentUrl/api/data/v9.2/businessunits?`$filter=parentbusinessunitid eq null&`$select=businessunitid,name"
$businessUnitResponse = Invoke-RestMethod -Uri $businessUnitsEndpoint -Method GET -Headers $headers
if ($businessUnitResponse.value -and $businessUnitResponse.value.Count -gt 0) {
$businessUnitId = $businessUnitResponse.value[0].businessunitid
Write-Host "Found root business unit: $($businessUnitResponse.value[0].name) ($businessUnitId)" -ForegroundColor Green
} else {
Write-Host "ERROR: Could not find root business unit" -ForegroundColor Red
return $null
}
} catch {
Write-Host "ERROR: Failed to get business unit: $_" -ForegroundColor Red
return $null
}
# Create role payload with proper business unit reference
$rolePayload = @{
name = $RoleName
description = $RoleDescription
"businessunitid@odata.bind" = "/businessunits($businessUnitId)"
} | ConvertTo-Json
# API endpoint for roles
$rolesEndpoint = "$EnvironmentUrl/api/data/v9.2/roles"
Write-Host "Creating role via Dataverse API..." -ForegroundColor Yellow
# Create the role
$response = Invoke-RestMethod -Uri $rolesEndpoint -Method POST -Body $rolePayload -Headers $headers
if ($response) {
Write-Host "SUCCESS: Role '$RoleName' created successfully!" -ForegroundColor Green
Write-Host "Role ID: $($response.roleid)" -ForegroundColor White
return $response.roleid
}
} catch {
Write-Host "ERROR: Failed to create role via API: $_" -ForegroundColor Red
# Provide specific guidance for common Service Principal issues
if ($UseServicePrincipal) {
Write-Host "`nService Principal Troubleshooting:" -ForegroundColor Yellow
Write-Host "1. Ensure the Service Principal has 'System Administrator' role in the Dataverse environment" -ForegroundColor White
Write-Host "2. Verify the Service Principal is added as an Application User in Power Platform Admin Center" -ForegroundColor White
Write-Host "3. Check that the ClientId and ClientSecret are correct" -ForegroundColor White
Write-Host "4. Confirm the TenantId matches your Azure AD tenant" -ForegroundColor White
}
return $null
}
}
# Function to assign privileges to role
function Add-RolePrivileges {
param(
[string]$EnvironmentUrl,
[string]$RoleId,
[array]$Privileges
)
try {
$token = Get-DataverseAccessToken -EnvironmentUrl $EnvironmentUrl -UseServicePrincipal $UseServicePrincipal -TenantId $TenantId -ClientId $ClientId -ClientSecret $ClientSecret
if (-not $token) {
Write-Host "ERROR: Could not obtain access token for privileges assignment" -ForegroundColor Red
return
}
$headers = @{
'Authorization' = "Bearer $token"
'Content-Type' = 'application/json'
'OData-MaxVersion' = '4.0'
'OData-Version' = '4.0'
}
Write-Host "Adding privileges to role..." -ForegroundColor Yellow
foreach ($privilege in $Privileges) {
$privilegePayload = @{
"privilege@odata.bind" = "/privileges($($privilege.privilegeid))"
"role@odata.bind" = "/roles($RoleId)"
depth = $privilege.depth
} | ConvertTo-Json
$endpoint = "$EnvironmentUrl/api/data/v9.2/roleprivileges"
try {
Invoke-RestMethod -Uri $endpoint -Method POST -Body $privilegePayload -Headers $headers
Write-Host " SUCCESS: Added privilege: $($privilege.privilegeid)" -ForegroundColor Gray
} catch {
Write-Warning "Failed to add privilege $($privilege.privilegeid): $_"
}
}
Write-Host "SUCCESS: Privileges assignment completed!" -ForegroundColor Green
} catch {
Write-Host "ERROR: Failed to assign privileges: $_" -ForegroundColor Red
}
}
Write-Host "Creating custom '$RoleName' security role..." -ForegroundColor Yellow
try {
# Connect to the environment
Write-Host "Resolving environment details..." -ForegroundColor Yellow
if ($EnvironmentId) {
Write-Host "Environment ID provided: $EnvironmentId" -ForegroundColor White
}
if ($EnvironmentUrl) {
Write-Host "Environment URL provided: $EnvironmentUrl" -ForegroundColor White
}
# Get environment details
$environment = $null
$targetEnvironmentUrl = $null
try {
# Priority logic: if both are provided, validate they match
if ($EnvironmentId -and $EnvironmentUrl) {
Write-Host "Both ID and URL provided - validating consistency..." -ForegroundColor Yellow
# Get environment by ID first
$environment = Get-AdminPowerAppEnvironment -EnvironmentName $EnvironmentId
if ($environment) {
Write-Host "Environment found by ID: $($environment.DisplayName)" -ForegroundColor Green
Write-Host "Environment URL from ID: $($environment.EnvironmentUrl)" -ForegroundColor White
# Check if URLs match (normalize URLs by removing trailing slashes)
$normalizedEnvUrl = $environment.EnvironmentUrl.TrimEnd('/')
$normalizedProvidedUrl = $EnvironmentUrl.TrimEnd('/')
if ($normalizedEnvUrl -eq $normalizedProvidedUrl) {
Write-Host "SUCCESS: Environment ID and URL are consistent" -ForegroundColor Green
$targetEnvironmentUrl = $environment.EnvironmentUrl
} else {
Write-Host "WARNING: Provided URL doesn't match environment ID." -ForegroundColor Yellow
Write-Host " Environment URL: $normalizedEnvUrl" -ForegroundColor Gray
Write-Host " Provided URL: $normalizedProvidedUrl" -ForegroundColor Gray
Write-Host " Using URL from environment ID." -ForegroundColor Yellow
$targetEnvironmentUrl = $environment.EnvironmentUrl
}
} else {
Write-Host "WARNING: Environment ID not found. Using provided URL." -ForegroundColor Yellow
$targetEnvironmentUrl = $EnvironmentUrl
}
}
elseif ($EnvironmentId) {
# Get environment by ID only
$environment = Get-AdminPowerAppEnvironment -EnvironmentName $EnvironmentId
if ($environment) {
Write-Host "Environment found: $($environment.DisplayName)" -ForegroundColor Green
Write-Host "Environment URL: $($environment.EnvironmentUrl)" -ForegroundColor White
$targetEnvironmentUrl = $environment.EnvironmentUrl
} else {
Write-Host "WARNING: Environment ID not found" -ForegroundColor Yellow
}
}
else {
# Use provided URL and try to find matching environment
$targetEnvironmentUrl = $EnvironmentUrl
Write-Host "Looking up environment details for URL: $EnvironmentUrl" -ForegroundColor Yellow
$environments = Get-AdminPowerAppEnvironment
$environment = $environments | Where-Object { $_.EnvironmentUrl -eq $EnvironmentUrl }
if ($environment) {
Write-Host "Environment found: $($environment.DisplayName)" -ForegroundColor Green
Write-Host "Environment ID: $($environment.EnvironmentName)" -ForegroundColor White
} else {
Write-Host "Environment details not found, but proceeding with provided URL" -ForegroundColor Yellow
}
}
} catch {
Write-Warning "Could not retrieve environment details. Continuing with role definition..."
# Use whatever URL we have
if ($EnvironmentUrl) {
$targetEnvironmentUrl = $EnvironmentUrl
} elseif ($environment -and $environment.EnvironmentUrl) {
$targetEnvironmentUrl = $environment.EnvironmentUrl
}
}
# Role definition with all privileges
$roleDefinition = @{
name = $RoleName
description = $RoleDescription
businessunitid = "root-business-unit-id"
# Core permissions for app development
privileges = @(
# Basic User Privileges
@{ privilegeid = "prvReadUser"; depth = "Basic" }
@{ privilegeid = "prvReadBusinessUnit"; depth = "Local" }
# App Creation Rights
@{ privilegeid = "prvCreateAppModule"; depth = "Global" }
@{ privilegeid = "prvWriteAppModule"; depth = "Global" }
@{ privilegeid = "prvReadAppModule"; depth = "Global" }
@{ privilegeid = "prvShareAppModule"; depth = "Global" }
# Canvas App Rights
@{ privilegeid = "prvCreateCanvasApp"; depth = "Global" }
@{ privilegeid = "prvWriteCanvasApp"; depth = "Global" }
@{ privilegeid = "prvReadCanvasApp"; depth = "Global" }
@{ privilegeid = "prvShareCanvasApp"; depth = "Global" }
# Flow Creation Rights
@{ privilegeid = "prvCreateWorkflow"; depth = "Global" }
@{ privilegeid = "prvWriteWorkflow"; depth = "Global" }
@{ privilegeid = "prvReadWorkflow"; depth = "Global" }
# Copilot Studio Agent Rights
@{ privilegeid = "prvCreateBot"; depth = "Global" }
@{ privilegeid = "prvWriteBot"; depth = "Global" }
@{ privilegeid = "prvReadBot"; depth = "Global" }
@{ privilegeid = "prvDeleteBot"; depth = "Global" }
@{ privilegeid = "prvShareBot"; depth = "Global" }
# Copilot Studio Component Rights
@{ privilegeid = "prvCreateBotComponent"; depth = "Global" }
@{ privilegeid = "prvWriteBotComponent"; depth = "Global" }
@{ privilegeid = "prvReadBotComponent"; depth = "Global" }
@{ privilegeid = "prvDeleteBotComponent"; depth = "Global" }
# AI Builder Rights (for AI capabilities in agents)
@{ privilegeid = "prvCreateAIModel"; depth = "Global" }
@{ privilegeid = "prvWriteAIModel"; depth = "Global" }
@{ privilegeid = "prvReadAIModel"; depth = "Global" }
@{ privilegeid = "prvUseAIModel"; depth = "Global" }
# Chatbot Rights
@{ privilegeid = "prvCreateChatbot"; depth = "Global" }
@{ privilegeid = "prvWriteChatbot"; depth = "Global" }
@{ privilegeid = "prvReadChatbot"; depth = "Global" }
@{ privilegeid = "prvDeleteChatbot"; depth = "Global" }
@{ privilegeid = "prvPublishChatbot"; depth = "Global" }
# Solution Rights (for packaging)
@{ privilegeid = "prvReadSolution"; depth = "Global" }
@{ privilegeid = "prvWriteSolution"; depth = "Global" }
@{ privilegeid = "prvCreateSolution"; depth = "Global" }
# Data Access (Read/Write on existing tables)
@{ privilegeid = "prvReadAccount"; depth = "Global" }
@{ privilegeid = "prvWriteAccount"; depth = "Global" }
@{ privilegeid = "prvCreateAccount"; depth = "Global" }
@{ privilegeid = "prvReadContact"; depth = "Global" }
@{ privilegeid = "prvWriteContact"; depth = "Global" }
@{ privilegeid = "prvCreateContact"; depth = "Global" }
# Add more table permissions as needed
# NOTE: Custom table permissions would be added here for existing custom tables
)
# Explicitly EXCLUDE metadata modification privileges
excludedPrivileges = @(
"prvCreateEntity", # Create custom tables
"prvWriteEntity", # Modify table metadata
"prvDeleteEntity", # Delete tables
"prvCreateAttribute", # Create custom fields
"prvWriteAttribute", # Modify field metadata
"prvDeleteAttribute", # Delete fields
"prvCreateRelationship", # Create relationships
"prvWriteRelationship", # Modify relationships
"prvDeleteRelationship", # Delete relationships
"prvCreateOptionSet", # Create choice columns
"prvWriteOptionSet", # Modify choice columns
"prvDeleteOptionSet", # Delete choice columns
"prvPublishDuplicateRule", # System customization
"prvPublishWorkflow" # Advanced workflow publishing
)
}
Write-Host "`nRole Definition:" -ForegroundColor Green
Write-Host "Name: $($roleDefinition.name)" -ForegroundColor White
Write-Host "Description: $($roleDefinition.description)" -ForegroundColor White
Write-Host "Privileges Count: $($roleDefinition.privileges.Count)" -ForegroundColor White
Write-Host "Excluded Privileges Count: $($roleDefinition.excludedPrivileges.Count)" -ForegroundColor White
Write-Host "`nIncluded Capabilities:" -ForegroundColor Green
Write-Host " - Canvas & Model-driven Apps" -ForegroundColor White
Write-Host " - Power Automate Flows" -ForegroundColor White
Write-Host " - Copilot Studio Agents & Chatbots" -ForegroundColor White
Write-Host " - AI Builder Models" -ForegroundColor White
Write-Host " - Read/Write on existing tables" -ForegroundColor White
Write-Host " - Solution packaging" -ForegroundColor White
Write-Host "`nRestricted Capabilities:" -ForegroundColor Red
Write-Host " - Custom table creation" -ForegroundColor White
Write-Host " - Field/column creation" -ForegroundColor White
Write-Host " - Relationship modification" -ForegroundColor White
Write-Host " - System-level customizations" -ForegroundColor White
Write-Host "`nATTEMPTING TO CREATE ROLE VIA DATAVERSE API..." -ForegroundColor Cyan
if ($targetEnvironmentUrl) {
# Try to create the role via API
$roleId = New-DataverseSecurityRole -EnvironmentUrl $targetEnvironmentUrl -RoleName $RoleName -RoleDescription $RoleDescription -Privileges $roleDefinition.privileges
if ($roleId) {
# Add privileges to the role
Add-RolePrivileges -EnvironmentUrl $targetEnvironmentUrl -RoleId $roleId -Privileges $roleDefinition.privileges
Write-Host "`nSUCCESS! Role created via Dataverse API!" -ForegroundColor Green
Write-Host "Role Name: $RoleName" -ForegroundColor White
Write-Host "Role ID: $roleId" -ForegroundColor White
if ($environment) {
Write-Host "Environment: $($environment.DisplayName)" -ForegroundColor White
} else {
Write-Host "Environment URL: $targetEnvironmentUrl" -ForegroundColor White
}
} else {
Write-Host "`nWARNING: API creation failed. Use manual steps below." -ForegroundColor Yellow
}
} else {
Write-Host "`nWARNING: Environment URL not available. Use manual steps below." -ForegroundColor Yellow
}
Write-Host "`n=== Fallback Manual Steps ===" -ForegroundColor Cyan
Write-Host "If API creation failed, use Power Platform Admin Center:" -ForegroundColor White
Write-Host "1. Go to Power Platform Admin Center" -ForegroundColor White
if ($EnvironmentId) {
Write-Host "2. Navigate to your environment: $EnvironmentId" -ForegroundColor White
} elseif ($environment) {
Write-Host "2. Navigate to your environment: $($environment.EnvironmentName)" -ForegroundColor White
} else {
Write-Host "2. Navigate to your environment (find via URL: $EnvironmentUrl)" -ForegroundColor White
}
Write-Host "3. Go to Settings > Users + permissions > Security roles" -ForegroundColor White
Write-Host "4. Create new role with name: '$RoleName'" -ForegroundColor White
Write-Host "5. Configure permissions as shown above" -ForegroundColor White
Write-Host "6. Save the role" -ForegroundColor Green
if ($UseServicePrincipal -and -not $roleId) {
Write-Host "`n=== Service Principal Setup Guide ===" -ForegroundColor Cyan
Write-Host "To use Service Principal authentication, complete these steps:" -ForegroundColor White
Write-Host "`n1. Create App Registration in Azure AD:" -ForegroundColor Yellow
Write-Host " - Go to Azure Portal > Azure Active Directory > App registrations" -ForegroundColor White
Write-Host " - Click 'New registration'" -ForegroundColor White
Write-Host " - Name: 'PowerPlatform-SecurityRole-Creator'" -ForegroundColor White
Write-Host " - Supported account types: 'Accounts in this organizational directory only'" -ForegroundColor White
Write-Host " - Click 'Register'" -ForegroundColor White
Write-Host "`n2. Create Client Secret:" -ForegroundColor Yellow
Write-Host " - In the app registration, go to 'Certificates & secrets'" -ForegroundColor White
Write-Host " - Click 'New client secret'" -ForegroundColor White
Write-Host " - Description: 'Dataverse API Access'" -ForegroundColor White
Write-Host " - Expires: Choose appropriate duration" -ForegroundColor White
Write-Host " - Copy the secret value (you won't see it again!)" -ForegroundColor White
Write-Host "`n3. Add API Permissions:" -ForegroundColor Yellow
Write-Host " - Go to 'API permissions'" -ForegroundColor White
Write-Host " - Click 'Add a permission'" -ForegroundColor White
Write-Host " - Choose 'Dynamics CRM'" -ForegroundColor White
Write-Host " - Select 'Delegated permissions'" -ForegroundColor White
Write-Host " - Check 'user_impersonation'" -ForegroundColor White
Write-Host " - Click 'Add permissions'" -ForegroundColor White
Write-Host " - Click 'Grant admin consent'" -ForegroundColor White
Write-Host "`n4. Add Service Principal to Power Platform:" -ForegroundColor Yellow
Write-Host " - Go to Power Platform Admin Center" -ForegroundColor White
Write-Host " - Navigate to your environment" -ForegroundColor White
Write-Host " - Go to Settings > Users + permissions > Application users" -ForegroundColor White
Write-Host " - Click 'New app user'" -ForegroundColor White
Write-Host " - Select your app registration" -ForegroundColor White
Write-Host " - Business unit: Select root business unit" -ForegroundColor White
Write-Host " - Security roles: Assign 'System Administrator' role" -ForegroundColor White
Write-Host " - Click 'Create'" -ForegroundColor White
Write-Host "`n5. Run script with Service Principal:" -ForegroundColor Yellow
Write-Host " .\Create-DeveloperSecurityRole.ps1 -UseServicePrincipal \\" -ForegroundColor Gray
Write-Host " -TenantId 'your-tenant-id' \\" -ForegroundColor Gray
Write-Host " -ClientId 'your-app-registration-client-id' \\" -ForegroundColor Gray
Write-Host " -ClientSecret 'your-client-secret' \\" -ForegroundColor Gray
Write-Host " -EnvironmentId 'your-environment-id'" -ForegroundColor Gray
}
Write-Host "`nSUCCESS: Role definition complete! Ready for creation in Admin Center." -ForegroundColor Green
} catch {
Write-Host "ERROR: Failed to create security role definition: $_" -ForegroundColor Red
}
Write-Host "`n=== Role Summary ===" -ForegroundColor Cyan
Write-Host "Role Name: $RoleName" -ForegroundColor White
Write-Host "Description: $RoleDescription" -ForegroundColor White
Write-Host "Purpose: Developer role with app/flow/agent creation but no metadata modification" -ForegroundColor White
Write-Host "`nNote: User assignment should be done separately in Power Platform Admin Center." -ForegroundColor Yellow