Skip to content

Commit 2b04950

Browse files
committed
update editintunescript to support other script types
1 parent c493e04 commit 2b04950

File tree

1 file changed

+97
-22
lines changed

1 file changed

+97
-22
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Endpoint/MEM/Invoke-EditIntuneScript.ps1

Lines changed: 97 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,110 @@ function Invoke-EditIntuneScript {
1414
$Headers = $Request.Headers
1515
Write-LogMessage -Headers $Headers -API $APINAME -message 'Accessed this API' -Sev Debug
1616

17-
$graphUrl = "https://graph.microsoft.com/beta"
18-
switch($Request.Method) {
19-
"GET" {
20-
$parms = @{
21-
uri = "$graphUrl/deviceManagement/deviceManagementScripts/$($Request.Query.ScriptId)"
22-
tenantid = $Request.Query.TenantFilter
17+
$graphUrl = 'https://graph.microsoft.com/beta'
18+
19+
# Define the endpoint based on script type
20+
function Get-ScriptEndpoint {
21+
param (
22+
[Parameter(Mandatory = $true)]
23+
[string]$ScriptType
24+
)
25+
26+
switch ($ScriptType) {
27+
'Windows' { return 'deviceManagement/deviceManagementScripts' }
28+
'MacOS' { return 'deviceManagement/deviceShellScripts' }
29+
'Remediation' { return 'deviceManagement/deviceHealthScripts' }
30+
'Linux' { return 'deviceManagement/configurationPolicies' }
31+
default { return 'deviceManagement/deviceManagementScripts' }
32+
}
33+
}
34+
35+
switch ($Request.Method) {
36+
'GET' {
37+
# First get the script type by querying the script ID
38+
$scriptId = $Request.Query.ScriptId
39+
$scriptTypeFound = $false
40+
41+
# Try each endpoint to find the script
42+
foreach ($scriptType in @('Windows', 'MacOS', 'Remediation', 'Linux')) {
43+
$endpoint = Get-ScriptEndpoint -ScriptType $scriptType
44+
$parms = @{
45+
uri = "$graphUrl/$endpoint/$scriptId"
46+
tenantid = $Request.Query.TenantFilter
47+
}
48+
49+
try {
50+
$intuneScript = New-GraphGetRequest @parms -ErrorAction Stop
51+
if ($intuneScript) {
52+
$intuneScript | Add-Member -MemberType NoteProperty -Name scriptType -Value $scriptType -Force
53+
$scriptTypeFound = $true
54+
break
55+
}
56+
} catch {
57+
# Script not found in this endpoint, try next one
58+
continue
59+
}
2360
}
2461

25-
$intuneScript = New-GraphGetRequest @parms
26-
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
27-
StatusCode = [HttpStatusCode]::OK
28-
Body = $intuneScript
29-
})
62+
if ($scriptTypeFound) {
63+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
64+
StatusCode = [HttpStatusCode]::OK
65+
Body = $intuneScript
66+
})
67+
} else {
68+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
69+
StatusCode = [HttpStatusCode]::NotFound
70+
Body = "Script with ID $scriptId was not found in any endpoint."
71+
})
72+
}
3073
}
31-
"PATCH" {
74+
'PATCH' {
75+
# Parse the script data to determine type
76+
$scriptData = $Request.Body.IntuneScript | ConvertFrom-Json
77+
$scriptType = $Request.Body.ScriptType
78+
79+
if (-not $scriptType) {
80+
# Try to determine script type from the request body
81+
if ($scriptData.PSObject.Properties.Name -contains '@odata.type') {
82+
switch ($scriptData.'@odata.type') {
83+
'#microsoft.graph.deviceManagementScript' { $scriptType = 'Windows' }
84+
'#microsoft.graph.deviceShellScript' { $scriptType = 'MacOS' }
85+
'#microsoft.graph.deviceHealthScript' { $scriptType = 'Remediation' }
86+
default {
87+
if ($scriptData.platforms -eq 'linux' -and $scriptData.templateReference.templateFamily -eq 'deviceConfigurationScripts') {
88+
$scriptType = 'Linux'
89+
} else {
90+
$scriptType = 'Windows' # Default to Windows if no definitive type found
91+
}
92+
}
93+
}
94+
}
95+
}
96+
97+
$endpoint = Get-ScriptEndpoint -ScriptType $scriptType
3298
$parms = @{
33-
uri = "$graphUrl/deviceManagement/deviceManagementScripts/$($Request.Body.ScriptId)"
99+
uri = "$graphUrl/$endpoint/$($Request.Body.ScriptId)"
34100
tenantid = $Request.Body.TenantFilter
35-
body = $Request.Body.IntuneScript
101+
body = $Request.Body.IntuneScript
102+
}
103+
104+
try {
105+
$patchResult = New-GraphPOSTRequest @parms -type 'PATCH'
106+
$body = [pscustomobject]@{'Results' = $patchResult }
107+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
108+
StatusCode = [HttpStatusCode]::OK
109+
Body = $body
110+
})
111+
} catch {
112+
$ErrorMessage = Get-CippException -Exception $_
113+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
114+
StatusCode = [HttpStatusCode]::BadRequest
115+
Body = "Failed to update script: $($ErrorMessage.NormalizedError)"
116+
})
36117
}
37-
$patchResult = New-GraphPOSTRequest @parms -type "PATCH"
38-
$body = [pscustomobject]@{'Results' = $patchResult }
39-
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
40-
StatusCode = [HttpStatusCode]::OK
41-
Body = $body
42-
})
43118
}
44-
"POST" {
45-
Write-Output "Adding script"
119+
'POST' {
120+
Write-Output 'Adding script'
46121
}
47122
}
48123
}

0 commit comments

Comments
 (0)