Skip to content
Snippets Groups Projects
Select Git revision
  • 1c3d2d2421acf7ebcdaa89944b66f9bc9a8212b3
  • main default
2 results

assignment_tests.sh

Blame
  • wingetInstaller.ps1 6.71 KiB
    # Self-elevate the script if required
    if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
        if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
            # PowerShell 5 and later
            $CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
            Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine
            Exit
        }
    }
    
    # List of applications to install
    $applications = @(
        # Applications available via PatchMyPC
        "dotPDN.PaintDotNet",
        "notepad++.notepad++",
        "PuTTY.PuTTY",
        "Posit.RStudio",
        "Microsoft.VisualStudioCode",
        "VideoLAN.VLC",
        "Mozilla.Firefox",
        "WiresharkFoundation.Wireshark",
        "WinSCP.WinSCP",
        "7zip.7zip",
        "HandBrake.HandBrake",
        "OBSProject.OBSStudio",
     
        # Applications to install manually or with winget
        "Zoom.Zoom",
        "JetBrains.IntelliJIDEA.Community",
        "AivarAnnamaa.Thonny",
        "Insecure.Nmap",
        "JetBrains.PyCharm.Community",
        "Racket.Racket"
    )
    
    # Initialize status tracking hashtables
    $installationStatus = @{}
    $updateStatus = @{}
    $errorMessages = @{}
    
    # Function to check if an application is installed
    function Test-ApplicationInstalled {
        param (
            [string]$AppId
        )
        
        $listResult = winget list --id $AppId --accept-source-agreements 2>&1
        return $listResult -like "*$AppId*"
    }
    
    # Function to check if an application has updates available
    function Test-UpdateAvailable {
        param (
            [string]$AppId
        )
        
        $upgradeResult = winget upgrade --id $AppId 2>&1
        return $upgradeResult -notlike "*No applicable upgrade found.*"
    }
    
    # Add a start message
    Write-Host "Starting installation/update process..." -ForegroundColor Green
    Write-Host "This script will install or update the following applications:" -ForegroundColor Yellow
    $applications | ForEach-Object { Write-Host "  - $_" -ForegroundColor Cyan }
    Write-Host ""
    
    # Counter for tracking progress
    $totalApps = $applications.Count
    $currentApp = 0
    
    # Iterate through the applications and install or update each
    foreach ($app in $applications) {
        $currentApp++
        Write-Host "`nProcessing ($currentApp/$totalApps): $app..." -ForegroundColor Yellow
        
        try {
            # Check if the application is already installed
            $wasInstalled = Test-ApplicationInstalled -AppId $app
            if ($wasInstalled) {
                Write-Host "$app is already installed. Checking for updates..." -ForegroundColor Cyan
                
                # Check if updates are available
                if (Test-UpdateAvailable -AppId $app) {
                    Write-Host "Update available for $app. Updating..." -ForegroundColor Green
                    winget upgrade --id $app --silent --accept-package-agreements --accept-source-agreements
                    
                    # Verify update
                    if (-not (Test-UpdateAvailable -AppId $app)) {
                        $updateStatus[$app] = "Success"
                        Write-Host "$app updated successfully!" -ForegroundColor Green
                    } else {
                        $updateStatus[$app] = "Failed"
                        Write-Host "Update may have failed for $app" -ForegroundColor Yellow
                    }
                } else {
                    $updateStatus[$app] = "UpToDate"
                    Write-Host "$app is up to date." -ForegroundColor Cyan
                }
            } else {
                Write-Host "$app is not installed. Installing..." -ForegroundColor Green
                winget install --id $app --silent --accept-package-agreements --accept-source-agreements
                
                # Verify installation
                if (Test-ApplicationInstalled -AppId $app) {
                    $installationStatus[$app] = "Success"
                    Write-Host "$app installed successfully!" -ForegroundColor Green
                } else {
                    $installationStatus[$app] = "Failed"
                    Write-Host "Installation may have failed for $app" -ForegroundColor Red
                }
            }
        } catch {
            $errorMessages[$app] = $_.Exception.Message
            Write-Host "Failed to process $app. Error: $_" -ForegroundColor Red
        }
    }
    
    # Generate and display summary
    Write-Host "`n===============================" -ForegroundColor Yellow
    Write-Host "Installation/Update Summary" -ForegroundColor Yellow
    Write-Host "===============================" -ForegroundColor Yellow
    
    # Show successful installations
    $successfulInstalls = $installationStatus.GetEnumerator() | Where-Object { $_.Value -eq "Success" }
    if ($successfulInstalls) {
        Write-Host "`nSuccessfully Installed:" -ForegroundColor Green
        $successfulInstalls | ForEach-Object { Write-Host "  - $($_.Key)" -ForegroundColor Cyan }
    }
    
    # Show failed installations
    $failedInstalls = $installationStatus.GetEnumerator() | Where-Object { $_.Value -eq "Failed" }
    if ($failedInstalls) {
        Write-Host "`nFailed Installations (May Need Manual Installation):" -ForegroundColor Red
        $failedInstalls | ForEach-Object { 
            Write-Host "  - $($_.Key)" -ForegroundColor Red
            if ($errorMessages.ContainsKey($_.Key)) {
                Write-Host "    Error: $($errorMessages[$_.Key])" -ForegroundColor Red
            }
        }
    }
    
    # Show successful updates
    $successfulUpdates = $updateStatus.GetEnumerator() | Where-Object { $_.Value -eq "Success" }
    if ($successfulUpdates) {
        Write-Host "`nSuccessfully Updated:" -ForegroundColor Green
        $successfulUpdates | ForEach-Object { Write-Host "  - $($_.Key)" -ForegroundColor Cyan }
    }
    
    # Show up-to-date applications
    $upToDate = $updateStatus.GetEnumerator() | Where-Object { $_.Value -eq "UpToDate" }
    if ($upToDate) {
        Write-Host "`nAlready Up To Date:" -ForegroundColor Cyan
        $upToDate | ForEach-Object { Write-Host "  - $($_.Key)" -ForegroundColor Cyan }
    }
    
    # Show failed updates
    $failedUpdates = $updateStatus.GetEnumerator() | Where-Object { $_.Value -eq "Failed" }
    if ($failedUpdates) {
        Write-Host "`nFailed Updates:" -ForegroundColor Red
        $failedUpdates | ForEach-Object { 
            Write-Host "  - $($_.Key)" -ForegroundColor Red
            if ($errorMessages.ContainsKey($_.Key)) {
                Write-Host "    Error: $($errorMessages[$_.Key])" -ForegroundColor Red
            }
        }
    }
    
    Write-Host "`nProcess completed! Please review the summary above." -ForegroundColor Yellow
    Write-Host "For failed installations or updates, please try installing manually." -ForegroundColor Yellow
    
    # Keep the terminal open
    Write-Host "`nPress any key to exit..." -ForegroundColor Cyan
    [System.Console]::ReadKey() > $null