-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Expand file tree
/
Copy pathInvoke-ExecDeployAppTemplate.ps1
More file actions
117 lines (99 loc) · 5.02 KB
/
Copy pathInvoke-ExecDeployAppTemplate.ps1
File metadata and controls
117 lines (99 loc) · 5.02 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
function Invoke-ExecDeployAppTemplate {
<#
.FUNCTIONALITY
Entrypoint,AnyTenant
.ROLE
Endpoint.Application.ReadWrite
#>
[CmdletBinding()]
param($Request, $TriggerMetadata)
$APIName = $Request.Params.CIPPEndpoint
$Headers = $Request.Headers
try {
$TemplateId = $Request.Body.templateId
if (!$TemplateId) { throw 'No template ID provided' }
$Table = Get-CippTable -tablename 'templates'
$Filter = "PartitionKey eq 'AppTemplate' and RowKey eq '$TemplateId'"
$TemplateEntity = Get-CIPPAzDataTableEntity @Table -Filter $Filter
if (!$TemplateEntity) { throw 'Template not found' }
$TemplateData = $TemplateEntity.JSON | ConvertFrom-Json -Depth 100
$AppsRaw = $TemplateData.Apps
$Apps = [System.Collections.Generic.List[PSCustomObject]]::new()
$AppTypes = @($AppsRaw.appType)
$AppNames = @($AppsRaw.appName)
$AppConfigs = @($AppsRaw.config)
for ($i = 0; $i -lt $AppTypes.Count; $i++) {
$Apps.Add([PSCustomObject]@{
appType = [string]$AppTypes[$i]
appName = [string]$AppNames[$i]
config = [string]$AppConfigs[$i]
})
}
$SelectedTenants = @($Request.Body.selectedTenants | ForEach-Object {
[PSCustomObject]@{
defaultDomainName = $_.defaultDomainName
customerId = $_.customerId
}
})
$OverrideAssignTo = $Request.Body.AssignTo
$OverrideCustomGroup = $Request.Body.customGroup
$Results = foreach ($App in $Apps) {
try {
$Config = $App.config
if ($Config -is [string]) {
# Parse case-sensitive to survive templates carrying both 'applicationName'
# and 'ApplicationName', then collapse them via a case-insensitive dictionary.
$Parsed = $Config | ConvertFrom-Json -Depth 100 -AsHashtable
$Config = [ordered]@{}
foreach ($Key in $Parsed.Keys) { $Config[$Key] = $Parsed[$Key] }
$Config = [PSCustomObject]$Config
}
$AppType = "$($App.appType ?? $App.AppType)"
$RequestBody = $Config | ConvertTo-Json -Depth 100 | ConvertFrom-Json -Depth 100
$RequestBody | Add-Member -NotePropertyName 'selectedTenants' -NotePropertyValue $SelectedTenants -Force
$RequestBody | Add-Member -NotePropertyName 'tenantFilter' -NotePropertyValue 'allTenants' -Force
if ($OverrideAssignTo) {
$RequestBody | Add-Member -NotePropertyName 'AssignTo' -NotePropertyValue $OverrideAssignTo -Force
if ($OverrideAssignTo -eq 'customGroup' -and $OverrideCustomGroup) {
$RequestBody | Add-Member -NotePropertyName 'CustomGroup' -NotePropertyValue $OverrideCustomGroup -Force
}
}
$MockRequest = [PSCustomObject]@{
Body = $RequestBody
Headers = $Headers
Params = @{ CIPPEndpoint = $APIName }
Query = @{}
}
$HandlerResult = switch ($AppType) {
'StoreApp' { Invoke-AddStoreApp -Request $MockRequest -TriggerMetadata $null }
'chocolateyApp' { Invoke-AddChocoApp -Request $MockRequest -TriggerMetadata $null }
'officeApp' { Invoke-AddOfficeApp -Request $MockRequest -TriggerMetadata $null }
'win32ScriptApp' { Invoke-AddWin32ScriptApp -Request $MockRequest -TriggerMetadata $null }
'mspApp' { Invoke-AddMSPApp -Request $MockRequest -TriggerMetadata $null }
default { throw "Unknown app type: $AppType" }
}
if ($HandlerResult.Body.Results) {
$HandlerResult.Body.Results
} elseif ($HandlerResult.Body) {
$HandlerResult.Body
} else {
"Queued '$($App.appName)'"
}
} catch {
$ErrorMessage = Get-CippException -Exception $_
"Failed '$($App.appName)': $($ErrorMessage.NormalizedMessage)"
Write-LogMessage -headers $Headers -API $APIName -message "Failed to deploy app '$($App.appName)' from template: $($ErrorMessage.NormalizedMessage)" -Sev 'Error' -LogData $ErrorMessage
}
}
$StatusCode = [HttpStatusCode]::OK
} catch {
$ErrorMessage = Get-CippException -Exception $_
$Results = "Failed to deploy app template: $($ErrorMessage.NormalizedMessage)"
Write-LogMessage -headers $Headers -API $APIName -message $Results -Sev 'Error' -LogData $ErrorMessage
$StatusCode = [HttpStatusCode]::InternalServerError
}
return ([HttpResponseContext]@{
StatusCode = $StatusCode
Body = @{ Results = @($Results) }
})
}