Skip to content

Commit c5ec080

Browse files
Merge branch 'dev' of https://github.com/KelvinTegelaar/CIPP-API into dev
2 parents d6e7e75 + e7a4b2c commit c5ec080

File tree

9 files changed

+116
-34
lines changed

9 files changed

+116
-34
lines changed

Modules/CIPPCore/Public/Entrypoints/Activity Triggers/Push-ExecOnboardTenantQueue.ps1

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Function Push-ExecOnboardTenantQueue {
1+
function Push-ExecOnboardTenantQueue {
22
<#
33
.FUNCTIONALITY
44
Entrypoint
@@ -354,22 +354,29 @@ Function Push-ExecOnboardTenantQueue {
354354
if ($OnboardingSteps.Step4.Status -eq 'succeeded') {
355355
if ($Item.StandardsExcludeAllTenants -eq $true) {
356356
$AddExclusionObj = [PSCustomObject]@{
357-
label = $Tenant.defaultDomainName
357+
label = '{0} ({1})' -f $Tenant.displayName, $Tenant.defaultDomainName
358358
value = $Tenant.defaultDomainName
359-
addedFields = @{}
359+
addedFields = @{
360+
customerId = $Tenant.customerId
361+
defaultDomainName = $Tenant.defaultDomainName
362+
}
360363
}
361364
$Table = Get-CippTable -tablename 'templates'
362365
$ExistingTemplates = Get-CippazDataTableEntity @Table -Filter "PartitionKey eq 'StandardsTemplateV2'" | Where-Object { $_.JSON -match 'AllTenants' }
363-
foreach ($AllTenantesTemplate in $ExistingTemplates) {
366+
foreach ($AllTenantsTemplate in $ExistingTemplates) {
364367
$object = $AllTenantesTemplate.JSON | ConvertFrom-Json
365-
$NewExcludedTenants = $object.excludedTenants + $AddExclusionObj
368+
$NewExcludedTenants = [system.collections.generic.list[object]]::new()
369+
foreach ($Tenant in $object.excludedTenants) {
370+
$NewExcludedTenants.Add($Tenant)
371+
}
372+
$NewExcludedTenants.Add($AddExclusionObj)
366373
$object.excludedTenants = $NewExcludedTenants
367374
$JSON = ConvertTo-Json -InputObject $object -Compress -Depth 10
368375
$Table.Force = $true
369376
Add-CIPPAzDataTableEntity @Table -Entity @{
370377
JSON = "$JSON"
371-
RowKey = $AllTenantesTemplate.RowKey
372-
GUID = $AllTenantesTemplate.GUID
378+
RowKey = $AllTenantsTemplate.RowKey
379+
GUID = $AllTenantsTemplate.GUID
373380
PartitionKey = 'StandardsTemplateV2'
374381
}
375382
}

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Settings/Invoke-ExecCippReplacemap.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ function Invoke-ExecCippReplacemap {
33
.FUNCTIONALITY
44
Entrypoint
55
.ROLE
6-
CIPP.Extension.ReadWrite
6+
Tenant.Config.ReadWrite
77
#>
88
[CmdletBinding()]
99
param($Request, $TriggerMetadata)
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using namespace System.Net
22

3-
Function Invoke-RemoveStandardTemplate {
3+
function Invoke-RemoveStandardTemplate {
44
<#
55
.FUNCTIONALITY
66
Entrypoint,AnyTenant
@@ -12,31 +12,29 @@ Function Invoke-RemoveStandardTemplate {
1212

1313
$APIName = $Request.Params.CIPPEndpoint
1414
$Headers = $Request.Headers
15-
Write-LogMessage -Headers $Headers -API $APINAME -message 'Accessed this API' -Sev 'Debug'
15+
Write-LogMessage -Headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug'
1616

1717
# Interact with query parameters or the body of the request.
1818
$ID = $Request.Body.ID ?? $Request.Query.ID
1919
try {
2020
$Table = Get-CippTable -tablename 'templates'
21-
$Filter = "PartitionKey eq 'StandardsTemplateV2' and RowKey eq '$id'"
22-
$ClearRow = Get-CIPPAzDataTableEntity @Table -Filter $Filter -Property PartitionKey, RowKey
23-
Remove-AzDataTableEntity -Force @Table -Entity $clearRow
24-
$Result = "Removed Standards Template named $($ClearRow.name) and id $($id)"
25-
Write-LogMessage -Headers $Headers -API $APINAME -message $Result -Sev 'Info'
21+
$Filter = "PartitionKey eq 'StandardsTemplateV2' and RowKey eq '$ID'"
22+
$ClearRow = Get-CIPPAzDataTableEntity @Table -Filter $Filter -Property PartitionKey, RowKey, JSON
23+
$TemplateName = (ConvertFrom-Json -InputObject $ClearRow.JSON).templateName
24+
Remove-AzDataTableEntity -Force @Table -Entity $ClearRow
25+
$Result = "Removed Standards Template named: '$($TemplateName)' with id: $($ID)"
26+
Write-LogMessage -Headers $Headers -API $APIName -message $Result -Sev Info
2627
$StatusCode = [HttpStatusCode]::OK
2728
} catch {
2829
$ErrorMessage = Get-CippException -Exception $_
29-
$Result = "Failed to remove Standards template $ID. $($ErrorMessage.NormalizedError)"
30-
Write-LogMessage -Headers $Headers -API $APINAME -message $Result -Sev 'Error' -LogData $ErrorMessage
30+
$Result = "Failed to remove Standards template: $TemplateName with id: $ID. Error: $($ErrorMessage.NormalizedError)"
31+
Write-LogMessage -Headers $Headers -API $APIName -message $Result -Sev Error -LogData $ErrorMessage
3132
$StatusCode = [HttpStatusCode]::InternalServerError
3233
}
3334

34-
3535
# Associate values to output bindings by calling 'Push-OutputBinding'.
3636
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
3737
StatusCode = $StatusCode
3838
Body = @{'Results' = $Result }
3939
})
40-
41-
4240
}

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tenant/Tools/Invoke-ExecGraphExplorerPreset.ps1

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using namespace System.Net
22

3-
Function Invoke-ExecGraphExplorerPreset {
3+
function Invoke-ExecGraphExplorerPreset {
44
<#
55
.FUNCTIONALITY
66
Entrypoint
@@ -22,7 +22,7 @@ Function Invoke-ExecGraphExplorerPreset {
2222

2323
switch ($Action) {
2424
'Copy' {
25-
$Id = $Request.Body.preset.id ? $Request.Body.preset.id: (New-Guid).Guid
25+
$Id = $Request.Body.preset.id ? $Request.Body.preset.id : (New-Guid).Guid
2626
}
2727
'Save' {
2828
$Id = $Request.Body.preset.id
@@ -42,6 +42,32 @@ Function Invoke-ExecGraphExplorerPreset {
4242
$params.'$select' = ($params.'$select').value -join ','
4343
}
4444

45+
if (!$Request.Body.preset.name) {
46+
$Message = 'Error: Preset name is required'
47+
$StatusCode = [HttpStatusCode]::BadRequest
48+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
49+
StatusCode = $StatusCode
50+
Body = @{
51+
Results = $Message
52+
Success = $false
53+
}
54+
})
55+
return
56+
}
57+
58+
if (!$Request.Body.preset.endpoint) {
59+
$Message = 'Error: Preset endpoint is required'
60+
$StatusCode = [HttpStatusCode]::BadRequest
61+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
62+
StatusCode = $StatusCode
63+
Body = @{
64+
Results = $Message
65+
Success = $false
66+
}
67+
})
68+
return
69+
}
70+
4571
$Preset = [PSCustomObject]@{
4672
PartitionKey = 'Preset'
4773
RowKey = [string]$Id

Modules/CIPPCore/Public/Entrypoints/Invoke-ListGraphExplorerPresets.ps1

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using namespace System.Net
22

3-
Function Invoke-ListGraphExplorerPresets {
3+
function Invoke-ListGraphExplorerPresets {
44
<#
55
.FUNCTIONALITY
66
Entrypoint,AnyTenant
@@ -19,14 +19,14 @@ Function Invoke-ListGraphExplorerPresets {
1919

2020
try {
2121
$Table = Get-CIPPTable -TableName 'GraphPresets'
22-
$Presets = Get-CIPPAzDataTableEntity @Table -Filter "Owner eq '$Username' or IsShared eq true" | Sort-Object -Property name
22+
$Presets = Get-CIPPAzDataTableEntity @Table | Where-Object { $Username -eq $_.Owner -or $_.IsShared -eq $true } | Sort-Object -Property name
2323
$Results = foreach ($Preset in $Presets) {
2424
[PSCustomObject]@{
2525
id = $Preset.Id
2626
name = $Preset.name
2727
IsShared = $Preset.IsShared
2828
IsMyPreset = $Preset.Owner -eq $Username
29-
params = ConvertFrom-Json -InputObject $Preset.Params
29+
params = (ConvertFrom-Json -InputObject $Preset.Params)
3030
}
3131
}
3232

@@ -35,6 +35,8 @@ Function Invoke-ListGraphExplorerPresets {
3535
$Results = $Results | Where-Object { ($_.params.endpoint -replace '^/', '') -eq $Endpoint }
3636
}
3737
} catch {
38+
Write-Warning "Could not list presets. $($_.Exception.Message)"
39+
Write-Information $_.InvocationInfo.PositionMessage
3840
$Results = @()
3941
}
4042
# Associate values to output bindings by calling 'Push-OutputBinding'.

Modules/CIPPCore/Public/Get-CIPPAuthentication.ps1

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,22 @@ function Get-CIPPAuthentication {
1919
}
2020
}
2121
} else {
22+
Write-Information 'Connecting to Azure'
2223
Connect-AzAccount -Identity
2324
$SubscriptionId = $env:WEBSITE_OWNER_NAME -split '\+' | Select-Object -First 1
24-
$null = Set-AzContext -SubscriptionId $SubscriptionId
25+
try {
26+
$Context = Get-AzContext
27+
if ($Context.Subscription) {
28+
#Write-Information "Current context: $($Context | ConvertTo-Json)"
29+
if ($Context.Subscription.Id -ne $SubscriptionId) {
30+
Write-Information "Setting context to subscription $SubscriptionId"
31+
$null = Set-AzContext -SubscriptionId $SubscriptionId
32+
}
33+
}
34+
} catch {
35+
Write-Information "ERROR: Could not set context to subscription $SubscriptionId."
36+
}
37+
2538
$keyvaultname = ($env:WEBSITE_DEPLOYMENT_ID -split '-')[0]
2639
$Variables | ForEach-Object {
2740
Set-Item -Path env:$_ -Value (Get-AzKeyVaultSecret -VaultName $keyvaultname -Name $_ -AsPlainText -ErrorAction Stop) -Force

Modules/CIPPCore/Public/GraphHelper/Get-Tenants.ps1

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ function Get-Tenants {
7575
if (($BuildRequired -or $TriggerRefresh.IsPresent) -and $PartnerTenantState.state -ne 'owntenant') {
7676
# Get TenantProperties table
7777
$PropertiesTable = Get-CippTable -TableName 'TenantProperties'
78-
$Aliases = Get-CIPPAzDataTableEntity @PropertiesTable -Filter "RowKey eq 'Alias'"
7978

8079
#get the full list of tenants
8180
$GDAPRelationships = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/tenantRelationships/delegatedAdminRelationships?`$filter=status eq 'active' and not startsWith(displayName,'MLT_')$RelationshipFilter&`$select=customer,autoExtendDuration,endDateTime&`$top=300" -NoAuthCheck:$true
@@ -95,7 +94,11 @@ function Get-Tenants {
9594
# Write-Host "Processing $($_.Name), $($_.displayName) to add to tenant list."
9695
$ExistingTenantInfo = Get-CIPPAzDataTableEntity @TenantsTable -Filter "PartitionKey eq 'Tenants' and RowKey eq '$($_.Name)'"
9796

98-
$Alias = ($Aliases | Where-Object { $_.PartitionKey -eq $_.Name }).Value
97+
$Alias = (Get-AzDataTableEntity @PropertiesTable -Filter "PartitionKey eq '$($_.Name)' and RowKey eq 'Alias'").Value
98+
99+
if ($Alias) {
100+
Write-Host "Alias found for $($_.Name) - $Alias."
101+
}
99102

100103
if ($TriggerRefresh.IsPresent -and $ExistingTenantInfo.customerId) {
101104
# Reset error count
@@ -104,8 +107,29 @@ function Get-Tenants {
104107
Add-CIPPAzDataTableEntity @TenantsTable -Entity $ExistingTenantInfo -Force | Out-Null
105108
}
106109

107-
if ($ExistingTenantInfo -and $ExistingTenantInfo.RequiresRefresh -eq $false -and $ExistingTenantInfo.displayName -eq $LatestRelationship.displayName) {
110+
if ($ExistingTenantInfo -and $ExistingTenantInfo.RequiresRefresh -eq $false -and ($ExistingTenantInfo.displayName -eq $LatestRelationship.displayName -or $ExistingTenantInfo.displayName -eq $Alias)) {
108111
Write-Host 'Existing tenant found. We already have it cached, skipping.'
112+
113+
$DisplayNameUpdated = $false
114+
if (![string]::IsNullOrEmpty($Alias)) {
115+
if ($Alias -ne $ExistingTenantInfo.displayName) {
116+
Write-Host "Alias found for $($_.Name)."
117+
$ExistingTenantInfo.displayName = $Alias
118+
$DisplayNameUpdated = $true
119+
}
120+
} else {
121+
if ($LatestRelationship.displayName -ne $ExistingTenantInfo.displayName) {
122+
Write-Host 'Display name changed from relationship, updating.'
123+
$ExistingTenantInfo.displayName = $LatestRelationship.displayName
124+
$DisplayNameUpdated = $true
125+
}
126+
}
127+
128+
if ($DisplayNameUpdated) {
129+
$ExistingTenantInfo.displayName = $LatestRelationship.displayName
130+
Add-CIPPAzDataTableEntity @TenantsTable -Entity $ExistingTenantInfo -Force | Out-Null
131+
}
132+
109133
$ExistingTenantInfo
110134
return
111135
}
@@ -136,9 +160,9 @@ function Get-Tenants {
136160
}
137161
Write-Host 'finished getting domain'
138162

139-
if ($Aliases.PartitionKey -contains $_.Name -and ![string]::IsNullOrEmpty($Alias)) {
140-
$Alias = $Aliases | Where-Object { $_.PartitionKey -eq $_.Name }
141-
$displayName = $Alias.Value
163+
if (![string]::IsNullOrEmpty($Alias)) {
164+
Write-Information "Setting display name to $Alias."
165+
$displayName = $Alias
142166
} else {
143167
$displayName = $LatestRelationship.displayName
144168
}

Modules/CippExtensions/Public/Extension Functions/Get-ExtensionAPIKey.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ function Get-ExtensionAPIKey {
2323
$keyvaultname = ($env:WEBSITE_DEPLOYMENT_ID -split '-')[0]
2424
$null = Connect-AzAccount -Identity
2525
$SubscriptionId = $env:WEBSITE_OWNER_NAME -split '\+' | Select-Object -First 1
26-
$null = Set-AzContext -SubscriptionId $SubscriptionId
26+
$Context = Get-AzContext
27+
if ($Context.Subscription) {
28+
if ($Context.Subscription.Id -ne $SubscriptionId) {
29+
Write-Information "Setting context to subscription $SubscriptionId"
30+
$null = Set-AzContext -SubscriptionId $SubscriptionId
31+
}
32+
}
2733
$APIKey = (Get-AzKeyVaultSecret -VaultName $keyvaultname -Name $Extension -AsPlainText)
2834
}
2935
Set-Item -Path "env:$Var" -Value $APIKey -Force -ErrorAction SilentlyContinue

Modules/CippExtensions/Public/Extension Functions/Set-ExtensionAPIKey.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ function Set-ExtensionAPIKey {
2626
$keyvaultname = ($env:WEBSITE_DEPLOYMENT_ID -split '-')[0]
2727
$null = Connect-AzAccount -Identity
2828
$SubscriptionId = $env:WEBSITE_OWNER_NAME -split '\+' | Select-Object -First 1
29-
$null = Set-AzContext -SubscriptionId $SubscriptionId
29+
$Context = Get-AzContext
30+
if ($Context.Subscription) {
31+
if ($Context.Subscription.Id -ne $SubscriptionId) {
32+
Write-Information "Setting context to subscription $SubscriptionId"
33+
$null = Set-AzContext -SubscriptionId $SubscriptionId
34+
}
35+
}
3036
$null = Set-AzKeyVaultSecret -VaultName $keyvaultname -Name $Extension -SecretValue (ConvertTo-SecureString -AsPlainText -Force -String $APIKey)
3137
}
3238
Set-Item -Path "env:$Var" -Value $APIKey -Force -ErrorAction SilentlyContinue

0 commit comments

Comments
 (0)