Files
Powershell/update-all.cmd
T

521 lines
23 KiB
Batchfile

# 2>NUL & @CLS & PUSHD "%~dp0" & "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -noLogo -noProfile -ExecutionPolicy bypass "[IO.File]::ReadAllText('%~f0')|iex" & POPD & EXIT /B
# =============================================================================
# COMPLETE SYSTEM UPDATE SCRIPT
# Dit script installeert en update alle applicaties, drivers en Windows updates
# =============================================================================
# Functie voor logging
function Write-Log {
param(
[string]$Message,
[string]$Level = "INFO"
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logMessage = "[$timestamp] [$Level] $Message"
Write-Host $logMessage
# Log ook naar bestand
try {
# Probeer verschillende methoden om het script pad te bepalen
$scriptPath = $null
# Methode 1: Via $MyInvocation
if ($MyInvocation.ScriptName) {
$scriptPath = Split-Path -Parent $MyInvocation.ScriptName
}
# Methode 2: Via $PSCommandPath
if (-not $scriptPath -and $PSCommandPath) {
$scriptPath = Split-Path -Parent $PSCommandPath
}
# Methode 3: Via $PSScriptRoot
if (-not $scriptPath -and $PSScriptRoot) {
$scriptPath = $PSScriptRoot
}
# Methode 4: Fallback naar huidige directory
if (-not $scriptPath) {
$scriptPath = Get-Location
}
$logFile = Join-Path $scriptPath "update-log.txt"
Add-Content -Path $logFile -Value $logMessage -ErrorAction SilentlyContinue
} catch {
# Als alles faalt, probeer gewoon in de huidige directory
try {
Add-Content -Path "update-log.txt" -Value $logMessage -ErrorAction SilentlyContinue
} catch {
# Laatste fallback - geen logging naar bestand
}
}
}
# Functie voor voortgangsindicator
function Show-Progress {
param(
[string]$Activity,
[string]$Status,
[int]$PercentComplete
)
Write-Progress -Activity $Activity -Status $Status -PercentComplete $PercentComplete
}
# Functie om te controleren of een commando bestaat
function Test-Command {
param([string]$Command)
try {
Get-Command $Command -ErrorAction Stop | Out-Null
return $true
} catch {
return $false
}
}
# Functie om te wachten op gebruiker input
function Wait-ForUser {
param([string]$Message = "Druk op Enter om door te gaan...")
Write-Host $Message -ForegroundColor Yellow
Read-Host
}
# =============================================================================
# HOOFDSCRIPT START
# =============================================================================
Write-Host "================================================" -ForegroundColor Cyan
Write-Host " COMPLETE SYSTEM UPDATE SCRIPT" -ForegroundColor Cyan
Write-Host " Alle applicaties, drivers en Windows updates" -ForegroundColor Cyan
Write-Host "================================================" -ForegroundColor Cyan
Write-Host ""
Write-Log "Script gestart" "INFO"
# Controleer administrator rechten
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
if (-not $isAdmin) {
Write-Log "Script moet als Administrator worden uitgevoerd" "ERROR"
Write-Host "Dit script moet als Administrator worden uitgevoerd!" -ForegroundColor Red
Write-Host "Start PowerShell opnieuw als Administrator en voer dit script opnieuw uit." -ForegroundColor Yellow
Wait-ForUser
exit 1
}
Write-Log "Administrator rechten bevestigd" "INFO"
# =============================================================================
# STAP 1: DEPENDENCY CONTROLES
# =============================================================================
Write-Host "STAP 1: Controleer dependencies..." -ForegroundColor Green
Show-Progress -Activity "Dependency Controle" -Status "Controleer winget..." -PercentComplete 10
# Controleer winget
try {
$wingetVersion = winget --version 2>$null
if ($wingetVersion) {
Write-Log "winget is beschikbaar: $wingetVersion" "INFO"
Write-Host "✓ winget is beschikbaar: $wingetVersion" -ForegroundColor Green
} else {
throw "winget niet gevonden"
}
} catch {
Write-Log "winget niet gevonden, probeer te installeren" "WARNING"
Write-Host "⚠ winget niet gevonden. Probeer te installeren..." -ForegroundColor Yellow
try {
Write-Host "Installeer winget via Microsoft Store..." -ForegroundColor Yellow
Start-Process "ms-windows-store://pdp/?ProductId=9NBLGGH4NNS1" -Wait -ErrorAction SilentlyContinue
Start-Sleep -Seconds 5
$wingetVersion = winget --version 2>$null
if ($wingetVersion) {
Write-Log "winget succesvol geïnstalleerd: $wingetVersion" "INFO"
Write-Host "✓ winget succesvol geïnstalleerd: $wingetVersion" -ForegroundColor Green
} else {
throw "winget installatie mislukt"
}
} catch {
Write-Log "winget installatie mislukt" "ERROR"
Write-Host "✗ Automatische winget installatie mislukt." -ForegroundColor Red
Write-Host "Probeer handmatig:" -ForegroundColor White
Write-Host "1. Open Microsoft Store" -ForegroundColor White
Write-Host "2. Zoek naar 'App Installer'" -ForegroundColor White
Write-Host "3. Installeer de App Installer" -ForegroundColor White
Wait-ForUser
exit 1
}
}
# Controleer Windows Update service
Show-Progress -Activity "Dependency Controle" -Status "Controleer Windows Update service..." -PercentComplete 20
try {
$wuService = Get-Service -Name "wuauserv" -ErrorAction SilentlyContinue
if ($wuService -and $wuService.Status -ne "Running") {
Write-Log "Start Windows Update service" "INFO"
Start-Service -Name "wuauserv" -ErrorAction SilentlyContinue
Write-Host "✓ Windows Update service gestart" -ForegroundColor Green
} elseif ($wuService) {
Write-Host "✓ Windows Update service is actief" -ForegroundColor Green
} else {
Write-Host "⚠ Windows Update service niet beschikbaar" -ForegroundColor Yellow
}
} catch {
Write-Log "Windows Update service probleem" "WARNING"
Write-Host "⚠ Windows Update service probleem" -ForegroundColor Yellow
}
# =============================================================================
# STAP 2: WINDOWS UPDATES
# =============================================================================
Write-Host ""
Write-Host "STAP 2: Windows Updates..." -ForegroundColor Green
Show-Progress -Activity "Windows Updates" -Status "Controleer beschikbare updates..." -PercentComplete 30
try {
Write-Log "Start Windows Update controle" "INFO"
Write-Host "Controleer beschikbare Windows updates..." -ForegroundColor Cyan
# Controleer updates
$updateSession = New-Object -ComObject Microsoft.Update.Session -ErrorAction SilentlyContinue
if ($updateSession) {
$updateSearcher = $updateSession.CreateUpdateSearcher()
$searchResult = $updateSearcher.Search("IsInstalled=0")
if ($searchResult.Updates.Count -gt 0) {
Write-Host "✓ $($searchResult.Updates.Count) Windows updates gevonden" -ForegroundColor Green
Write-Log "Gevonden $($searchResult.Updates.Count) Windows updates" "INFO"
# Toon details van elke update
Write-Host "Gevonden updates:" -ForegroundColor Cyan
for ($i = 0; $i -lt $searchResult.Updates.Count; $i++) {
$update = $searchResult.Updates.Item($i)
$updateNumber = $i + 1
$kbNumber = "KB" + $update.KBArticleIDs.Item(0)
Write-Host "$updateNumber. $kbNumber - $($update.Title)" -ForegroundColor White
}
Write-Host ""
# Download en installeer updates
Show-Progress -Activity "Windows Updates" -Status "Download en installeer updates..." -PercentComplete 40
$updatesToDownload = New-Object -ComObject Microsoft.Update.UpdateColl
foreach ($update in $searchResult.Updates) {
if ($update.IsDownloaded -eq $false) {
$updatesToDownload.Add($update)
}
}
if ($updatesToDownload.Count -gt 0) {
Write-Host "Download $($updatesToDownload.Count) updates..." -ForegroundColor Cyan
$downloader = $updateSession.CreateUpdateDownloader()
$downloader.Updates = $updatesToDownload
$downloadResult = $downloader.Download()
if ($downloadResult.ResultCode -eq 2) {
Write-Host "✓ Updates succesvol gedownload" -ForegroundColor Green
Write-Log "Windows updates gedownload" "INFO"
} else {
Write-Host "⚠ Download gedeeltelijk voltooid" -ForegroundColor Yellow
Write-Log "Windows updates gedeeltelijk gedownload" "WARNING"
}
}
# Installeer updates
$updatesToInstall = New-Object -ComObject Microsoft.Update.UpdateColl
foreach ($update in $searchResult.Updates) {
if ($update.IsDownloaded -eq $true) {
$updatesToInstall.Add($update)
}
}
if ($updatesToInstall.Count -gt 0) {
Write-Host "Installeer $($updatesToInstall.Count) updates..." -ForegroundColor Cyan
$installer = $updateSession.CreateUpdateInstaller()
$installer.Updates = $updatesToInstall
$installResult = $installer.Install()
if ($installResult.ResultCode -eq 2) {
Write-Host "✓ Windows updates succesvol geïnstalleerd" -ForegroundColor Green
Write-Log "Windows updates geïnstalleerd" "INFO"
} else {
Write-Host "⚠ Installatie gedeeltelijk voltooid" -ForegroundColor Yellow
Write-Log "Windows updates gedeeltelijk geïnstalleerd" "WARNING"
}
}
} else {
Write-Host "✓ Geen Windows updates beschikbaar" -ForegroundColor Green
Write-Log "Geen Windows updates beschikbaar" "INFO"
}
} else {
Write-Host "⚠ Windows Update service niet beschikbaar" -ForegroundColor Yellow
Write-Log "Windows Update service niet beschikbaar" "WARNING"
}
} catch {
Write-Log "Windows Update fout" "ERROR"
Write-Host "✗ Fout bij Windows updates" -ForegroundColor Red
}
# =============================================================================
# STAP 3: DRIVER UPDATES
# =============================================================================
Write-Host ""
Write-Host "STAP 3: Driver Updates..." -ForegroundColor Green
Show-Progress -Activity "Driver Updates" -Status "Controleer driver updates..." -PercentComplete 50
# Dell Command Update (als beschikbaar)
if (Test-Command "DellCommandUpdate") {
try {
Write-Host "Controleer Dell driver updates..." -ForegroundColor Cyan
Write-Log "Start Dell Command Update" "INFO"
# Voer Dell Command Update uit
$dellProcess = Start-Process -FilePath "DellCommandUpdate" -ArgumentList "/silent" -PassThru -Wait -ErrorAction SilentlyContinue
if ($dellProcess -and $dellProcess.ExitCode -eq 0) {
Write-Host "✓ Dell driver updates voltooid" -ForegroundColor Green
Write-Log "Dell driver updates voltooid" "INFO"
} else {
Write-Host "⚠ Dell driver updates gedeeltelijk voltooid" -ForegroundColor Yellow
Write-Log "Dell driver updates gedeeltelijk voltooid" "WARNING"
}
} catch {
Write-Log "Dell Command Update fout" "ERROR"
Write-Host "✗ Fout bij Dell driver updates" -ForegroundColor Red
}
} else {
Write-Host "⚠ Dell Command Update niet gevonden" -ForegroundColor Yellow
Write-Log "Dell Command Update niet beschikbaar" "WARNING"
}
# Windows Driver Updates via winget
try {
Write-Host "Controleer Windows driver updates via winget..." -ForegroundColor Cyan
Show-Progress -Activity "Driver Updates" -Status "Update drivers via winget..." -PercentComplete 60
# Zoek naar driver updates
$driverUpdates = winget upgrade --source winget --include-unknown 2>$null | Select-String "driver|Driver"
if ($driverUpdates) {
Write-Host "✓ Driver updates gevonden via winget" -ForegroundColor Green
Write-Log "Driver updates gevonden via winget" "INFO"
# Installeer driver updates
winget upgrade --all --silent --accept-source-agreements --accept-package-agreements --include-unknown 2>$null
Write-Host "✓ Driver updates geïnstalleerd" -ForegroundColor Green
Write-Log "Driver updates geïnstalleerd via winget" "INFO"
} else {
Write-Host "✓ Geen driver updates beschikbaar via winget" -ForegroundColor Green
Write-Log "Geen driver updates beschikbaar via winget" "INFO"
}
} catch {
Write-Log "Driver update fout" "ERROR"
Write-Host "✗ Fout bij driver updates" -ForegroundColor Red
}
# =============================================================================
# STAP 4: APPLICATIE UPDATES
# =============================================================================
Write-Host ""
Write-Host "STAP 4: Applicatie Updates..." -ForegroundColor Green
Show-Progress -Activity "Applicatie Updates" -Status "Update alle applicaties..." -PercentComplete 70
try {
Write-Host "Controleer beschikbare applicatie updates..." -ForegroundColor Cyan
Write-Log "Start applicatie updates via winget" "INFO"
# Controleer eerst welke updates beschikbaar zijn
Write-Host "Zoek naar beschikbare updates..." -ForegroundColor Yellow
$availableUpdates = winget upgrade --source winget --include-unknown 2>$null
if ($availableUpdates -and $availableUpdates -match "No applicable updates found" -eq $false) {
# Parse de output om updates te vinden
$updateLines = $availableUpdates | Where-Object { $_ -match "^\w+\." -and $_ -notmatch "No applicable updates found" }
if ($updateLines.Count -gt 0) {
Write-Host "✓ $($updateLines.Count) applicatie updates gevonden" -ForegroundColor Green
Write-Log "Gevonden $($updateLines.Count) applicatie updates" "INFO"
# Toon details van elke update
Write-Host "Gevonden applicatie updates:" -ForegroundColor Cyan
$updateNumber = 1
foreach ($line in $updateLines) {
if ($line -match "^\w+\.") {
# Parse de regel om app naam en versie te extraheren
$parts = $line -split "\s+"
if ($parts.Count -ge 3) {
$appName = $parts[0]
$currentVersion = $parts[1]
$newVersion = $parts[2]
Write-Host "$updateNumber. $appName ($currentVersion → $newVersion)" -ForegroundColor White
$updateNumber++
}
}
}
Write-Host ""
} else {
Write-Host "✓ Geen applicatie updates beschikbaar" -ForegroundColor Green
Write-Log "Geen applicatie updates beschikbaar" "INFO"
}
} else {
Write-Host "✓ Geen applicatie updates beschikbaar" -ForegroundColor Green
Write-Log "Geen applicatie updates beschikbaar" "INFO"
}
# Voer winget upgrade uit voor alle applicaties
Write-Host "Update alle applicaties via winget..." -ForegroundColor Cyan
$upgradeResult = winget upgrade --all --silent --accept-source-agreements --accept-package-agreements --include-unknown 2>$null
if ($LASTEXITCODE -eq 0) {
Write-Host "✓ Alle applicaties succesvol geüpdatet" -ForegroundColor Green
Write-Log "Applicatie updates voltooid" "INFO"
} else {
Write-Host "⚠ Applicatie updates gedeeltelijk voltooid" -ForegroundColor Yellow
Write-Log "Applicatie updates gedeeltelijk voltooid" "WARNING"
}
} catch {
Write-Log "Applicatie update fout" "ERROR"
Write-Host "✗ Fout bij applicatie updates" -ForegroundColor Red
}
# =============================================================================
# STAP 5: OPTIONELE APPLICATIE INSTALLATIES
# =============================================================================
Write-Host ""
Write-Host "STAP 5: Optionele Applicatie Installaties..." -ForegroundColor Green
Show-Progress -Activity "Applicatie Installaties" -Status "Installeer essentiële applicaties..." -PercentComplete 80
# Lijst van essentiële applicaties
$essentialApps = @(
"Microsoft.DotNet.DesktopRuntime.8",
"Google.Chrome",
"Google.GoogleDrive",
"Google.ChromeRemoteDesktopHost",
"Dell.CommandUpdate",
"Logitech.OptionsPlus",
"Notepad++.Notepad++"
)
$totalApps = $essentialApps.Count
$currentApp = 0
foreach ($app in $essentialApps) {
$currentApp++
$percentComplete = 80 + (($currentApp / $totalApps) * 15)
Show-Progress -Activity "Applicatie Installaties" -Status "Installeer $app..." -PercentComplete $percentComplete
Write-Host "[$currentApp/$totalApps] Controleer $app..." -ForegroundColor Cyan
try {
# Controleer of app al geïnstalleerd is
$installed = winget list --id $app --exact 2>$null
if (-not $installed -or $installed -match "No installed package found") {
Write-Host "Installeer $app..." -ForegroundColor Yellow
winget install --id $app -e --silent --accept-package-agreements --accept-source-agreements --source winget --scope machine 2>$null
Write-Host "✓ $app succesvol geïnstalleerd" -ForegroundColor Green
Write-Log "Applicatie geïnstalleerd: $app" "INFO"
} else {
Write-Host "✓ $app is al geïnstalleerd" -ForegroundColor Green
}
} catch {
Write-Host "✗ Fout bij installatie van $app" -ForegroundColor Red
Write-Log "Fout bij installatie van $app" "ERROR"
}
}
# =============================================================================
# STAP 6: SYSTEEM OPTIMALISATIE
# =============================================================================
Write-Host ""
Write-Host "STAP 6: Systeem Optimalisatie..." -ForegroundColor Green
Show-Progress -Activity "Systeem Optimalisatie" -Status "Voer systeem optimalisatie uit..." -PercentComplete 95
try {
Write-Host "Voer systeem optimalisatie uit..." -ForegroundColor Cyan
Write-Log "Start systeem optimalisatie" "INFO"
# Schoon Windows Update cache
Write-Host "Schoon Windows Update cache..." -ForegroundColor Yellow
Stop-Service -Name "wuauserv" -Force -ErrorAction SilentlyContinue
Remove-Item -Path "$env:SystemRoot\SoftwareDistribution\Download\*" -Recurse -Force -ErrorAction SilentlyContinue
Start-Service -Name "wuauserv" -ErrorAction SilentlyContinue
Write-Host "✓ Windows Update cache opgeschoond" -ForegroundColor Green
# Schoon temp bestanden
Write-Host "Schoon temp bestanden..." -ForegroundColor Yellow
Get-ChildItem -Path $env:TEMP -Recurse -Force -ErrorAction SilentlyContinue | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "✓ Temp bestanden opgeschoond" -ForegroundColor Green
# Voer DISM health check uit
Write-Host "Voer DISM health check uit..." -ForegroundColor Yellow
DISM /Online /Cleanup-Image /CheckHealth /NoRestart 2>$null | Out-Null
DISM /Online /Cleanup-Image /ScanHealth /NoRestart 2>$null | Out-Null
Write-Host "✓ DISM health check voltooid" -ForegroundColor Green
Write-Log "Systeem optimalisatie voltooid" "INFO"
} catch {
Write-Log "Systeem optimalisatie fout" "ERROR"
Write-Host "✗ Fout bij systeem optimalisatie" -ForegroundColor Red
}
# =============================================================================
# VOLTOOIING
# =============================================================================
Show-Progress -Activity "Update Proces" -Status "Voltooid!" -PercentComplete 100
Write-Host ""
Write-Host "================================================" -ForegroundColor Green
Write-Host " UPDATE PROCES VOLTOOID!" -ForegroundColor Green
Write-Host "================================================" -ForegroundColor Green
Write-Host ""
Write-Log "Update proces voltooid" "INFO"
Write-Host "Samenvatting:" -ForegroundColor Cyan
Write-Host "✓ Windows Updates gecontroleerd en geïnstalleerd" -ForegroundColor Green
Write-Host "✓ Driver Updates gecontroleerd en geïnstalleerd" -ForegroundColor Green
Write-Host "✓ Applicatie Updates uitgevoerd" -ForegroundColor Green
Write-Host "✓ Essentiële applicaties geïnstalleerd" -ForegroundColor Green
Write-Host "✓ Systeem optimalisatie uitgevoerd" -ForegroundColor Green
Write-Host ""
# Bepaal log bestand locatie
$logPath = "update-log.txt"
try {
if ($MyInvocation.ScriptName) {
$scriptPath = Split-Path -Parent $MyInvocation.ScriptName
$logPath = Join-Path $scriptPath "update-log.txt"
} elseif ($PSCommandPath) {
$scriptPath = Split-Path -Parent $PSCommandPath
$logPath = Join-Path $scriptPath "update-log.txt"
} elseif ($PSScriptRoot) {
$logPath = Join-Path $PSScriptRoot "update-log.txt"
}
} catch {
# Fallback naar huidige directory
}
Write-Host "Log bestand opgeslagen in: $logPath" -ForegroundColor Yellow
Write-Host ""
# Vraag of gebruiker wil herstarten
$restart = Read-Host "Wil je de computer nu herstarten? (j/n)"
if ($restart -eq "j" -or $restart -eq "J" -or $restart -eq "y" -or $restart -eq "Y") {
Write-Host "Computer wordt herstart in 10 seconden..." -ForegroundColor Yellow
Write-Log "Computer herstart gepland" "INFO"
Start-Sleep -Seconds 10
Restart-Computer -Force
} else {
Write-Host "Herstart later handmatig om alle updates volledig toe te passen." -ForegroundColor Yellow
Wait-ForUser
}
Write-Log "Script beëindigd" "INFO"