|
| 1 | +function Invoke-AddWin32ScriptApp { |
| 2 | + <# |
| 3 | + .FUNCTIONALITY |
| 4 | + Entrypoint,AnyTenant |
| 5 | + .ROLE |
| 6 | + Endpoint.Application.ReadWrite |
| 7 | + #> |
| 8 | + [CmdletBinding()] |
| 9 | + param($Request, $TriggerMetadata) |
| 10 | + |
| 11 | + $APIName = $Request.Params.CIPPEndpoint |
| 12 | + $Headers = $Request.Headers |
| 13 | + |
| 14 | + $Win32ScriptApp = $Request.Body |
| 15 | + $AssignTo = $Win32ScriptApp.AssignTo -eq 'customGroup' ? $Win32ScriptApp.CustomGroup : $Win32ScriptApp.AssignTo |
| 16 | + |
| 17 | + # Validate required fields |
| 18 | + if ([string]::IsNullOrEmpty($Win32ScriptApp.ApplicationName) -and [string]::IsNullOrEmpty($Win32ScriptApp.applicationName)) { |
| 19 | + return ([HttpResponseContext]@{ |
| 20 | + StatusCode = [HttpStatusCode]::BadRequest |
| 21 | + Body = @{ Results = @('Application name is required') } |
| 22 | + }) |
| 23 | + } |
| 24 | + |
| 25 | + if ([string]::IsNullOrEmpty($Win32ScriptApp.installScript)) { |
| 26 | + return ([HttpResponseContext]@{ |
| 27 | + StatusCode = [HttpStatusCode]::BadRequest |
| 28 | + Body = @{ Results = @('Install script is required') } |
| 29 | + }) |
| 30 | + } |
| 31 | + |
| 32 | + # Use whichever case was provided |
| 33 | + $AppName = if ($Win32ScriptApp.ApplicationName) { $Win32ScriptApp.ApplicationName } else { $Win32ScriptApp.applicationName } |
| 34 | + |
| 35 | + $AllowedTenants = Test-CIPPAccess -Request $Request -TenantList |
| 36 | + $Tenants = ($Request.Body.selectedTenants | Where-Object { $AllowedTenants -contains $_.customerId -or $AllowedTenants -contains 'AllTenants' }).defaultDomainName |
| 37 | + |
| 38 | + $Results = foreach ($Tenant in $Tenants) { |
| 39 | + try { |
| 40 | + $CompleteObject = [PSCustomObject]@{ |
| 41 | + tenant = $Tenant |
| 42 | + Applicationname = $AppName |
| 43 | + assignTo = $AssignTo |
| 44 | + InstallationIntent = $Win32ScriptApp.InstallationIntent |
| 45 | + type = 'Win32ScriptApp' |
| 46 | + description = $Win32ScriptApp.description |
| 47 | + publisher = $Win32ScriptApp.publisher |
| 48 | + installScript = $Win32ScriptApp.installScript |
| 49 | + uninstallScript = $Win32ScriptApp.uninstallScript |
| 50 | + detectionPath = $Win32ScriptApp.detectionPath |
| 51 | + detectionFile = $Win32ScriptApp.detectionFile |
| 52 | + runAsAccount = if ($Win32ScriptApp.InstallAsSystem) { 'system' } else { 'user' } |
| 53 | + deviceRestartBehavior = if ($Win32ScriptApp.DisableRestart) { 'suppress' } else { 'allow' } |
| 54 | + runAs32Bit = [bool]$Win32ScriptApp.runAs32Bit |
| 55 | + enforceSignatureCheck = [bool]$Win32ScriptApp.enforceSignatureCheck |
| 56 | + } | ConvertTo-Json -Depth 15 |
| 57 | + |
| 58 | + $Table = Get-CippTable -tablename 'apps' |
| 59 | + $Table.Force = $true |
| 60 | + Add-CIPPAzDataTableEntity @Table -Entity @{ |
| 61 | + JSON = "$CompleteObject" |
| 62 | + RowKey = "$((New-Guid).GUID)" |
| 63 | + PartitionKey = 'apps' |
| 64 | + status = 'Not Deployed yet' |
| 65 | + } |
| 66 | + "Successfully added Win32 Script App for $($Tenant) to queue." |
| 67 | + Write-LogMessage -headers $Headers -API $APIName -tenant $Tenant -message "Successfully added Win32 Script App $AppName to queue" -Sev 'Info' |
| 68 | + } catch { |
| 69 | + Write-LogMessage -headers $Headers -API $APIName -tenant $Tenant -message "Failed to add Win32 Script App $AppName to queue. Error: $($_.Exception.Message)" -Sev 'Error' |
| 70 | + "Failed to add Win32 Script App for $($Tenant) to queue: $($_.Exception.Message)" |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + $body = [PSCustomObject]@{ 'Results' = $Results } |
| 75 | + |
| 76 | + return ([HttpResponseContext]@{ |
| 77 | + StatusCode = [HttpStatusCode]::OK |
| 78 | + Body = $body |
| 79 | + }) |
| 80 | +} |
0 commit comments