From 7db0b8a857bcde79f36f5399c2e32ff0f42bbf93 Mon Sep 17 00:00:00 2001 From: Akhil Kumar Achanta <71771166+ZeusCraft10@users.noreply.github.com> Date: Tue, 16 Sep 2025 15:16:04 -0400 Subject: [PATCH] Fix: retry password prompt on failure to prevent script halting (#3581) * Fix: retry password prompt on failure to prevent script halting * Update Invoke-WPFInstall.ps1 * format fix --- functions/public/Invoke-WPFInstall.ps1 | 76 +++++++++++++++----------- 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/functions/public/Invoke-WPFInstall.ps1 b/functions/public/Invoke-WPFInstall.ps1 index 618d0a5f..7085be7b 100644 --- a/functions/public/Invoke-WPFInstall.ps1 +++ b/functions/public/Invoke-WPFInstall.ps1 @@ -39,42 +39,54 @@ function Invoke-WPFInstall { $user = $env:USERNAME Get-LocalUser | Where-Object Enabled -eq $true | ForEach-Object { - try { - $myPasswordIsBlank = $PrincipalContext.ValidateCredentials($user, $null) - } catch { - $form = New-Object System.Windows.Forms.Form - $form.Text = "Set password for $user" - $form.Size = New-Object System.Drawing.Size(500, 200) + $validPassword = $false + while (-not $validPassword) { + try { + $myPasswordIsBlank = $PrincipalContext.ValidateCredentials($user, $null) + $validPassword = $true + } catch { + $form = New-Object System.Windows.Forms.Form + $form.Text = "Set password for $user" + $form.Size = New-Object System.Drawing.Size(500, 200) - $label = New-Object System.Windows.Forms.Label - $label.Text = 'Maybe a program needs to be installed in "usermode" and you have no password set, you need to set it here. After putting a password into the text box a page asking for your password might open (not right after). If you keep the text box empty, nothing will happen. - REMEMBER THE PASSWORD FOR THE FUTURE. YOU WILL NEED FOR STUFF AND TO LOGIN IF AUTOLOGIN ISN`T SET' - $label.Size = New-Object System.Drawing.Size(480, 60) - $label.Location = New-Object System.Drawing.Point(10, 10) - $form.Controls.Add($label) + $label = New-Object System.Windows.Forms.Label + $label.Text = 'Admin mode install failed. Set a USER password for login and user-based installation.' + $label.Size = New-Object System.Drawing.Size(480, 60) + $label.Location = New-Object System.Drawing.Point(10, 10) + $form.Controls.Add($label) - $passwordBox = New-Object System.Windows.Forms.TextBox - $passwordBox.Size = New-Object System.Drawing.Size(380, 20) - $passwordBox.UseSystemPasswordChar = $true - $passwordBox.Location = New-Object System.Drawing.Point(10, 125) - $form.Controls.Add($passwordBox) + $passwordBox = New-Object System.Windows.Forms.TextBox + $passwordBox.Size = New-Object System.Drawing.Size(380, 20) + $passwordBox.UseSystemPasswordChar = $true + $passwordBox.Location = New-Object System.Drawing.Point(10, 125) + $form.Controls.Add($passwordBox) - $button = New-Object System.Windows.Forms.Button - $button.Text = 'Submit' - $button.Size = New-Object System.Drawing.Size(75, 23) - $button.Location = New-Object System.Drawing.Point(400, 125) - $button.Add_Click({ - $password = $passwordBox.Text | ConvertTo-SecureString -AsPlainText -Force - if ($password) { - Set-LocalUser -Name $user -Password $password - $Form.Close() - } else { - [System.Windows.Forms.MessageBox]::Show('No password entered!') + $button = New-Object System.Windows.Forms.Button + $button.Text = 'Submit' + $button.Size = New-Object System.Drawing.Size(75, 23) + $button.Location = New-Object System.Drawing.Point(400, 125) + $button.Add_Click({ + $password = $passwordBox.Text | ConvertTo-SecureString -AsPlainText -Force + if ($password) { + try { + Set-LocalUser -Name $user -Password $password + $validPassword = $PrincipalContext.ValidateCredentials($user, $passwordBox.Text) + if ($validPassword) { + $form.Close() + } else { + [System.Windows.Forms.MessageBox]::Show('Invalid password! Please try again.') + } + } catch { + [System.Windows.Forms.MessageBox]::Show('Error setting password!') + } + } else { + [System.Windows.Forms.MessageBox]::Show('No password entered!') + } + }) + $form.Controls.Add($button) + $form.ShowDialog() | Out-Null } - }) - $form.Controls.Add($button) - $form.ShowDialog() | Out-Null - } + } } Show-WPFInstallAppBusy -text "Installing apps..."