Add files via upload
parent
42305453cf
commit
fb0de5a673
|
@ -1,150 +1,91 @@
|
||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
Configure Winget to daily update installed apps.
|
Install apps with Winget-Install and configure Winget-AutoUpdate
|
||||||
|
|
||||||
.DESCRIPTION
|
.DESCRIPTION
|
||||||
Install powershell scripts and scheduled task to daily run Winget upgrade and notify connected users.
|
Install apps with Winget from a list file (apps.txt).
|
||||||
Possible to exclude apps from auto-update
|
Install Winget-AutoUpdate to get apps daily updated
|
||||||
|
https://github.com/Romanitho/Winget-AllinOne
|
||||||
.PARAMETER Silent
|
|
||||||
Install Winget-AutoUpdate and prerequisites silently
|
|
||||||
|
|
||||||
.PARAMETER WingetUpdatePath
|
|
||||||
Specify Winget-AutoUpdate installation localtion. Default: C:\ProgramData\winget-update\
|
|
||||||
|
|
||||||
.PARAMETER DoNotUpdate
|
|
||||||
Do not run Winget-autoupdate after installation. By default, Winget-AutoUpdate is run just after installation.
|
|
||||||
|
|
||||||
.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-update",
|
|
||||||
[Parameter(Mandatory=$False)] [Switch] $DoNotUpdate = $false
|
|
||||||
)
|
|
||||||
|
|
||||||
<# FUNCTIONS #>
|
<# FUNCTIONS #>
|
||||||
|
|
||||||
function Check-Prerequisites{
|
function Download-GitHubRepository {
|
||||||
#Check if Visual C++ 2019 installed
|
param(
|
||||||
$app = "Microsoft Visual C++*2019*"
|
[Parameter()] [string] $Url,
|
||||||
$path = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object {$_.DisplayName -like $app } | Select-Object -Property Displayname, DisplayVersion
|
[Parameter()] [string] $Location
|
||||||
|
)
|
||||||
|
|
||||||
#If not installed, ask for installation
|
# Force to create a zip file
|
||||||
if (!($path)){
|
$ZipFile = "$location\temp.zip"
|
||||||
#If -silent option, force installation
|
New-Item $ZipFile -ItemType File -Force | Out-Null
|
||||||
if ($Silent){
|
|
||||||
$InstallApp = "y"
|
# download the zip
|
||||||
}
|
Write-Host 'Starting downloading the GitHub Repository'
|
||||||
else{
|
Invoke-RestMethod -Uri $Url -OutFile $ZipFile
|
||||||
#Ask for installation
|
Write-Host 'Download finished'
|
||||||
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]"
|
#Extract Zip File
|
||||||
}
|
Write-Host 'Starting unzipping the GitHub Repository locally'
|
||||||
}
|
Expand-Archive -Path $ZipFile -DestinationPath $Location -Force
|
||||||
if ($InstallApp -eq "y"){
|
Get-ChildItem -Path $Location -Recurse | Unblock-File
|
||||||
try{
|
Write-Host 'Unzip finished'
|
||||||
if((Get-CimInStance Win32_OperatingSystem).OSArchitecture -like "*64*"){
|
|
||||||
$OSArch = "x64"
|
# remove the zip file
|
||||||
}
|
Remove-Item -Path $ZipFile -Force
|
||||||
else{
|
}
|
||||||
$OSArch = "x86"
|
|
||||||
}
|
function Get-WingetStatus{
|
||||||
Write-host "Downloading VC_redist.$OSArch.exe..."
|
$hasAppInstaller = Get-AppXPackage -name 'Microsoft.DesktopAppInstaller'
|
||||||
$SourceURL = "https://aka.ms/vs/16/release/VC_redist.$OSArch.exe"
|
if (!($hasAppInstaller)){
|
||||||
$Installer = $WingetUpdatePath + "\VC_redist.$OSArch.exe"
|
Write-Host -ForegroundColor Yellow "Installing WinGet..."
|
||||||
$ProgressPreference = 'SilentlyContinue'
|
Add-AppxPackage -Path https://aka.ms/Microsoft.VCLibs.x64.14.00.Desktop.appx
|
||||||
Invoke-WebRequest $SourceURL -OutFile $Installer
|
$releases_url = "https://api.github.com/repos/microsoft/winget-cli/releases/latest"
|
||||||
Write-host "Installing VC_redist.$OSArch.exe..."
|
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
||||||
Start-Process -FilePath $Installer -Args "-q" -Wait
|
$releases = Invoke-RestMethod -uri "$($releases_url)"
|
||||||
Remove-Item $Installer -ErrorAction Ignore
|
$latestRelease = $releases.assets | Where-Object { $_.browser_download_url.EndsWith("msixbundle") } | Select-Object -First 1
|
||||||
Write-host "MS Visual C++ 2015-2019 installed successfully" -ForegroundColor Green
|
Add-AppxPackage -Path $latestRelease.browser_download_url
|
||||||
}
|
Write-Host -ForegroundColor Green "WinGet successfully installed."
|
||||||
catch{
|
|
||||||
Write-host "MS Visual C++ 2015-2019 installation failed." -ForegroundColor Red
|
|
||||||
Start-Sleep 3
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else{
|
else {
|
||||||
Write-Host "Prerequisites checked. OK" -ForegroundColor Green
|
Write-Host -ForegroundColor Green "WinGet is already installed."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function Install-WingetAutoUpdate{
|
function Get-WingetCmd {
|
||||||
try{
|
#Get WinGet Location in User context
|
||||||
#Copy files to location
|
$WingetCmd = Get-Command winget.exe -ErrorAction SilentlyContinue
|
||||||
if (!(Test-Path $WingetUpdatePath)){
|
if ($WingetCmd){
|
||||||
New-Item -ItemType Directory -Force -Path $WingetUpdatePath
|
$Script:winget = $WingetCmd.Source
|
||||||
}
|
|
||||||
Copy-Item -Path "$PSScriptRoot\winget-update\*" -Destination $WingetUpdatePath -Recurse -Force -ErrorAction SilentlyContinue
|
|
||||||
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
|
|
||||||
$taskAction = New-ScheduledTaskAction –Execute "powershell.exe" -Argument "-ExecutionPolicy Bypass -File `"$($WingetUpdatePath)\winget-upgrade.ps1`""
|
|
||||||
$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 Update' -InputObject $task -Force
|
|
||||||
|
|
||||||
# Settings for the scheduled task for Notifications
|
|
||||||
$taskAction = New-ScheduledTaskAction –Execute "wscript.exe" -Argument "`"$($WingetUpdatePath)\Invisible.vbs`" `"powershell.exe -ExecutionPolicy Bypass -File `"`"`"$($WingetUpdatePath)\winget-notify.ps1`"`""
|
|
||||||
$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 Update Notify' -InputObject $task -Force
|
|
||||||
|
|
||||||
Write-host "`nInstallation succeeded!" -ForegroundColor Green
|
|
||||||
Start-sleep 1
|
|
||||||
|
|
||||||
#Run Winget ?
|
|
||||||
Start-WingetAutoUpdate
|
|
||||||
}
|
}
|
||||||
catch{
|
#Get WinGet Location in System context (WinGet < 1.17)
|
||||||
Write-host "`nInstallation failed! Run me with admin rights" -ForegroundColor Red
|
elseif (Test-Path "C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe\AppInstallerCLI.exe"){
|
||||||
Start-sleep 1
|
$Script:winget = Resolve-Path "C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe\AppInstallerCLI.exe" | Select-Object -ExpandProperty Path
|
||||||
return $False
|
}
|
||||||
|
#Get WinGet Location in System context (WinGet > 1.17)
|
||||||
|
elseif (Test-Path "C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe\winget.exe"){
|
||||||
|
$Script:winget = Resolve-Path "C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe\winget.exe" | Select-Object -ExpandProperty Path
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function Start-WingetAutoUpdate{
|
function Get-AppList{
|
||||||
#If -DoNotUpdate is true, skip.
|
if (Test-Path "$PSScriptRoot\apps.txt"){
|
||||||
if (!($DoNotUpdate)){
|
$AppList = Get-Content -Path "$PSScriptRoot\apps_to_install.txt"
|
||||||
#If -Silent, run Winget-AutoUpdate now
|
return $AppList -join ","
|
||||||
if ($Silent){
|
}
|
||||||
$RunWinget = "y"
|
}
|
||||||
}
|
|
||||||
#Ask for WingetAutoUpdate
|
function Get-ExcludedApps{
|
||||||
else{
|
if (Test-Path "$PSScriptRoot\excluded_apps.txt"){
|
||||||
while("y","n" -notcontains $RunWinget){
|
Write-Host "Installing Custom 'excluded_apps.txt' file"
|
||||||
$RunWinget = Read-Host "Start Winget-AutoUpdate now? [Y/N]"
|
Copy-Item -Path "$PSScriptRoot\excluded_apps.txt" -Destination "$env:ProgramData\winget-update" -Recurse -Force -ErrorAction SilentlyContinue
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($RunWinget -eq "y"){
|
|
||||||
try{
|
|
||||||
Write-host "Running Winget-AutoUpdate..." -ForegroundColor Yellow
|
|
||||||
Get-ScheduledTask -TaskName "Winget Update" -ErrorAction SilentlyContinue | Start-ScheduledTask -ErrorAction SilentlyContinue
|
|
||||||
}
|
|
||||||
catch{
|
|
||||||
Write-host "Failed to run Winget-AutoUpdate..." -ForegroundColor Red
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
Write-host "Skip running Winget-AutoUpdate"
|
Write-Host "Keeping default 'excluded_apps.txt' file"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,14 +94,40 @@ function Start-WingetAutoUpdate{
|
||||||
|
|
||||||
Write-host "###################################"
|
Write-host "###################################"
|
||||||
Write-host "# #"
|
Write-host "# #"
|
||||||
Write-host "# Winget AutoUpdate #"
|
Write-host "# Winget AllinOne #"
|
||||||
Write-host "# #"
|
Write-host "# #"
|
||||||
Write-host "###################################`n"
|
Write-host "###################################`n"
|
||||||
Write-host "Installing to $WingetUpdatePath\"
|
|
||||||
|
|
||||||
Check-Prerequisites
|
#Temp folder
|
||||||
|
$Location = "$env:ProgramData\Winget"
|
||||||
|
|
||||||
Install-WingetAutoUpdate
|
#Download Winget-AutoUpdate
|
||||||
|
Download-GitHubRepository "https://github.com/Romanitho/Winget-AutoUpdate/archive/refs/heads/main.zip" $Location
|
||||||
|
|
||||||
Write-host "End of process."
|
#Download Winget-Install
|
||||||
|
Download-GitHubRepository "https://github.com/Romanitho/Winget-Install/archive/refs/heads/main.zip" $Location
|
||||||
|
|
||||||
|
#Check Winget is installed, and install if not
|
||||||
|
Get-WingetStatus
|
||||||
|
|
||||||
|
#Get App List
|
||||||
|
$AppToInstall = Get-AppList
|
||||||
|
|
||||||
|
#Install Winget-Autoupdate
|
||||||
|
Write-Host 'Installing Winget-AutoUpdate...'
|
||||||
|
Start-Process "powershell.exe" -Argument "-executionpolicy bypass -file `"$Location\Winget-AutoUpdate-main\winget-install-and-update.ps1`" -Silent -DoNotUpdate" -Wait
|
||||||
|
|
||||||
|
#Run Winget-Install
|
||||||
|
Write-Host 'Running Winget-Install...'
|
||||||
|
Start-Process "powershell.exe" -Argument "-executionpolicy bypass -command `"$Location\Winget-Install-main\winget-install.ps1 -AppIDs $AppToInstall`"" -Wait
|
||||||
|
|
||||||
|
#Configure ExcludedApps
|
||||||
|
Get-ExcludedApps
|
||||||
|
|
||||||
|
#Run WAU
|
||||||
|
Write-Host "Running Winget-AutoUpdate"
|
||||||
|
Get-ScheduledTask -TaskName "Winget Update" -ErrorAction SilentlyContinue | Start-ScheduledTask -ErrorAction SilentlyContinue
|
||||||
|
|
||||||
|
Remove-Item -Path $Location -Force -Recurse
|
||||||
|
Write-Host "End." -ForegroundColor Cyan
|
||||||
Start-Sleep 3
|
Start-Sleep 3
|
Loading…
Reference in New Issue