Open tjgruber opened 5 months ago
I believe the upload retry logic is a badly needed feature of IntuneWin32App since Intune gets unpredictable too often. Especially for automation scenarios. So good job on implementing that Tim.
I'm now testing specifically this part of the PR but all the uploads end up with:
WARNING: An error occurred while creating the Win32 application. Error message: Cannot convert the "9/27/2024 10:30:07 AM +00:00" value of type "System.DateTimeOffset" to type "System.DateTime".
As it turns out there's a problem with a cast in this line https://github.com/MSEndpointMgr/IntuneWin32App/blob/84bc50a61b206bd3482d4e90bb0d6200f3914345/Private/Invoke-AzureStorageBlobUpload.ps1#L58
Looking at the original it looks like you just lost a call to the UtcDateTime
property :)
https://github.com/MSEndpointMgr/IntuneWin32App/blob/21b0c09691d864f30a00d611f568a6fde10b7afb/Private/Invoke-AzureStorageBlobUpload.ps1#L58
And honestly I believe a cast to datetime is redundant here since $Global:AccessToken.ExpiresOn.UtcDateTime
property is already a DateTime. We can just rewrite the line like this:
$TokenExpireMinutes = [System.Math]::Round(($Global:AccessToken.ExpiresOn.UtcDateTime - $UTCDateTime).TotalMinutes)
On my end, I got errors when using the original:
Once I removed .UtcDateTime
, it worked without issue locally and in GitHub Actions. I'm strictly using it in an automation scenario there since the PR. I'm curious why that part doesn't work for you but does for me and in automation?
I'll test your below suggestion to see if that works for me as well:
$TokenExpireMinutes = [System.Math]::Round(($Global:AccessToken.ExpiresOn.UtcDateTime - $UTCDateTime).TotalMinutes)`
My end goal is Github Actions as well - I already have an instance of Intune App Factory running there. But I'm testing all the code changes in Windows Sandbox, that's where I got the error above.
Not sure why the statement behaves differently for us, but it could be due to different locales and date/time formats.
My end goal is Github Actions as well - I already have an instance of Intune App Factory running there. But I'm testing all the code changes in Windows Sandbox, that's where I got the error above.
Not sure why the statement behaves differently for us, but it could be due to different locales and date/time formats.
@obuolinis Are you able to test this latest change? It is working well on everything I can test it on. I'd appreciate it if you could try it when you have time.
Next up, I need to add retry logic to almost all calls to MS. It's actually fairly frequent that I get request timeouts, and simply waiting a few seconds fixes it.
Summary
Overview:
This pull request introduces several key changes aimed at enhancing the reliability, flexibility, and automation capabilities (e.g., GitHub Actions, etc.) of the Intune Win32 app lifecycle management using modern authentication (MSAL.PS is not recommended). The changes primarily address issues related to token handling, authentication flow, Azure Storage blob upload processes, and error handling. Designed to automate via Azure Service Principal auth.
Key Changes:
Enhanced Token Handling and Authentication Flow:
New-ClientCredentialsAccessToken
function now calculates and adds anExpiresOn
property to the token object for easier expiration checks.Test-AccessToken
function now uses theExpiresOn
property to determine token expiration, enhancing reliability.Connect-MSIntuneGraph
: Improved error handling and dynamic installation of theMSAL.PS
module if still required for other auth methods.ExpiresOn
property directly.Content-Type Header Addition:
content-type
header to the REST request to ensure correct handling of the request body during upload finalization.Fixing
System.DateTime
Error:System.DateTime
usage in Azure blob upload processes.Retry Logic and SAS URI Renewal:
Module Configuration:
MSAL.PS
from the required modules to support environments where it is dynamically loaded.This PR also introduces retry logic to improve the robustness of the Add-IntuneWin32App function, in hopes to help mitigate some transient errors in the following potential areas: (see Issue #8)
Detailed Changes:
Token Handling Enhancements:
ExpiresOn
property.ExpiresOn
property directly.Azure Storage Blob Upload:
Module Configuration:
MSAL.PS
from the required modules to allow dynamic loading.Error Handling Improvements:
Add-IntuneWin32App: (see Issue #8) Adds retry logic to the following:
Impact:
These improvements are expected to significantly enhance the automation capabilities of the Intune Win32 app lifecycle management process. They address previous limitations and errors, making the system more robust against issues like authentication, throttling / rate limitations, blob upload issues, and some other minor fixes.
Testing:
I have tested this using a fully automated GitHub Actions Intune app management lifecycle workflow, and locally on up to date PS 7. If others can help test other scenarios I would appreciate the help.
Example of the fix working successfully during a GitHub Action (throttling / network / etc issue):
Please review the changes and provide feedback. Thank you!