I followed the official Microsoft Graph docs to create a deviceHealthScript object using detection and remediation PowerShell scripts. However, I keep getting an error:
Invoke-MgGraphRequest: POST https://graph.microsoft.com/beta/deviceManagement/deviceHealthScripts
HTTP/2.0 400 Bad Request
Vary: Accept-Encoding
Strict-Transport-Security: max-age=31536000
request-id: 35504886-0ed7-4a01-9519-52746591a0d8
client-request-id: ecda70e9-e087-433a-8ed0-bf3469bcf46c
x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"Germany West Central","Slice":"E","Ring":"4","ScaleUnit":"004","RoleInstance":"FR2PEPF000003EF"}}
Date: Fri, 13 Sep 2024 09:48:03 GMT
Content-Type: application/json
Content-Encoding: gzip
{"error":{"code":"UnknownError","message":"Microsoft Proprietary scripts cannot be created via Graph","innerError":{"date":"2024-09-13T09:48:03","request-id":"35504886-0ed7-4a01-9519-52746591a0d8","client-request-id":"ecda70e9-e087-433a-8ed0-bf3469bcf46c"}}}
The PowerShell code I'm trying to use is:
# Define the script content
$detectionScriptContent = @'
If (Test-Path "C:\Proactive_Remediation_Test.txt" -PathType Leaf) {
Write-Host "Exists"
Exit 0
} Else {
Write-Host "File Not Found"
Exit 1
}
'@
$remediationScriptContent = @'
New-Item -Path "C:\Proactive_Remediation_Test.txt" -ItemType File
'@
# Encode the script content to base64
$base64DetectionScriptContent = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($detectionScriptContent))
$base64RemediationScriptContent = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($remediationScriptContent))
$body = @"
{
"@odata.type": "#microsoft.graph.deviceHealthScript",
"publisher": "Publisher value",
"version": "Version value",
"displayName": "Display Name value",
"description": "Description value",
"detectionScriptContent": "$base64DetectionScriptContent",
"remediationScriptContent": "$base64RemediationScriptContent",
"runAsAccount": "user",
"enforceSignatureCheck": true,
"runAs32Bit": false,
"isGlobalScript": true,
"highestAvailableVersion": "Highest Available Version value",
"deviceHealthScriptType": "managedInstallerScript",
"detectionScriptParameters": [],
"remediationScriptParameters": []
}
"@
Invoke-MgGraphRequest -Method POST -Uri "beta/deviceManagement/deviceHealthScripts" -Body $body -ContentType "application/json"
Can someone please help me identify what I'm doing wrong or if it's possible to create this object via Microsoft Graph Rest Beta API?