From aee6f711d9018a5d8fd71760a22ccab54bf57c49 Mon Sep 17 00:00:00 2001 From: Romanitho <96626929+Romanitho@users.noreply.github.com> Date: Sat, 22 Jan 2022 16:44:48 +0100 Subject: [PATCH] Minor changes --- sample-excluded_apps.txt | 2 +- winget-install-and-update.ps1 | 73 ++++++++++++++++++++--------------- 2 files changed, 42 insertions(+), 33 deletions(-) diff --git a/sample-excluded_apps.txt b/sample-excluded_apps.txt index e68b376..225371a 100644 --- a/sample-excluded_apps.txt +++ b/sample-excluded_apps.txt @@ -5,4 +5,4 @@ Microsoft.Edge Microsoft.EdgeWebView2Runtime Microsoft.Office Microsoft.OneDrive -Microsoft.Teams +Microsoft.Teams \ No newline at end of file diff --git a/winget-install-and-update.ps1 b/winget-install-and-update.ps1 index 80d2368..a8cada2 100644 --- a/winget-install-and-update.ps1 +++ b/winget-install-and-update.ps1 @@ -1,36 +1,45 @@ #Create winget-update folder and content structure $WingetUpdatePath = "$env:ProgramData\winget-update" +Write-host "Instaling to $WingetUpdatePath\" -#Copy files to location -if (!(Test-Path $WingetUpdatePath)){ - New-Item -ItemType Directory -Force -Path $WingetUpdatePath +try{ + #Copy files to location + if (!(Test-Path $WingetUpdatePath)){ + New-Item -ItemType Directory -Force -Path $WingetUpdatePath + } + 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 'SYSTEM' -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-32-545 + $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 + + # Run Winget + Get-ScheduledTask -TaskName "Winget Update" -ErrorAction SilentlyContinue | Start-ScheduledTask -ErrorAction SilentlyContinue + Write-host "Installation succeeded!" -ForegroundColor Green + Start-sleep 5 } -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 -& reg add "HKCR\AppUserModelId\Windows.SystemToast.Winget.Notification" /v IconUri /t REG_EXPAND_SZ /d %SystemRoot%\system32\@WindowsUpdateToastIcon.png /f - -# 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 'SYSTEM' -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-32-545 -$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 - -# Run Winget -Get-ScheduledTask -TaskName "Winget Update" -ErrorAction SilentlyContinue | Start-ScheduledTask -ErrorAction SilentlyContinue \ No newline at end of file +catch{ + Write-host "Installation failed! Run me with admin rights" -ForegroundColor Red + Start-sleep 5 +} \ No newline at end of file