Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SamBlaettler opened this issue Oct 11, 2023 · 1 comment

Comments

@SamBlaettler
Copy link

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
Copy link

ngjrjeff commented Apr 25, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants