Files
Powershell/scripts/update-all.cmd
T

571 lines
27 KiB
Batchfile
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 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.MyCommand.Path
if ($MyInvocation.MyCommand.Path) {
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
}
# Methode 2: Via $MyInvocation.ScriptName
if (-not $scriptPath -and $MyInvocation.ScriptName) {
$scriptPath = Split-Path -Parent $MyInvocation.ScriptName
}
# Methode 3: Via $PSCommandPath
if (-not $scriptPath -and $PSCommandPath) {
$scriptPath = Split-Path -Parent $PSCommandPath
}
# Methode 4: Via $PSScriptRoot
if (-not $scriptPath -and $PSScriptRoot) {
$scriptPath = $PSScriptRoot
}
# Methode 5: Fallback naar huidige directory (when executed via .cmd wrapper)
if (-not $scriptPath) {
$location = Get-Location
if ($location -is [System.IO.DirectoryInfo]) {
$scriptPath = $location.FullName
} else {
$scriptPath = $location.ToString()
}
}
$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
$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
Write-Host ""
# Download updates één voor één met voortgangsbalk
$downloadedCount = 0
$successCount = 0
foreach ($update in $updatesToDownload) {
$downloadedCount++
$kbNumber = if ($update.KBArticleIDs.Count -gt 0) { "KB" + $update.KBArticleIDs.Item(0) } else { "Update" }
$updateTitle = if ($update.Title.Length -gt 60) { $update.Title.Substring(0, 57) + "..." } else { $update.Title }
$percentComplete = [math]::Round(((($downloadedCount - 1) / $updatesToDownload.Count) * 100))
Show-Progress -Activity "Windows Updates Downloaden" -Status "[$downloadedCount/$($updatesToDownload.Count)] $kbNumber - $updateTitle" -PercentComplete $percentComplete
Write-Host "[$downloadedCount/$($updatesToDownload.Count)] Download: $kbNumber" -ForegroundColor Cyan
# Maak een nieuwe downloader voor deze update
$singleUpdateDownloader = $updateSession.CreateUpdateDownloader()
$singleUpdateColl = New-Object -ComObject Microsoft.Update.UpdateColl
$singleUpdateColl.Add($update) | Out-Null
$singleUpdateDownloader.Updates = $singleUpdateColl
$singleDownloadResult = $singleUpdateDownloader.Download()
# Update voortgangsbalk na download
$percentComplete = [math]::Round(($downloadedCount / $updatesToDownload.Count) * 100)
Show-Progress -Activity "Windows Updates Downloaden" -Status "[$downloadedCount/$($updatesToDownload.Count)] $kbNumber - Voltooid" -PercentComplete $percentComplete
if ($singleDownloadResult.ResultCode -eq 2) {
Write-Host " ✓ Succesvol gedownload" -ForegroundColor Green
$successCount++
} else {
Write-Host " ⚠ Gedeeltelijk gedownload (Code: $($singleDownloadResult.ResultCode))" -ForegroundColor Yellow
}
Write-Host ""
}
Write-Progress -Activity "Windows Updates Downloaden" -Status "Voltooid!" -PercentComplete 100
Start-Sleep -Milliseconds 500
Write-Progress -Activity "Windows Updates Downloaden" -Completed
if ($successCount -eq $updatesToDownload.Count) {
Write-Host "✓ Alle $successCount updates succesvol gedownload" -ForegroundColor Green
Write-Log "Windows updates gedownload" "INFO"
} else {
Write-Host "⚠ $successCount van $($updatesToDownload.Count) updates gedownload" -ForegroundColor Yellow
Write-Log "Windows updates gedeeltelijk gedownload" "WARNING"
}
Write-Host ""
}
# 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
Write-Host ""
# Installeer updates één voor één met voortgangsbalk
$installedCount = 0
$successCount = 0
foreach ($update in $updatesToInstall) {
$installedCount++
$kbNumber = if ($update.KBArticleIDs.Count -gt 0) { "KB" + $update.KBArticleIDs.Item(0) } else { "Update" }
$updateTitle = if ($update.Title.Length -gt 60) { $update.Title.Substring(0, 57) + "..." } else { $update.Title }
$percentComplete = [math]::Round(((($installedCount - 1) / $updatesToInstall.Count) * 100))
Show-Progress -Activity "Windows Updates Installeren" -Status "[$installedCount/$($updatesToInstall.Count)] $kbNumber - $updateTitle" -PercentComplete $percentComplete
Write-Host "[$installedCount/$($updatesToInstall.Count)] Installeer: $kbNumber" -ForegroundColor Cyan
# Maak een nieuwe installer voor deze update
$singleUpdateInstaller = $updateSession.CreateUpdateInstaller()
$singleInstallColl = New-Object -ComObject Microsoft.Update.UpdateColl
$singleInstallColl.Add($update) | Out-Null
$singleUpdateInstaller.Updates = $singleInstallColl
$singleInstallResult = $singleUpdateInstaller.Install()
# Update voortgangsbalk na installatie
$percentComplete = [math]::Round(($installedCount / $updatesToInstall.Count) * 100)
Show-Progress -Activity "Windows Updates Installeren" -Status "[$installedCount/$($updatesToInstall.Count)] $kbNumber - Voltooid" -PercentComplete $percentComplete
if ($singleInstallResult.ResultCode -eq 2) {
Write-Host " ✓ Succesvol geïnstalleerd" -ForegroundColor Green
$successCount++
} elseif ($singleInstallResult.ResultCode -eq 3) {
Write-Host " ℹ Herstart vereist (Code: $($singleInstallResult.ResultCode))" -ForegroundColor Yellow
$successCount++
} else {
Write-Host " ⚠ Gedeeltelijk geïnstalleerd (Code: $($singleInstallResult.ResultCode))" -ForegroundColor Yellow
}
Write-Host ""
}
Write-Progress -Activity "Windows Updates Installeren" -Status "Voltooid!" -PercentComplete 100
Start-Sleep -Milliseconds 500
Write-Progress -Activity "Windows Updates Installeren" -Completed
if ($successCount -eq $updatesToInstall.Count) {
Write-Host "✓ Alle $successCount updates succesvol geïnstalleerd" -ForegroundColor Green
Write-Log "Windows updates geïnstalleerd" "INFO"
} else {
Write-Host "⚠ $successCount van $($updatesToInstall.Count) updates geïnstalleerd" -ForegroundColor Yellow
Write-Log "Windows updates gedeeltelijk geïnstalleerd" "WARNING"
}
Write-Host ""
}
} 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: SYSTEEM OPTIMALISATIE
# =============================================================================
Write-Host ""
Write-Host "STAP 5: Systeem Optimalisatie..." -ForegroundColor Green
Show-Progress -Activity "Systeem Optimalisatie" -Status "Voer systeem optimalisatie uit..." -PercentComplete 80
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 "✓ Systeem optimalisatie uitgevoerd" -ForegroundColor Green
Write-Host ""
# Bepaal log bestand locatie
$logPath = "update-log.txt"
try {
$scriptPath = $null
# Try multiple methods to determine script path
if ($MyInvocation.MyCommand.Path) {
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
} elseif ($MyInvocation.ScriptName) {
$scriptPath = Split-Path -Parent $MyInvocation.ScriptName
} elseif ($PSCommandPath) {
$scriptPath = Split-Path -Parent $PSCommandPath
} elseif ($PSScriptRoot) {
$scriptPath = $PSScriptRoot
} else {
# Fallback: when executed via .cmd wrapper, PUSHD sets current directory
$location = Get-Location
if ($location -is [System.IO.DirectoryInfo]) {
$scriptPath = $location.FullName
} else {
$scriptPath = $location.ToString()
}
}
if ($scriptPath) {
$logPath = Join-Path $scriptPath "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"