Unable to create deviceHealthScript object via Microsoft Graph Rest Beta API

Sergii Machulin 65 Reputation points
2024-09-13T09:54:33.5633333+00:00

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?

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,265 questions
Microsoft Intune
Microsoft Intune
A Microsoft cloud-based management solution that offers mobile device management, mobile application management, and PC management capabilities.
5,197 questions
0 comments No comments
{count} votes

Accepted answer
  1. ZhoumingDuan-MSFT 13,720 Reputation points Microsoft Vendor
    2024-09-16T06:53:55.2333333+00:00

    @Sergii Machulin,Thanks for posting in Q&A.

    Based on my testing, I have encountered the same error with you, this Graph API under the beta version maybe are removed but the official document has been not updated in time, also, there is also no relate Graph API in v1.0, you can submit your feedback to Graph API.

    https://feedbackportal.microsoft.com/feedback/forum/ebe2edae-97d1-ec11-a7b5-0022481f3c80

    Hope it will help.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.