|
| 1 | +using namespace System.Net |
| 2 | + |
| 3 | +Function Invoke-EditIntunePolicy { |
| 4 | + <# |
| 5 | + .FUNCTIONALITY |
| 6 | + Entrypoint |
| 7 | + .ROLE |
| 8 | + Endpoint.MEM.Read |
| 9 | + #> |
| 10 | + [CmdletBinding()] |
| 11 | + param($Request, $TriggerMetadata) |
| 12 | + |
| 13 | + $APIName = $Request.Params.CIPPEndpoint |
| 14 | + $Headers = $Request.Headers |
| 15 | + Write-LogMessage -headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug' |
| 16 | + |
| 17 | + # Interact with query parameters or the body of the request. |
| 18 | + $TenantFilter = $Request.Query.tenantFilter ?? $Request.Body.tenantFilter |
| 19 | + $ID = $Request.Query.ID ?? $Request.Body.ID |
| 20 | + $DisplayName = $Request.Query.newDisplayName ?? $Request.Body.newDisplayName |
| 21 | + $PolicyType = $Request.Query.policyType ?? $Request.Body.policyType |
| 22 | + |
| 23 | + try { |
| 24 | + $properties = @{} |
| 25 | + |
| 26 | + # Only add displayName if it's provided |
| 27 | + if ($DisplayName) { |
| 28 | + $properties["displayName"] = $DisplayName |
| 29 | + } |
| 30 | + |
| 31 | + # Update the policy |
| 32 | + $Request = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/$PolicyType/$ID" -tenantid $TenantFilter -type PATCH -body ($properties | ConvertTo-Json) -asapp $true |
| 33 | + |
| 34 | + $Result = "Successfully updated Intune policy $($ID)" |
| 35 | + if ($DisplayName) { $Result += " name to '$($DisplayName)'" } |
| 36 | + |
| 37 | + Write-LogMessage -headers $Headers -API $APIName -tenant $($TenantFilter) -message $Result -Sev 'Info' |
| 38 | + $StatusCode = [HttpStatusCode]::OK |
| 39 | + } catch { |
| 40 | + $ErrorMessage = Get-CippException -Exception $_ |
| 41 | + $Result = "Failed to update Intune policy $($ID): $($ErrorMessage.NormalizedError)" |
| 42 | + Write-LogMessage -headers $Headers -API $APIName -tenant $($TenantFilter) -message $Result -Sev 'Error' -LogData $ErrorMessage |
| 43 | + $StatusCode = [HttpStatusCode]::InternalServerError |
| 44 | + } |
| 45 | + |
| 46 | + # Associate values to output bindings by calling 'Push-OutputBinding'. |
| 47 | + Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ |
| 48 | + StatusCode = $StatusCode |
| 49 | + Body = @{ 'Results' = $Result } |
| 50 | + }) |
| 51 | +} |
0 commit comments