Skip to content

Commit

Permalink
Final touches
Browse files Browse the repository at this point in the history
  • Loading branch information
Romanitho committed Oct 9, 2023
1 parent e42a8ce commit 8835de9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
12 changes: 12 additions & 0 deletions Winget-AutoUpdate/Winget-Upgrade.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,18 @@ if (Test-Network) {
#Run WAU in user context if feature is activated
if ($WAUConfig.WAU_UserContext -eq 1) {

#Create User context task if not existing
$UserContextTask = Get-ScheduledTask -TaskName 'Winget-AutoUpdate-UserContext' -ErrorAction SilentlyContinue
if (!$UserContextTask) {
#Create the scheduled task in User context
$taskAction = New-ScheduledTaskAction -Execute "wscript.exe" -Argument "`"$($WingetUpdatePath)\Invisible.vbs`" `"powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"`"`"$($WingetUpdatePath)\winget-upgrade.ps1`"`""
$taskUserPrincipal = New-ScheduledTaskPrincipal -GroupId S-1-5-11
$taskSettings = New-ScheduledTaskSettingsSet -Compatibility Win8 -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit 03:00:00
$task = New-ScheduledTask -Action $taskAction -Principal $taskUserPrincipal -Settings $taskSettings
Register-ScheduledTask -TaskName 'Winget-AutoUpdate-UserContext' -TaskPath 'WAU' -InputObject $task -Force | Out-Null
Write-ToLog "-> User Context task created."
}

#User check routine from: https://stackoverflow.com/questions/23219718/powershell-script-to-see-currently-logged-in-users-domain-and-machine-status
$explorerprocesses = @(Get-WmiObject -Query "Select * FROM Win32_Process WHERE Name='explorer.exe'" -ErrorAction SilentlyContinue)
If ($explorerprocesses.Count -eq 0) {
Expand Down
26 changes: 9 additions & 17 deletions Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -186,26 +186,18 @@ function Invoke-PostUpdateActions {
}
}

#Create User context task if not existing and manage its activation
$UserContextTask = Get-ScheduledTask -TaskName 'Winget-AutoUpdate-UserContext' -ErrorAction SilentlyContinue
if (!$UserContextTask) {
#Create the scheduled task in User context
$taskAction = New-ScheduledTaskAction -Execute "wscript.exe" -Argument "`"$($WingetUpdatePath)\Invisible.vbs`" `"powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"`"`"$($WingetUpdatePath)\winget-upgrade.ps1`"`""
$taskUserPrincipal = New-ScheduledTaskPrincipal -GroupId S-1-5-11
$taskSettings = New-ScheduledTaskSettingsSet -Compatibility Win8 -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit 03:00:00
$task = New-ScheduledTask -Action $taskAction -Principal $taskUserPrincipal -Settings $taskSettings
Register-ScheduledTask -TaskName 'Winget-AutoUpdate-UserContext' -TaskPath 'WAU' -InputObject $task -Force | Out-Null

#If scheduled task didn't exist, user context was not activated. Set it in registry.
New-ItemProperty $regPath -Name WAU_UserContext -Value 0 -PropertyType DWord -Force | Out-Null
Write-ToLog "-> User Context task created and set to 'disabled' in registry."
}
#If scheduled task exists but no WAU_UserContext setting, user context was activated. Set it in registry.
elseif (!($WAUConfig.WAU_UserContext)) {
#Activate WAU in user context if previously configured (as "Winget-AutoUpdate-UserContext" at root)
$UserContextTask = Get-ScheduledTask -TaskName 'Winget-AutoUpdate-UserContext' -TaskPath '\' -ErrorAction SilentlyContinue
if ($UserContextTask) {
#Remove Winget-AutoUpdate-UserContext at root.
Unregister-ScheduledTask $UserContextTask -Confirm:$False

#Set it in registry as activated.
New-ItemProperty $regPath -Name WAU_UserContext -Value 1 -PropertyType DWord -Force | Out-Null
Write-ToLog "-> User Context task existes and set to 'enabled' in registry."
Write-ToLog "-> Old User Context task deleted and set to 'enabled' in registry."
}


### End of post update actions ###

#Reset WAU_UpdatePostActions Value
Expand Down

0 comments on commit 8835de9

Please sign in to comment.