byteben / MEM

40 stars 8 forks source link

Step 6 of 6 script states that WinGet app has been installed, but this is not the case... #12

Open SamBlaettler opened 9 months ago

SamBlaettler commented 9 months ago

I plan to use this script as a proactive remediation in our tenant. On a few of our test devices we had the issue that the Company Portal was uninstalled, but not reinstalled again in machine context. Further checking the logs showed that the script installed the Company Portal successfully (but this was not true, the CP was not there on these devices)

image

Very strange, the log says that the installation was not successfull and on the next line the script ends with success. How is this possible?

I had a closer look at the code around Step 6 of 6 and tried to replicate the error by copying the code and simulate the not successfull installation:

                write-host "***********************************"                    
                write-host "Running current version of script"
                write-host "***********************************"
                $winGetRetries = 10
                $winGetAppName = "Company Portal"

                $winGetAppTest = 'Not Installed'
                $testAppxPackage = New-Object -TypeName psobject 
                $testAppxPackage | Add-Member -MemberType NoteProperty -Name Result -Value 'Not Installed'

                # Retry Loop. Number based on $winGetRetries value
                $i = 1

                do {
                    Write-Host "Retry attempt $i of $winGetRetries"
                    #Increment retry counter
                    if ($i -le $winGetRetries -and (-not $winGetAppTest.Result -eq 'Installed' -and (-not $testAppxPackage.Result -eq 'Installed'))) {
                        $i++
                    }
                }

                # Keep retrying the WinGet app install until both the WinGet app and InstallWingetApp are installed correctly or the $winGetRetries value is reached
                while ($i -le $winGetRetries -and (-not $winGetAppTest -eq 'Installed' -and (-not $testAppxPackage.Result -eq 'Installed')))

                if ($i -eq $winGetRetries + 1 -and (-not $winGetAppTest -eq 'Installed' -or (-not $testAppxPackage.Result -eq 'Installed'))) {
                    if ($winGetRetries -ge 2) { $count = "s" }
                    Write-Warning -Message ("The WinGet app '$($winGetAppName)', did not install correctly after '$($winGetRetries)' retry attempt{0}. The maximum number of retries has been reached" -f $count)
                    $global:exitCode = 1
                }
                else {  

                    if ($i -ge 2) { $count2 = "s" }
                    Write-Host ("The WinGet app '$($winGetAppName)', installed correctly after '$($i)' retry attempt{0}" -f $count2)
                }

and guess what happend? I got exactly the same error I had on one of the devices where the installation of the CP failed:


Running current version of script


Retry attempt 1 of 10 The WinGet app 'Company Portal', installed correctly after '1' retry attempts (this is not true!!!)

I then played with the parentheses around "-Not" and ran that version of the script again:

                write-host "***********************************"
                Write-Host "Running corrected version of script"
                write-host "***********************************"
                $winGetRetries = 10
                $winGetAppName = "Company Portal"

                $winGetAppTest = 'Not Installed'
                $testAppxPackage = New-Object -TypeName psobject 
                $testAppxPackage | Add-Member -MemberType NoteProperty -Name Result -Value 'Not Installed'

                # Retry Loop. Number based on $winGetRetries value
                $i = 1

                do {
                    Write-Host "Retry attempt $i of $winGetRetries"

                    #Increment retry counter
                    if ($i -le $winGetRetries -and (-not ($winGetAppTest.Result -eq 'Installed') -and (-not ($testAppxPackage.Result -eq 'Installed')))) {
                        $i++
                    }
                }

                # Keep retrying the WinGet app install until both the WinGet app and InstallWingetApp are installed correctly or the $winGetRetries value is reached
                while ($i -le $winGetRetries -and (-not ($winGetAppTest -eq 'Installed') -and (-not ($testAppxPackage.Result -eq 'Installed'))))

                if ($i -eq $winGetRetries + 1 -and (-not ($winGetAppTest -eq 'Installed') -or (-not ($testAppxPackage.Result -eq 'Installed')))) {
                    if ($winGetRetries -ge 2) { $count = "s" }
                    Write-Warning -Message ("The WinGet app '$($winGetAppName)', did not install correctly after '$($winGetRetries)' retry attempt{0}. The maximum number of retries has been reached" -f $count)
                    $global:exitCode = 1
                }
                else {  

                    if ($i -ge 2) { $count2 = "s" }
                    Write-Host ("The WinGet app '$($winGetAppName)', installed correctly after '$($i)' retry attempt{0}" -f $count2)
                }

and hurray! the script now behaved as expected (trying to install CP 10 times and than raise an error message in case CP is still not installed. This is the output of the script:


Running corrected version of script


Retry attempt 1 of 10 Retry attempt 2 of 10 Retry attempt 3 of 10 Retry attempt 4 of 10 Retry attempt 5 of 10 Retry attempt 6 of 10 Retry attempt 7 of 10 Retry attempt 8 of 10 Retry attempt 9 of 10 Retry attempt 10 of 10 WARNING: The WinGet app 'Company Portal', did not install correctly after '10' retry attempts. The maximum number of retries has been reached

ngjrjeff commented 3 months ago

I tried to use your updated script but it does not work