37 lines
1.5 KiB
PowerShell
37 lines
1.5 KiB
PowerShell
# Upgrade-WingetMsStore.ps1
|
|
# Runs winget upgrade for all MS Store packages daily and logs the output.
|
|
|
|
$LogFile = Join-Path $PSScriptRoot "Upgrade-WingetMsStore.log"
|
|
|
|
function Log-Message {
|
|
param([string]$Message)
|
|
$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
|
$LogLine = "[$Timestamp] $Message"
|
|
Write-Output $LogLine
|
|
Add-Content -Path $LogFile -Value $LogLine -ErrorAction SilentlyContinue
|
|
}
|
|
|
|
Log-Message "=================================================="
|
|
Log-Message "Starting WinGet MS Store upgrade task..."
|
|
Log-Message "=================================================="
|
|
|
|
# Check if winget is available in the environment path
|
|
if (Get-Command winget -ErrorAction SilentlyContinue) {
|
|
Log-Message "WinGet found. Executing upgrade command..."
|
|
|
|
try {
|
|
# Execute winget upgrade and append all output streams (stdout, stderr) to the log file
|
|
& winget upgrade --all --source msstore --accept-package-agreements --accept-source-agreements 2>&1 | Add-Content -Path $LogFile -ErrorAction SilentlyContinue
|
|
Log-Message "WinGet upgrade completed. Exit code: $LastExitCode"
|
|
}
|
|
catch {
|
|
Log-Message "An error occurred while executing winget: $_"
|
|
}
|
|
} else {
|
|
Log-Message "Error: winget command not found in the current environment path."
|
|
}
|
|
|
|
Log-Message "=================================================="
|
|
Log-Message "WinGet MS Store upgrade task finished."
|
|
Log-Message "=================================================="
|