Added parameters

pull/2/head
Romain 2022-01-27 21:52:17 +01:00 committed by GitHub
parent c304168b1a
commit b4adb584dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 158 additions and 57 deletions

View File

@ -1,26 +1,79 @@
#Create winget-update folder and content structure <#
$WingetUpdatePath = "$env:ProgramData\winget-update" .SYNOPSIS
Write-host "###################################" Configure Winget to daily update installed apps.
Write-host "# #"
Write-host "# Winget AutoUpdate #"
Write-host "# #"
Write-host "################################### `n"
Write-host "Instaling to $WingetUpdatePath\"
#Check if Visual C++ 2015-2019 is installed. If not, download and install .DESCRIPTION
$app = "Microsoft Visual C++ 2019 X64*" Install powershell scripts and scheduled task to daily run Winget upgrade and notify connected users.
Possible to exclude apps from auto-update
.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 #>
function Check-Prerequisites{
#Check if Visual C++ 2019 installed
$app = "Microsoft Visual C++*2019*"
$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 $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
#If not installed, ask for installation
if (!($path)){ if (!($path)){
Write-host "MS Visual C++ 2015-2019 is not installed." #If -silent option, force installation
Write-host "Downloading VC_redist.x64.exe..." if ($Silent){
$SourceURL = "https://aka.ms/vs/16/release/VC_redist.x64.exe" $InstallApp = "y"
$Installer = $env:TEMP + "\vscode.exe" }
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"
Invoke-WebRequest $SourceURL -OutFile $Installer Invoke-WebRequest $SourceURL -OutFile $Installer
Write-host "Installing VC_redist.x64.exe..." Write-host "Installing VC_redist.$OSArch.exe..."
Start-Process -FilePath $Installer -Args "-q" -Wait Start-Process -FilePath $Installer -Args "-q" -Wait
Remove-Item $Installer -ErrorAction SilentlyContinue 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{ try{
#Copy files to location #Copy files to location
if (!(Test-Path $WingetUpdatePath)){ if (!(Test-Path $WingetUpdatePath)){
@ -53,12 +106,60 @@ try{
$task = New-ScheduledTask -Action $taskAction -Principal $taskUserPrincipal -Settings $taskSettings $task = New-ScheduledTask -Action $taskAction -Principal $taskUserPrincipal -Settings $taskSettings
Register-ScheduledTask -TaskName 'Winget Update Notify' -InputObject $task -Force Register-ScheduledTask -TaskName 'Winget Update Notify' -InputObject $task -Force
# Run Winget
Get-ScheduledTask -TaskName "Winget Update" -ErrorAction SilentlyContinue | Start-ScheduledTask -ErrorAction SilentlyContinue
Write-host "`nInstallation succeeded!" -ForegroundColor Green Write-host "`nInstallation succeeded!" -ForegroundColor Green
Start-sleep 5 Start-sleep 1
#Run Winget ?
Start-WingetAutoUpdate
} }
catch{ catch{
Write-host "`nInstallation failed! Run me with admin rights" -ForegroundColor Red Write-host "`nInstallation failed! Run me with admin rights" -ForegroundColor Red
Start-sleep 5 Start-sleep 1
return $False
} }
}
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 Update" -ErrorAction SilentlyContinue | Start-ScheduledTask -ErrorAction SilentlyContinue
}
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"
Write-host "Installing to $WingetUpdatePath\"
Check-Prerequisites
Install-WingetAutoUpdate
Write-host "End of process."
Start-Sleep 3