2023-03-07 12:28:00 -08:00
function Invoke-WPFInstall {
2025-03-01 20:50:12 +01:00
param (
[ Parameter ( Mandatory = $false )]
[ PSObject[] ] $PackagesToInstall = $ ( $sync . selectedApps | Foreach-Object { $sync . configs . applicationsHashtable . $_ })
)
2023-03-07 12:28:00 -08:00
<#
2023-10-19 17:12:55 -05:00
. SYNOPSIS
2024-03-21 16:23:24 -07:00
Installs the selected programs using winget, if one or more of the selected programs are already installed on the system, winget will try and perform an upgrade if there's a newer version to install.
2023-03-07 12:28:00 -08:00
#>
2024-08-06 23:35:17 +03:00
if ( $sync . ProcessRunning ) {
2024-03-21 16:23:24 -07:00
$msg = "[Invoke-WPFInstall] An Install process is currently running."
2023-03-07 12:28:00 -08:00
[ System.Windows.MessageBox ]:: Show ( $msg , "Winutil" , [ System.Windows.MessageBoxButton ]:: OK , [ System.Windows.MessageBoxImage ]:: Warning )
return
}
2024-06-04 20:27:27 -07:00
if ( $PackagesToInstall . Count -eq 0 ) {
2024-03-21 16:23:24 -07:00
$WarningMsg = "Please select the program(s) to install or upgrade"
2023-03-07 12:28:00 -08:00
[ System.Windows.MessageBox ]:: Show ( $WarningMsg , $AppTitle , [ System.Windows.MessageBoxButton ]:: OK , [ System.Windows.MessageBoxImage ]:: Warning )
return
}
2025-04-14 13:33:16 -05:00
$ManagerPreference = $sync [ "ManagerPreference" ]
Invoke-WPFRunspace -ParameterList @ (( "PackagesToInstall" , $PackagesToInstall ),( "ManagerPreference" , $ManagerPreference )) -DebugPreference $DebugPreference -ScriptBlock {
param ( $PackagesToInstall , $ManagerPreference , $DebugPreference )
$packagesSorted = Get-WinUtilSelectedPackages -PackageList $PackagesToInstall -Preference $ManagerPreference
$packagesWinget = $packagesSorted [ [PackageManagers ]:: Winget ]
$packagesChoco = $packagesSorted [ [PackageManagers ]:: Choco ]
2024-01-15 11:32:19 -06:00
2024-08-06 23:35:17 +03:00
try {
2023-03-07 12:28:00 -08:00
$sync . ProcessRunning = $true
2025-08-01 17:13:33 +02:00
if ( $packagesWinget . Count -gt 0 -and $packagesWinget -ne "0" ) {
Add-Type -AssemblyName System . DirectoryServices . AccountManagement
Add-Type -assembly System . Windows . Forms
$PrincipalContext = New-Object System . DirectoryServices . AccountManagement . PrincipalContext ( 'Machine' )
$user = $env:USERNAME
Get-LocalUser | Where-Object Enabled -eq $true | ForEach-Object {
2025-09-16 15:16:04 -04:00
$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 )
2025-08-01 17:13:33 +02:00
2025-09-16 15:16:04 -04:00
$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 )
2025-06-26 19:11:38 +02:00
2025-09-16 15:16:04 -04:00
$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 )
2025-08-01 17:13:33 +02:00
2025-09-16 15:16:04 -04:00
$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
2025-08-01 17:13:33 +02:00
}
2025-09-16 15:16:04 -04:00
}
2025-08-01 17:13:33 +02:00
}
Show-WPFInstallAppBusy -text "Installing apps..."
2024-06-04 20:27:27 -07:00
Install-WinUtilWinget
2024-09-10 20:02:22 +02:00
Install-WinUtilProgramWinget -Action Install -Programs $packagesWinget
2024-06-04 20:27:27 -07:00
}
2024-08-06 23:35:17 +03:00
if ( $packagesChoco . Count -gt 0 ) {
2024-06-04 20:27:27 -07:00
Install-WinUtilChoco
2024-09-10 20:02:22 +02:00
Install-WinUtilProgramChoco -Action Install -Programs $packagesChoco
2024-06-04 20:27:27 -07:00
}
2025-06-26 19:11:38 +02:00
Hide-WPFInstallAppBusy
2023-03-07 12:28:00 -08:00
Write-Host "==========================================="
Write-Host "-- Installs have finished ---"
Write-Host "==========================================="
2024-07-25 23:19:45 +02:00
$sync . form . Dispatcher . Invoke ([ action ]{ Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" })
2024-08-06 23:35:17 +03:00
} catch {
2023-03-07 12:28:00 -08:00
Write-Host "==========================================="
2024-02-07 18:11:40 -06:00
Write-Host "Error: $_ "
2023-03-07 12:28:00 -08:00
Write-Host "==========================================="
2024-07-25 23:19:45 +02:00
$sync . form . Dispatcher . Invoke ([ action ]{ Set-WinUtilTaskbaritem -state "Error" -overlay "warning" })
2023-03-07 12:28:00 -08:00
}
$sync . ProcessRunning = $False
}
2024-03-21 16:23:24 -07:00
}