Skip to content

Commit 0397fb8

Browse files
authored
Merge branch 'KelvinTegelaar:master' into master
2 parents b044a9f + fe30ed9 commit 0397fb8

212 files changed

Lines changed: 2018 additions & 2127 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.DS_Store

10 KB
Binary file not shown.

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ yarn.lock
1515

1616
# Cursor IDE
1717
.cursor/rules
18+
19+
# Ignore all root PowerShell files except profile.ps1
20+
/*.ps1
21+
!/profile.ps1

CIPP-Permissions.json

Lines changed: 0 additions & 814 deletions
This file was deleted.

Config/ExcludeSkuList.JSON

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
},
66
{
77
"GUID": "f30db892-07e9-47e9-837c-80727f46fd3d",
8-
"Product_Display_Name": "MICROSOFT FLOW FREE"
8+
"Product_Display_Name": "Microsoft Power Automate Free"
99
},
1010
{
1111
"GUID": "16ddbbfc-09ea-4de2-b1d7-312db6112d70",
12-
"Product_Display_Name": "MICROSOFT TEAMS (FREE)"
12+
"Product_Display_Name": "Microsoft Teams (Free)"
1313
},
1414
{
1515
"GUID": "a403ebcc-fae0-4ca2-8c8c-7a907fd6c235",
16-
"Product_Display_Name": "Power BI (free)"
16+
"Product_Display_Name": "Microsoft Fabric (Free)"
1717
},
1818
{
1919
"GUID": "61e6bd70-fbdb-4deb-82ea-912842f39431",
@@ -25,7 +25,7 @@
2525
},
2626
{
2727
"GUID": "338148b6-1b11-4102-afb9-f92b6cdc0f8d",
28-
"Product_Display_Name": "DYNAMICS 365 P1 TRIAL FOR INFORMATION WORKERS"
28+
"Product_Display_Name": "Dynamics 365 P1 Tria for Information Workers"
2929
},
3030
{
3131
"GUID": "fcecd1f9-a91e-488d-a918-a96cdb6ce2b0",
@@ -41,19 +41,19 @@
4141
},
4242
{
4343
"GUID": "606b54a9-78d8-4298-ad8b-df6ef4481c80",
44-
"Product_Display_Name": "Power Virtual Agents Viral Trial"
44+
"Product_Display_Name": "Microsoft Copilot Studio Viral Trial"
4545
},
4646
{
4747
"GUID": "1f2f344a-700d-42c9-9427-5cea1d5d7ba6",
48-
"Product_Display_Name": "MICROSOFT STREAM"
48+
"Product_Display_Name": "Microsoft Stream"
4949
},
5050
{
5151
"GUID": "6470687e-a428-4b7a-bef2-8a291ad947c9",
52-
"Product_Display_Name": "WINDOWS STORE FOR BUSINESS"
52+
"Product_Display_Name": "Windows Store for Business"
5353
},
5454
{
5555
"GUID": "710779e8-3d4a-4c88-adb9-386c958d1fdf",
56-
"Product_Display_Name": "MICROSOFT TEAMS EXPLORATORY"
56+
"Product_Display_Name": "Microsoft Teams Exploratory"
5757
},
5858
{
5959
"GUID": "8c4ce438-32a7-4ac5-91a6-e22ae08d9c8b",
@@ -94,5 +94,9 @@
9494
{
9595
"GUID": "99049c9c-6011-4908-bf17-15f496e6519d",
9696
"Product_Display_Name": "Office 365 Extra File Storage"
97+
},
98+
{
99+
"GUID": "47794cd0-f0e5-45c5-9033-2eb6b5fc84e0",
100+
"Product_Display_Name": "Communications Credits"
97101
}
98102
]

Config/SchedulerRateLimits.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

Modules/CIPPCore/Public/Add-CIPPApplicationPermission.ps1

Lines changed: 79 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ function Add-CIPPApplicationPermission {
77
$TenantFilter
88
)
99
if ($ApplicationId -eq $env:ApplicationID -and $TenantFilter -eq $env:TenantID) {
10-
#return @('Cannot modify application permissions for CIPP-SAM on partner tenant')
1110
$RequiredResourceAccess = 'CIPPDefaults'
1211
}
13-
Set-Location (Get-Item $PSScriptRoot).FullName
1412
if ($RequiredResourceAccess -eq 'CIPPDefaults') {
15-
#$RequiredResourceAccess = (Get-Content '.\SAMManifest.json' | ConvertFrom-Json).requiredResourceAccess
1613

14+
Set-Location (Get-Item $PSScriptRoot).FullName
1715
$Permissions = Get-CippSamPermissions -NoDiff
1816
$RequiredResourceAccess = [System.Collections.Generic.List[object]]::new()
1917

@@ -59,33 +57,72 @@ function Add-CIPPApplicationPermission {
5957
}
6058
}
6159

60+
Write-Information "Adding application permissions to application $ApplicationId in tenant $TenantFilter"
6261

63-
$ServicePrincipalList = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/servicePrincipals?`$select=AppId,id,displayName&`$top=999" -skipTokenCache $true -tenantid $TenantFilter -NoAuthCheck $true
62+
$ServicePrincipalList = [System.Collections.Generic.List[object]]::new()
63+
$SPList = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/servicePrincipals?`$select=AppId,id,displayName&`$top=999" -skipTokenCache $true -tenantid $TenantFilter -NoAuthCheck $true
64+
foreach ($SP in $SPList) { $ServicePrincipalList.Add($SP) }
6465
$ourSVCPrincipal = $ServicePrincipalList | Where-Object -Property AppId -EQ $ApplicationId
6566
if (!$ourSVCPrincipal) {
6667
#Our Service Principal isn't available yet. We do a sleep and reexecute after 3 seconds.
6768
Start-Sleep -Seconds 5
68-
$ServicePrincipalList = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/servicePrincipals?`$select=AppId,id,displayName&`$top=999" -skipTokenCache $true -tenantid $TenantFilter -NoAuthCheck $true
69+
$ServicePrincipalList.Clear()
70+
$SPList = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/servicePrincipals?`$select=AppId,id,displayName&`$top=999" -skipTokenCache $true -tenantid $TenantFilter -NoAuthCheck $true
71+
foreach ($SP in $SPList) { $ServicePrincipalList.Add($SP) }
6972
$ourSVCPrincipal = $ServicePrincipalList | Where-Object -Property AppId -EQ $ApplicationId
7073
}
7174

7275
$Results = [System.Collections.Generic.List[string]]::new()
7376

7477
$CurrentRoles = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/servicePrincipals/$($ourSVCPrincipal.id)/appRoleAssignments" -tenantid $TenantFilter -skipTokenCache $true -NoAuthCheck $true
7578

76-
$Grants = foreach ($App in $RequiredResourceAccess) {
79+
# Collect missing service principals and prepare bulk request
80+
$MissingServicePrincipals = [System.Collections.Generic.List[object]]::new()
81+
$AppIdToRequestId = @{}
82+
$requestId = 1
83+
84+
foreach ($App in $RequiredResourceAccess) {
7785
$svcPrincipalId = $ServicePrincipalList | Where-Object -Property AppId -EQ $App.resourceAppId
7886
if (!$svcPrincipalId) {
79-
try {
80-
$Body = @{
81-
appId = $App.resourceAppId
82-
} | ConvertTo-Json -Compress
83-
$svcPrincipalId = New-GraphPOSTRequest -uri 'https://graph.microsoft.com/beta/servicePrincipals' -tenantid $TenantFilter -body $Body -type POST
84-
} catch {
85-
$Results.add("Failed to create service principal for $($App.resourceAppId): $(Get-NormalizedError -message $_.Exception.Message)")
86-
continue
87+
$Body = @{
88+
appId = $App.resourceAppId
89+
}
90+
$MissingServicePrincipals.Add(@{
91+
id = $requestId.ToString()
92+
method = 'POST'
93+
url = '/servicePrincipals'
94+
headers = @{
95+
'Content-Type' = 'application/json'
96+
}
97+
body = $Body
98+
})
99+
$AppIdToRequestId[$App.resourceAppId] = $requestId.ToString()
100+
$requestId++
101+
}
102+
}
103+
104+
# Create missing service principals in bulk
105+
if ($MissingServicePrincipals.Count -gt 0) {
106+
try {
107+
$BulkResults = New-GraphBulkRequest -Requests $MissingServicePrincipals -tenantid $TenantFilter -NoAuthCheck $true
108+
foreach ($Result in $BulkResults) {
109+
if ($Result.status -eq 201) {
110+
$ServicePrincipalList.Add($Result.body)
111+
} else {
112+
$AppId = ($MissingServicePrincipals | Where-Object { $_.id -eq $Result.id }).body.appId
113+
$Results.add("Failed to create service principal for $($AppId): $($Result.body.error.message)")
114+
}
87115
}
116+
} catch {
117+
$Results.add("Failed to create service principals in bulk: $(Get-NormalizedError -message $_.Exception.Message)")
88118
}
119+
}
120+
121+
# Build grants list
122+
$Grants = foreach ($App in $RequiredResourceAccess) {
123+
$svcPrincipalId = $ServicePrincipalList | Where-Object -Property AppId -EQ $App.resourceAppId
124+
if (!$svcPrincipalId) { continue }
125+
89126
foreach ($SingleResource in $App.ResourceAccess | Where-Object -Property Type -EQ 'Role') {
90127
if ($SingleResource.id -in $CurrentRoles.appRoleId) { continue }
91128
[pscustomobject]@{
@@ -95,14 +132,37 @@ function Add-CIPPApplicationPermission {
95132
}
96133
}
97134
}
135+
136+
# Apply grants in bulk
98137
$counter = 0
99-
foreach ($Grant in $Grants) {
138+
if ($Grants.Count -gt 0) {
139+
$GrantRequests = [System.Collections.Generic.List[object]]::new()
140+
$requestId = 1
141+
foreach ($Grant in $Grants) {
142+
$GrantRequests.Add(@{
143+
id = $requestId.ToString()
144+
method = 'POST'
145+
url = "/servicePrincipals/$($ourSVCPrincipal.id)/appRoleAssignedTo"
146+
headers = @{
147+
'Content-Type' = 'application/json'
148+
}
149+
body = $Grant
150+
})
151+
$requestId++
152+
}
153+
100154
try {
101-
$SettingsRequest = New-GraphPOSTRequest -body (ConvertTo-Json -InputObject $Grant -Depth 5) -uri "https://graph.microsoft.com/beta/servicePrincipals/$($ourSVCPrincipal.id)/appRoleAssignedTo" -tenantid $TenantFilter -type POST -NoAuthCheck $true
102-
$counter++
155+
$BulkResults = New-GraphBulkRequest -Requests $GrantRequests -tenantid $TenantFilter -NoAuthCheck $true
156+
foreach ($Result in $BulkResults) {
157+
if ($Result.status -eq 201) {
158+
$counter++
159+
} else {
160+
$GrantRequest = $GrantRequests | Where-Object { $_.id -eq $Result.id }
161+
$Results.add("Failed to grant $($GrantRequest.body.appRoleId) to $($GrantRequest.body.resourceId): $($Result.body.error.message)")
162+
}
163+
}
103164
} catch {
104-
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
105-
$Results.add("Failed to grant $($Grant.appRoleId) to $($Grant.resourceId): $ErrorMessage")
165+
$Results.add("Failed to grant permissions in bulk: $(Get-NormalizedError -message $_.Exception.Message)")
106166
}
107167
}
108168
"Added $counter Application permissions to $($ourSVCPrincipal.displayName)"

0 commit comments

Comments
 (0)