wingetautoupdate/Winget-AutoUpdate-Install.ps1

224 lines
9.4 KiB
PowerShell
Raw Normal View History

2022-02-12 15:35:51 +00:00
<#
.SYNOPSIS
Configure Winget to daily update installed apps.
.DESCRIPTION
Install powershell scripts and scheduled task to daily run Winget upgrade and notify connected users.
Possible to exclude apps from auto-update
https://github.com/Romanitho/Winget-AutoUpdate
.PARAMETER Silent
Install Winget-AutoUpdate and prerequisites silently
.PARAMETER WingetUpdatePath
Specify Winget-AutoUpdate installation localtion. Default: C:\ProgramData\Winget-AutoUpdate\
2022-02-12 15:35:51 +00:00
.PARAMETER DoNotUpdate
2022-03-07 01:35:40 +00:00
Do not run Winget-AutoUpdate after installation. By default, Winget-AutoUpdate is run just after installation.
.PARAMETER DisableWAUAutoUpdate
Disable Winget-AutoUpdate update checking. By default, WAU auto update if new version is available on Github.
2022-02-12 15:35:51 +00:00
2022-04-02 17:24:18 +00:00
.PARAMETER Uninstall
Remove scheduled tasks and scripts.
2022-02-12 15:35:51 +00:00
.EXAMPLE
.\winget-install-and-update.ps1 -Silent -DoNotUpdate
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$False)] [Alias('S')] [Switch] $Silent = $false,
[Parameter(Mandatory=$False)] [Alias('Path')] [String] $WingetUpdatePath = "$env:ProgramData\Winget-AutoUpdate",
2022-03-07 01:35:40 +00:00
[Parameter(Mandatory=$False)] [Switch] $DoNotUpdate = $false,
2022-04-02 17:24:18 +00:00
[Parameter(Mandatory=$False)] [Switch] $DisableWAUAutoUpdate = $false,
[Parameter(Mandatory=$False)] [Switch] $Uninstall = $false
2022-02-12 15:35:51 +00:00
)
<# FUNCTIONS #>
2022-03-26 07:49:53 +00:00
function Install-Prerequisites{
2022-02-12 15:35:51 +00:00
#Check if Visual C++ 2019 installed
$app = "Microsoft Visual C++*2019*"
2022-02-20 21:52:10 +00:00
$path = Get-Item HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object { $_.GetValue("DisplayName") -like $app}
2022-02-12 15:35:51 +00:00
#If not installed, ask for installation
if (!($path)){
#If -silent option, force installation
if ($Silent){
$InstallApp = "y"
}
else{
#Ask for installation
while("y","n" -notcontains $InstallApp){
$InstallApp = Read-Host "[Prerequisite for Winget] Microsoft Visual C++ 2019 is not installed. Would you like to install it? [Y/N]"
}
}
if ($InstallApp -eq "y"){
try{
if((Get-CimInStance Win32_OperatingSystem).OSArchitecture -like "*64*"){
$OSArch = "x64"
}
else{
$OSArch = "x86"
}
Write-host "Downloading VC_redist.$OSArch.exe..."
$SourceURL = "https://aka.ms/vs/16/release/VC_redist.$OSArch.exe"
$Installer = $WingetUpdatePath + "\VC_redist.$OSArch.exe"
$ProgressPreference = 'SilentlyContinue'
2022-03-10 21:09:23 +00:00
Invoke-WebRequest $SourceURL -OutFile (New-Item -Path $Installer -Force)
2022-02-12 15:35:51 +00:00
Write-host "Installing VC_redist.$OSArch.exe..."
2022-02-26 01:07:50 +00:00
Start-Process -FilePath $Installer -Args "/quiet /norestart" -Wait
2022-02-12 15:35:51 +00:00
Remove-Item $Installer -ErrorAction Ignore
Write-host "MS Visual C++ 2015-2019 installed successfully" -ForegroundColor Green
}
catch{
Write-host "MS Visual C++ 2015-2019 installation failed." -ForegroundColor Red
Start-Sleep 3
}
}
}
else{
Write-Host "Prerequisites checked. OK" -ForegroundColor Green
}
}
function Install-WingetAutoUpdate{
try{
#Check if previous version location exists and delete
$OldWingetUpdatePath = $WingetUpdatePath.Replace("\Winget-AutoUpdate","\winget-update")
if (Test-Path ($OldWingetUpdatePath)){
Remove-Item $OldWingetUpdatePath -Force -Recurse
}
Get-ScheduledTask -TaskName "Winget Update" -ErrorAction SilentlyContinue | Unregister-ScheduledTask -Confirm:$False
Get-ScheduledTask -TaskName "Winget Update Notify" -ErrorAction SilentlyContinue | Unregister-ScheduledTask -Confirm:$False
2022-02-12 15:35:51 +00:00
#Copy files to location
if (!(Test-Path $WingetUpdatePath)){
New-Item -ItemType Directory -Force -Path $WingetUpdatePath
}
Copy-Item -Path "$PSScriptRoot\Winget-AutoUpdate\*" -Destination $WingetUpdatePath -Recurse -Force -ErrorAction SilentlyContinue
2022-02-12 15:35:51 +00:00
Copy-Item -Path "$PSScriptRoot\excluded_apps.txt" -Destination $WingetUpdatePath -Recurse -Force -ErrorAction SilentlyContinue
# Set dummy regkeys for notification name and icon
& reg add "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /v DisplayName /t REG_EXPAND_SZ /d "Application Update" /f | Out-Null
& reg add "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /v IconUri /t REG_EXPAND_SZ /d %SystemRoot%\system32\@WindowsUpdateToastIcon.png /f | Out-Null
# Settings for the scheduled task for Updates
2022-03-22 07:29:19 +00:00
$taskAction = New-ScheduledTaskAction Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$($WingetUpdatePath)\winget-upgrade.ps1`""
2022-02-12 15:35:51 +00:00
$taskTrigger1 = New-ScheduledTaskTrigger -AtLogOn
$taskTrigger2 = New-ScheduledTaskTrigger -Daily -At 6AM
$taskUserPrincipal = New-ScheduledTaskPrincipal -UserId S-1-5-18 -RunLevel Highest
$taskSettings = New-ScheduledTaskSettingsSet -Compatibility Win8 -StartWhenAvailable -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit 03:00:00
# Set up the task, and register it
$task = New-ScheduledTask -Action $taskAction -Principal $taskUserPrincipal -Settings $taskSettings -Trigger $taskTrigger2,$taskTrigger1
Register-ScheduledTask -TaskName 'Winget-AutoUpdate' -InputObject $task -Force
2022-02-12 15:35:51 +00:00
# Settings for the scheduled task for Notifications
2022-03-22 07:29:19 +00:00
$taskAction = New-ScheduledTaskAction Execute "wscript.exe" -Argument "`"$($WingetUpdatePath)\Invisible.vbs`" `"powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"`"`"$($WingetUpdatePath)\winget-notify.ps1`"`""
2022-02-12 15:35:51 +00:00
$taskUserPrincipal = New-ScheduledTaskPrincipal -GroupId S-1-5-11
$taskSettings = New-ScheduledTaskSettingsSet -Compatibility Win8 -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit 00:05:00
# Set up the task, and register it
$task = New-ScheduledTask -Action $taskAction -Principal $taskUserPrincipal -Settings $taskSettings
Register-ScheduledTask -TaskName 'Winget-AutoUpdate-Notify' -InputObject $task -Force
2022-02-12 15:35:51 +00:00
2022-03-07 01:35:40 +00:00
# Install config file
[xml]$ConfigXML = @"
<?xml version="1.0"?>
<app>
<WAUautoupdate>$(!($DisableWAUAutoUpdate))</WAUautoupdate>
</app>
"@
$ConfigXML.Save("$WingetUpdatePath\config\config.xml")
2022-02-12 15:35:51 +00:00
Write-host "`nInstallation succeeded!" -ForegroundColor Green
Start-sleep 1
#Run Winget ?
Start-WingetAutoUpdate
}
catch{
Write-host "`nInstallation failed! Run me with admin rights" -ForegroundColor Red
Start-sleep 1
return $False
}
}
2022-04-02 17:24:18 +00:00
function Uninstall-WingetAutoUpdate{
Write-Host "Starting uninstall"
try{
#Check if installed location exists and delete
if (Test-Path ($WingetUpdatePath)){
Remove-Item $WingetUpdatePath -Force -Recurse
Get-ScheduledTask -TaskName "Winget-AutoUpdate" -ErrorAction SilentlyContinue | Unregister-ScheduledTask -Confirm:$False
Get-ScheduledTask -TaskName "Winget-AutoUpdate-Notify" -ErrorAction SilentlyContinue | Unregister-ScheduledTask -Confirm:$False
& reg delete "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /f | Out-Null
Write-host "Uninstallation succeeded!" -ForegroundColor Green
Start-sleep 1
}
else {
Write-host "$WingetUpdatePath not found! Uninstallation failed!" -ForegroundColor Red
}
}
catch{
Write-host "`nUninstallation failed! Run as admin ?" -ForegroundColor Red
Start-sleep 1
}
}
2022-02-12 15:35:51 +00:00
function Start-WingetAutoUpdate{
#If -DoNotUpdate is true, skip.
if (!($DoNotUpdate)){
#If -Silent, run Winget-AutoUpdate now
if ($Silent){
$RunWinget = "y"
}
#Ask for WingetAutoUpdate
else{
while("y","n" -notcontains $RunWinget){
$RunWinget = Read-Host "Start Winget-AutoUpdate now? [Y/N]"
}
}
if ($RunWinget -eq "y"){
try{
Write-host "Running Winget-AutoUpdate..." -ForegroundColor Yellow
Get-ScheduledTask -TaskName "Winget-AutoUpdate" -ErrorAction SilentlyContinue | Start-ScheduledTask -ErrorAction SilentlyContinue
2022-04-02 17:24:18 +00:00
while ((Get-ScheduledTask -TaskName "Winget-AutoUpdate").State -ne 'Ready') {
Start-Sleep 1
}
2022-02-12 15:35:51 +00:00
}
catch{
Write-host "Failed to run Winget-AutoUpdate..." -ForegroundColor Red
}
}
}
else{
Write-host "Skip running Winget-AutoUpdate"
}
}
<# MAIN #>
Write-host "###################################"
Write-host "# #"
Write-host "# Winget AutoUpdate #"
Write-host "# #"
Write-host "###################################`n"
2022-04-02 17:24:18 +00:00
if (!$Uninstall){
Write-host "Installing to $WingetUpdatePath\"
Install-Prerequisites
Install-WingetAutoUpdate
}
else {
Uninstall-WingetAutoUpdate
}
2022-02-12 15:35:51 +00:00
Write-host "End of process."
2022-02-20 21:52:10 +00:00
Start-Sleep 3