From d4d1e7d2162a5574126fde538aeda93b78938f05 Mon Sep 17 00:00:00 2001 From: Timothy Gruber Date: Tue, 18 Jun 2024 10:40:02 -0700 Subject: [PATCH 1/4] feat: Add retry logic to Win32 app creation --- Public/Add-IntuneWin32App.ps1 | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/Public/Add-IntuneWin32App.ps1 b/Public/Add-IntuneWin32App.ps1 index 0586111..8693ef9 100644 --- a/Public/Add-IntuneWin32App.ps1 +++ b/Public/Add-IntuneWin32App.ps1 @@ -505,16 +505,35 @@ function Add-IntuneWin32App { $Win32AppBody.Add("requirementRules", $AdditionalRequirementRule) } - # Create the Win32 app - Write-Verbose -Message "Attempting to create Win32 app using constructed body converted to JSON content" - $Win32MobileAppRequest = Invoke-IntuneGraphRequest -APIVersion "Beta" -Resource "mobileApps" -Method "POST" -Body ($Win32AppBody | ConvertTo-Json) - if ($Win32MobileAppRequest.'@odata.type' -notlike "#microsoft.graph.win32LobApp") { - Write-Warning -Message "Failed to create Win32 app using constructed body. Passing converted body as JSON to output." - Write-Warning -Message ($Win32AppBody | ConvertTo-Json); break + # Define retry parameters + $RetryCount = 5 + $RetryDelay = 10 + + # Create the Win32 app with retry logic + $AppCreationSuccess = $false + for ($i = 0; $i -lt $RetryCount; $i++) { + try { + Write-Verbose -Message "Attempting to create Win32 app using constructed body converted to JSON content" + $Win32MobileAppRequest = Invoke-IntuneGraphRequest -APIVersion "Beta" -Resource "mobileApps" -Method "POST" -Body ($Win32AppBody | ConvertTo-Json) -ErrorAction Stop + if ($Win32MobileAppRequest.'@odata.type' -notlike "#microsoft.graph.win32LobApp") { + Write-Warning -Message "Failed to create Win32 app using constructed body. Passing converted body as JSON to output." + Write-Warning -Message ($Win32AppBody | ConvertTo-Json); break + } else { + Write-Verbose -Message "Successfully created Win32 app with ID: $($Win32MobileAppRequest.id)" + $AppCreationSuccess = $true + break + } + } catch { + Write-Warning "An error occurred while creating the Win32 application. Attempt $($i + 1) of $RetryCount. Error: $_" + Start-Sleep -Seconds $RetryDelay + } } - else { - Write-Verbose -Message "Successfully created Win32 app with ID: $($Win32MobileAppRequest.id)" + if (-not $AppCreationSuccess) { + Write-Error "Failed to create Win32 app after $RetryCount attempts. Aborting process." + return + } + else { # Invoke request to setup the reference pointers of each category added to the Win32 app if ($PSBoundParameters["CategoryName"]) { if ($CategoryList.Count -ge 1) { From 6f2bed7fe08b2d37d57d8ce35aafa5beaee7bb06 Mon Sep 17 00:00:00 2001 From: Timothy Gruber Date: Tue, 18 Jun 2024 10:48:59 -0700 Subject: [PATCH 2/4] feat: Add retry logic to content version creation --- Public/Add-IntuneWin32App.ps1 | 40 +++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/Public/Add-IntuneWin32App.ps1 b/Public/Add-IntuneWin32App.ps1 index 8693ef9..fbb11f2 100644 --- a/Public/Add-IntuneWin32App.ps1 +++ b/Public/Add-IntuneWin32App.ps1 @@ -506,12 +506,12 @@ function Add-IntuneWin32App { } # Define retry parameters - $RetryCount = 5 - $RetryDelay = 10 + $CreateWin32AppRetryCount = 5 + $CreateWin32AppRetryDelay = 10 # Create the Win32 app with retry logic $AppCreationSuccess = $false - for ($i = 0; $i -lt $RetryCount; $i++) { + for ($i = 0; $i -lt $CreateWin32AppRetryCount; $i++) { try { Write-Verbose -Message "Attempting to create Win32 app using constructed body converted to JSON content" $Win32MobileAppRequest = Invoke-IntuneGraphRequest -APIVersion "Beta" -Resource "mobileApps" -Method "POST" -Body ($Win32AppBody | ConvertTo-Json) -ErrorAction Stop @@ -524,13 +524,13 @@ function Add-IntuneWin32App { break } } catch { - Write-Warning "An error occurred while creating the Win32 application. Attempt $($i + 1) of $RetryCount. Error: $_" - Start-Sleep -Seconds $RetryDelay + Write-Warning "An error occurred while creating the Win32 application. Attempt $($i + 1) of $CreateWin32AppRetryCount. Error: $_" + Start-Sleep -Seconds $CreateWin32AppRetryDelay } } if (-not $AppCreationSuccess) { - Write-Error "Failed to create Win32 app after $RetryCount attempts. Aborting process." + Write-Error "Failed to create Win32 app after $CreateWin32AppRetryCount attempts. Aborting process." return } else { @@ -547,15 +547,33 @@ function Add-IntuneWin32App { } } + # Define retry parameters + $CreateContentVRetryCount = 5 + $CreateContentRetryDelay = 10 + # Create Content Version for the Win32 app Write-Verbose -Message "Attempting to create contentVersions resource for the Win32 app" - $Win32MobileAppContentVersionRequest = Invoke-IntuneGraphRequest -APIVersion "Beta" -Resource "mobileApps/$($Win32MobileAppRequest.id)/microsoft.graph.win32LobApp/contentVersions" -Method "POST" -Body "{}" - if ([string]::IsNullOrEmpty($Win32MobileAppContentVersionRequest.id)) { - Write-Warning -Message "Failed to create contentVersions resource for Win32 app" + $ContentVersionSuccess = $false + for ($i = 0; $i -lt $CreateContentVRetryCount; $i++) { + try { + $Win32MobileAppContentVersionRequest = Invoke-IntuneGraphRequest -APIVersion "Beta" -Resource "mobileApps/$($Win32MobileAppRequest.id)/microsoft.graph.win32LobApp/contentVersions" -Method "POST" -Body "{}" -ErrorAction Stop + if ([string]::IsNullOrEmpty($Win32MobileAppContentVersionRequest.id)) { + Write-Warning -Message "Failed to create contentVersions resource for Win32 app" + } else { + Write-Verbose -Message "Successfully created contentVersions resource with ID: $($Win32MobileAppContentVersionRequest.id)" + $ContentVersionSuccess = $true + break + } + } catch { + Write-Warning "An error occurred while creating content version. Attempt $($i + 1) of $CreateContentVRetryCount. Error: $_" + Start-Sleep -Seconds $CreateContentRetryDelay + } + } + if (-not $ContentVersionSuccess) { + Write-Error "Failed to create content version after $CreateContentVRetryCount attempts. Aborting process." + return } else { - Write-Verbose -Message "Successfully created contentVersions resource with ID: $($Win32MobileAppContentVersionRequest.id)" - # Extract compressed .intunewin file to subfolder $IntuneWinFilePath = Expand-IntuneWin32AppCompressedFile -FilePath $FilePath -FileName $IntuneWinXMLMetaData.ApplicationInfo.FileName -FolderName "Expand" if ($IntuneWinFilePath -ne $null) { From bb5cbd01b3a410dbfa8077baacbbfa914a59bebe Mon Sep 17 00:00:00 2001 From: Timothy Gruber Date: Tue, 18 Jun 2024 10:54:36 -0700 Subject: [PATCH 3/4] feat: Add retry logic to file content creation --- Public/Add-IntuneWin32App.ps1 | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/Public/Add-IntuneWin32App.ps1 b/Public/Add-IntuneWin32App.ps1 index fbb11f2..c7976b7 100644 --- a/Public/Add-IntuneWin32App.ps1 +++ b/Public/Add-IntuneWin32App.ps1 @@ -549,7 +549,7 @@ function Add-IntuneWin32App { # Define retry parameters $CreateContentVRetryCount = 5 - $CreateContentRetryDelay = 10 + $CreateContentVRetryDelay = 10 # Create Content Version for the Win32 app Write-Verbose -Message "Attempting to create contentVersions resource for the Win32 app" @@ -566,7 +566,7 @@ function Add-IntuneWin32App { } } catch { Write-Warning "An error occurred while creating content version. Attempt $($i + 1) of $CreateContentVRetryCount. Error: $_" - Start-Sleep -Seconds $CreateContentRetryDelay + Start-Sleep -Seconds $CreateContentVRetryDelay } } if (-not $ContentVersionSuccess) { @@ -589,10 +589,29 @@ function Add-IntuneWin32App { "isDependency" = $false } + # Define retry parameters + $CreateContentVResourceRetryCount = 5 + $CreateContentVResourceRetryDelay = 10 + # Create the contentVersions files resource - $Win32MobileAppFileContentRequest = Invoke-IntuneGraphRequest -APIVersion "Beta" -Resource "mobileApps/$($Win32MobileAppRequest.id)/microsoft.graph.win32LobApp/contentVersions/$($Win32MobileAppContentVersionRequest.id)/files" -Method "POST" -Body ($Win32AppFileBody | ConvertTo-Json) - if ([string]::IsNullOrEmpty($Win32MobileAppFileContentRequest.id)) { - Write-Warning -Message "Failed to create Azure Storage blob for contentVersions/files resource for Win32 app" + $FileContentSuccess = $false + for ($i = 0; $i -lt $CreateContentVResourceRetryCount; $i++) { + try { + $Win32MobileAppFileContentRequest = Invoke-IntuneGraphRequest -APIVersion "Beta" -Resource "mobileApps/$($Win32MobileAppRequest.id)/microsoft.graph.win32LobApp/contentVersions/$($Win32MobileAppContentVersionRequest.id)/files" -Method "POST" -Body ($Win32AppFileBody | ConvertTo-Json) -ErrorAction Stop + if ([string]::IsNullOrEmpty($Win32MobileAppFileContentRequest.id)) { + Write-Warning -Message "Failed to create Azure Storage blob for contentVersions/files resource for Win32 app" + } else { + $FileContentSuccess = $true + break + } + } catch { + Write-Warning "An error occurred while creating file content. Attempt $($i + 1) of $CreateContentVResourceRetryCount. Error: $_" + Start-Sleep -Seconds $CreateContentVResourceRetryDelay + } + } + if (-not $FileContentSuccess) { + Write-Error "Failed to create file content after $CreateContentVResourceRetryCount attempts. Aborting process." + return } else { # Wait for the Win32 app file content URI to be created From 796718ee49316e66a8895f410eeccec2beb5b0a5 Mon Sep 17 00:00:00 2001 From: Timothy Gruber Date: Tue, 18 Jun 2024 11:07:38 -0700 Subject: [PATCH 4/4] lint: linting fixes --- Public/Add-IntuneWin32App.ps1 | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Public/Add-IntuneWin32App.ps1 b/Public/Add-IntuneWin32App.ps1 index c7976b7..3f17440 100644 --- a/Public/Add-IntuneWin32App.ps1 +++ b/Public/Add-IntuneWin32App.ps1 @@ -11,16 +11,16 @@ function Add-IntuneWin32App { .PARAMETER DisplayName Specify a display name for the Win32 application. - + .PARAMETER Description Specify a description for the Win32 application. - + .PARAMETER Publisher Specify a publisher name for the Win32 application. .PARAMETER AppVersion Specify the app version for the Win32 application. - + .PARAMETER Developer Specify the developer name for the Win32 application. @@ -32,10 +32,10 @@ function Add-IntuneWin32App { .PARAMETER InformationURL Specify the information URL for the Win32 application. - + .PARAMETER PrivacyURL Specify the privacy URL for the Win32 application. - + .PARAMETER CompanyPortalFeaturedApp Specify whether to have the Win32 application featured in Company Portal or not. @@ -44,13 +44,13 @@ function Add-IntuneWin32App { .PARAMETER InstallCommandLine Specify the install command line for the Win32 application. - + .PARAMETER UninstallCommandLine Specify the uninstall command line for the Win32 application. .PARAMETER InstallExperience Specify the install experience for the Win32 application. Supported values are: system or user. - + .PARAMETER RestartBehavior Specify the restart behavior for the Win32 application. Supported values are: allow, basedOnReturnCode, suppress or force. @@ -59,7 +59,7 @@ function Add-IntuneWin32App { .PARAMETER AllowAvailableUninstall Specify whether to allow the Win32 application to be uninstalled from the Company Portal app when assigned as available. - + .PARAMETER DetectionRule Provide an array of a single or multiple OrderedDictionary objects as detection rules that will be used for the Win32 application. @@ -106,7 +106,7 @@ function Add-IntuneWin32App { 1.0.6 - (2021-08-31) Added AppVersion optional parameter 1.0.7 - (2022-09-02) Removed break command that would prevent the Win32 app body JSON output from being display in case an error occured 1.0.8 - (2022-10-02) Added UseAzCopy parameter switch to override the native transfer method. Specify the UseAzCopy parameter switch when uploading large applications. - Added fallback removal code for the cleanup operation at the end of this function, since OneDrive's Files On Demand feature sometimes blocks the + Added fallback removal code for the cleanup operation at the end of this function, since OneDrive's Files On Demand feature sometimes blocks the expanded .intunewin file cleanup process. 1.0.9 - (2023-01-20) Added parameter AzCopyWindowStyle and ScopeTagName. Updated regex pattern for .intunewin file and parameter FilePath. Added support for specifying Scope Tags when creating the Win 32 app, using the ScopeTagName parameter. Added UnattendedInstall and @@ -332,7 +332,7 @@ function Add-IntuneWin32App { } } } - + # Generate Win32 application body data table with different parameters based upon parameter set name Write-Verbose -Message "Start constructing basic layout of Win32 app body" switch ($PSCmdlet.ParameterSetName) { @@ -373,7 +373,7 @@ function Add-IntuneWin32App { if (-not($PSBoundParameters["Developer"])) { $Developer = [string]::Empty } - + # Generate Win32 application body $AppBodySplat = @{ "MSI" = $true @@ -479,7 +479,7 @@ function Add-IntuneWin32App { if (($DetectionRule.'@odata.type' -contains "#microsoft.graph.win32LobAppPowerShellScriptDetection") -and (@($DetectionRules).'@odata.type'.Count -gt 1)) { Write-Warning -Message "Multiple PowerShell Script detection rules were detected, this is not a supported configuration"; break } - + # Add detection rules to Win32 app body object Write-Verbose -Message "Detection rule objects passed validation checks, attempting to add to existing Win32 app body" $Win32AppBody.Add("detectionRules", $DetectionRule) @@ -693,7 +693,7 @@ function Add-IntuneWin32App { $Win32MobileAppRequest = Invoke-IntuneGraphRequest -APIVersion "Beta" -Resource "mobileApps/$($Win32MobileAppRequest.id)" -Method "GET" Write-Output -InputObject $Win32MobileAppRequest } - } + } } try {