In PowerShell 7.4, there’s a change in the default encoding that can cause issues when using the Microsoft Graph API, especially when committing files in Intune. Starting from PowerShell 7.4, the default encoding for HTTP requests has changed from ASCII to UTF-8. This affects cmdlets like Invoke-WebRequest
and Invoke-RestMethod
.
What goes wrong?
When uploading .intunewin
files and performing the commit step via the Graph API, this encoding change results in the error: commitFileFailed
. Looking deeper into the root cause of this error, we see that the payload size is incorrect (due to UTF-8 multibyte characters), which leads to a mismatch in fileDigest
or mac
values. As a result, the commit fails, even though the exact same code works perfectly in PowerShell 7.3.9 (or lower).
A GitHub issue has been created for this problem: Github Issue. If you want to dive deeper into the issue, you can find more background information there.
Solution
The solution I used was to downgrade to PowerShell 7.3.9, where the encoding is still ASCII or ISO-8859-1. But if that’s not an option, you can also try the following:
Explicitly force the encoding in your script
$body = [System.Text.Encoding]::GetEncoding("iso-8859-1").GetBytes($jsonBody)
Invoke-RestMethod -Uri $url -Method Post -Headers $header -Body $body
Note: this is a workaround and doesn’t always work, depending on how the Graph API processes the payload.
Check the Content-Type header
Try using:
"Content-Type" = "application/json; charset=utf-8"
"Content-Type" = "application/json; charset=iso-8859-1"
But be cautious: some Graph API endpoints only accept UTF-8.
Install the Preview version of PowerShell 7.3
You can also install the preview version of PowerShell 7.3 (v7.3.0-preview.8) alongside your existing installation and use that specific version for uploading and committing .intunewin
files. But be aware that it’s a preview version and may contain bugs and/or CVEs.
Summary
Starting with PowerShell 7.4, the default encoding for HTTP requests has changed from ASCII to UTF-8. This change can cause issues when interacting with the Microsoft Graph API, particularly during the commit step of .intunewin
file uploads in Intune. The encoding shift may lead to payload size mismatches and digest errors, resulting in a commitFileFailed
error. While downgrading to PowerShell 7.3.9 resolves the issue, alternative workarounds include explicitly setting the encoding, adjusting the Content-Type
header, or using the PowerShell 7.3 preview version. However, these workarounds may not be universally reliable.
Leave a Reply