Skip to main content

Scheduled Task Winget Update All (Create)

Summary

The ultimate objective of the task is to establish and oversee two scheduled tasks (Winget Update All [System] and Winget Update All [Logged on User]) designed to update installed applications utilizing Winget on Windows 10 and 11.

The task can only be executed against the companies where the company-level custom field WingetUpdateAll_Task_Create is marked.

Sample Run

Sample Run 1

Sample Run 2

Dependencies

Variables

NameDescription
UserTaskWhether the user task is disabled or not.
OutputOutput of the PowerShell script.

Task Creation

Create a new Script Editor style script in the system to implement this task.

Task Creation 1

Task Creation 2

Name: Scheduled Task Winget Update All (Create)

Description: The ultimate objective of the task is to establish and oversee two scheduled tasks (Winget Update All [System] and Winget Update All [Logged on User]) designed to update installed applications utilizing Winget on Windows 10 and 11.

Category: Patching

Task Creation 3

Task

Start by adding a row. You can do this by clicking the Add Row button at the bottom of the script page.

Add Row

This function will appear.

Function Appears

Row 1 Function: PowerShell Script

Search and select the PowerShell Script function.

PowerShell Script Selection

PowerShell Script Selected

Paste in the following PowerShell script and set the expected time of script execution to 300 seconds. Click the Save button.

if ( (get-ciminstance -classname Win32_OperatingSystem).caption -match 'Windows 1[01]' ) { 'Supported' } else { 'Unsupported' }

PowerShell Script Pasted

Row 2 Logic: If Then

Row 2 Logic

Row 2 Logic 2

Row 2a Condition: Output Contains

Type Unsupported in the Input Value or Variable field and press Enter.

Row 2a Condition

Row 2b Function: Script Log

Add a new row by clicking on the Add Row button.

Add Row for Script Log

Search and select the Script Log function.

Script Log Selection

Script Log Selected

Add the following log in the Script Log Message field and click the Save button:

Unsupported Operating System.

Supported OS are Windows 10 and Windows 11

alt text

Row 2c Function: Script Exit

Add a new row by clicking on the Add Row button.

Add Row for Script Exit

Search and select the Script Exit function.

Script Exit Selection

Note: Do not add any message or words in the Error Message field. Leave it blank and click the Save button.

Row 3 Logic: If Then

Row 3 Logic

Row 3 Logic 2

Row 3a Condition: Custom Field Equals

Select the Custom Field option from the dropdown.

Row 3a Condition

Row 3a Condition 2

Search and select the WingetUpdateAll_Task_Result custom field in the Search field.

Row 3a Condition 3

Row 3a Condition 4

Change the operator to Equals from Contains.

Row 3a Condition 5

Type Task Created in the comparator field and press Enter.

Row 3a Condition 6

Row 3b Function: Script Log

Add a new row by clicking on the Add Row button.

Add Row for Script Log

Search and select the Script Log function.

Script Log Selection

Script Log Selected

Add the following log in the Script Log Message field and click the Save button:

Task is already created on the computer.

alt text

Row 3c Function: Script Exit

Add a new row by clicking on the Add Row button.

Add Row for Script Exit

Search and select the Script Exit function.

Script Exit Selection

Note: Do not add any message or words in the Error Message field. Leave it blank and click the Save button.

Row 4 Logic: If Then

Row 4 Logic

Row 4 Logic 2

Row 4a Condition: Custom Field Equals

Select the Custom Field option from the dropdown.

Row 4a Condition

Row 4a Condition 2

Search and select the WingetUpdateAll_Task_Create custom field in the Search field.

Row 4a Condition 3

Row 4a Condition 4

Change the operator to Equals from Contains.

Row 4a Condition 5

Keep the comparator to False.

Row 4a Condition 6

Row 4b Function: Script Log

Add a new row by clicking on the Add Row button.

Add Row for Script Log

Search and select the Script Log function.

Script Log Selection

Script Log Selected

Add the following log in the Script Log Message field and click the Save button:

Winget Update All scheduled task creation is not enabled for the Company.

alt text

Row 4c Function: Script Exit

Add a new row by clicking on the Add Row button.

Add Row for Script Exit

Search and select the Script Exit function.

Script Exit Selection

Note: Do not add any message or words in the Error Message field. Leave it blank and click the Save button.

Row 5 Logic: If Then

Row 5 Logic

Row 5 Logic 2

Row 5a Condition: Custom Field Equals

Select the Custom Field option from the dropdown.

Row 5a Condition

Row 5a Condition 2

Search and select the wingetupdateall_task_Exclude custom field in the Search field.

Row 5a Condition 3

Change the operator to Equals from Contains.

Row 5a Condition 4

Change the comparator to True from False.

Row 5a Condition 5

Row 5b Function: Script Log

Add a new row by clicking on the Add Row button.

Add Row for Script Log

Search and select the Script Log function.

Script Log Selection

Script Log Selected

Add the following log in the Script Log Message field and click the Save button:

Computer is excluded from Winget Update All Scheduled Task Creation.

alt text

Row 5c Function: Script Exit

Add a new row by clicking on the Add Row button.

Add Row for Script Exit

Search and select the Script Exit function.

Script Exit Selection

Note: Do not add any message or words in the Error Message field. Leave it blank and click the Save button.

Row 6 Logic: If Then

Row 6 Logic

Row 6 Logic 2

Row 6a Condition: Custom Field Equals

Select the Custom Field option from the dropdown.

Row 6a Condition

Row 6a Condition 2

Search and select the wingetupdateall_task_Exclude custom field in the Search field. In this step, select the Site-Level custom field.

Change the operator to Equals from Contains.

Row 6a Condition 3

Change the comparator to True from False.

Row 6a Condition 4

Row 6b Function: Script Log

Add a new row by clicking on the Add Row button.

Add Row for Script Log

Search and select the Script Log function.

Script Log Selection

Script Log Selected

Add the following log in the Script Log Message field and click the Save button:

Site is excluded from Winget Update All Scheduled Task Creation.

alt text

Row 6c Function: Script Exit

Add a new row by clicking on the Add Row button.

Add Row for Script Exit

Search and select the Script Exit function.

Script Exit Selection

Note: Do not add any message or words in the Error Message field. Leave it blank and click the Save button.

Row 7 Logic: If Then Else

Row 7 Logic

Row 7 Logic 2

Row 7a Condition: Custom Field Equals

Select the Custom Field option from the dropdown.

Row 7a Condition

Row 7a Condition 2

Search and select the WingetUpdateAll_UserTask_Disable custom field in the Search field. Select the Company-Level Boolean field.

Row 7a Condition 3

Change the operator to Equals from Contains.

Row 7a Condition 4

Change the comparator to True from False.

Row 7a Condition 5

Add another condition by clicking the Add Condition button.

Add Condition

Change the logical operator to Or from And.

Change Logical Operator

Select the Custom Field option from the dropdown.

Row 7a Condition 6

Search and select the WingetUpdateAll_UserTask_Disable custom field in the Search field. Select the Site-Level Boolean field.

alt text

Change the operator to Equals from Contains and the comparator to True from False.

alt text

Add another condition by clicking the Add Condition button.

alt text

Change the logical operator to Or from And.

alt text

Select the Custom Field option from the dropdown.

alt text

Search and select the WingetUpdateAll_UserTask_Disable custom field in the Search field. Select the Computer-Level Boolean field.

alt text

Change the operator to Equals from Contains and the comparator to True from False.

alt text

Row 7b Function: Set User Variable

Add a new row by clicking on the Add Row button.

alt text

Search and select the Set User Variable function.

alt text

Add UserTask in the Variable Name field and 0 in the Value field, then click the Save button.

alt text

Row 7c Function: Set User Variable

Add a new row by clicking on the Add Row button in the Else section.

alt text

Search and select the Set User Variable function.

alt text alt text

Add UserTask in the Variable Name field and 1 in the Value field, then click the Save button.

alt text
alt text

Row 8 Function: PowerShell Script

Add a new row by clicking on the Add Row button outside the If/Then/Else logic.

alt text
alt text

Search and select the PowerShell Script function.

alt text
alt text
alt text

Paste in the following PowerShell script and set the expected time of script execution to 600 seconds. Click the Save button.

$DisableUserTask = if ( '@UserTask@' -eq 0 ) { $True } else { $False }
$Force = 1
$ProjectName = 'Invoke-WingetProcessor'
$WorkingDirectory = "C:\ProgramData\_automation\script\$ProjectName"
$FilePath = "$WorkingDirectory\Winget-UpdateAll.ps1"
#region Setup - Folder Structure
if ( $Force ) {
Remove-Item -Path $WorkingDirectory -Force -Recurse -ErrorAction SilentlyContinue -Confirm:$false | Out-Null
}
if ( !( Test-Path $WorkingDirectory ) ) {
try {
New-Item -Path $WorkingDirectory -ItemType Directory -ErrorAction Stop -Force | Out-Null
} catch {
throw "Failed to create the working directory. Reason: $($Error[0].Exception.Message)"
}
}
if (-not ( ( ( Get-Acl $WorkingDirectory ).Access | Where-Object { $_.IdentityReference -Match 'EveryOne' } ).FileSystemRights -Match 'FullControl' ) ) {
$ACl = Get-Acl $WorkingDirectory -ErrorAction SilentlyContinue
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule('Everyone', 'FullControl', 'ContainerInherit, ObjectInherit', 'none', 'Allow')
$Acl.AddAccessRule($AccessRule)
Set-Acl $WorkingDirectory $Acl -ErrorAction SilentlyContinue
}
$FileContent = @"
#region Setup - Variables
`$ProjectName = 'Invoke-WingetProcessor'
`$BaseURL = 'https://file.provaltech.com/repo'
`$PS1URL = "`$BaseURL/script/`$ProjectName.ps1"
`$WorkingDirectory = "C:\ProgramData\_automation\script\`$ProjectName"
`$PS1Path = "`$WorkingDirectory\`$ProjectName.ps1"
`$LogPath = "`$WorkingDirectory\`$ProjectName-log.txt"
`$OldLogPath = "`$WorkingDirectory\`$ProjectName-log-old.txt"
#endregion
if ( Test-Path `$OldLogPath ) {
if ( (Get-ItemProperty -Path `$OldLogPath).CreationDate -le (Get-Date).AddDays(-29) ) {
Remove-Item -Path `$OldLogPath -ErrorAction SilentlyContinue -Force -Confirm:`$false
}
}
if ( Test-Path `$LogPath ) {
if ( (Get-ItemProperty -Path `$LogPath).CreationDate -le (Get-Date).AddDays(-30) ) {
Rename-Item -Path `$LogPath -NewName "`$(`$ProjectName)-log-old.txt" -ErrorAction SilentlyContinue -Force -Confirm:`$false
}
}
`$Timeout = 1
do {
try { `$connection = test-connection 8.8.8.8 -Erroraction Stop } catch { `$connection = 'down' }
Start-Sleep -Seconds 1
`$Timeout++
} until ( `$connection -ne 'Down' -or `$Timeout -lt 120 )
if ( `$Timeout -ge 120 ) {
return
}
`$response = Invoke-WebRequest -Uri `$PS1URL -UseBasicParsing
if ( ( `$response.StatusCode -ne 200 ) -and ( !( Test-Path -Path `$PS1Path ) ) ) {
throw "No pre-downloaded script exists and the script '`$PS1URL' failed to download. Exiting."
} elseif ( `$response.StatusCode -eq 200 ) {
Remove-Item -Path `$PS1Path -ErrorAction SilentlyContinue
`$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding `$False
[System.IO.File]::WriteAllLines(`$PS1Path, `$response.Content, `$Utf8NoBomEncoding)
}
if ( !( Test-Path -Path `$PS1Path ) ) {
throw 'An error occurred and the script was unable to be downloaded. Exiting.'
}
#endregion
`$op = & `$PS1Path -UpdateAll
Write-Log -Text "Output: `$op"
"@
try {
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
[System.IO.File]::WriteAllLines($FilePath, $FileContent, $Utf8NoBomEncoding)
} catch {
if ( $Error.Exception.Message -match 'Cannot create a file when that file already exists' ) {
} else {
throw "Failed to write the powershell script. Reason: $($Error[0].Exception.Message)"
}
}

Function New-WingetTask {
[CmdletBinding()]
param (
[Parameter()][String]$TaskName,
[Parameter(Mandatory = $False)][Switch]$Force
)
if ( $Force ) {
( Get-ScheduledTask | Where-Object { $_.TaskName -eq $Taskname } ) | Unregister-ScheduledTask -Confirm:$False -ErrorAction SilentlyContinue | Out-Null
}
if ( ( Get-ScheduledTask | Where-Object { $_.TaskName -eq $TaskName } ).State -match 'Ready|Running' ) {
Write-Output "Scheduled Task $TaskName already exists"
} else {
$Action = New-ScheduledTaskAction -Execute 'cmd.exe'-WorkingDirectory $WorkingDirectory -Argument ('/c start /min "" Powershell' + ' -NoLogo -ExecutionPolicy Bypass -NoProfile -NonInteractive -Windowstyle Hidden' + " -File ""$($FilePath)""")
$Trigger = if ( $TaskName -match 'System' ) {
New-ScheduledTaskTrigger -AtStartup
} else {
New-ScheduledTaskTrigger -AtLogOn
}
$setting = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries
$principal = if ( $TaskName -match 'System' ) {
New-ScheduledTaskPrincipal -UserId 'NT AUTHORITY\SYSTEM' -RunLevel Highest
} else {
New-ScheduledTaskPrincipal -GroupId ( ( New-Object System.Security.Principal.SecurityIdentifier('S-1-5-32-545') ).Translate( [System.Security.Principal.NTAccount] ).Value ) -RunLevel Highest
}
$ScheduledTask = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $setting -Principal $principal
try {
Register-ScheduledTask -TaskName $TaskName -InputObject $ScheduledTask -ErrorAction Stop | Out-Null
Write-Output "Successfully created the scheduled task, $TaskName"
} catch {
throw "Failed to Schedule the task. Reason: $($Error[0].Exception.Message)"
}
}
}
New-WingetTask -TaskName 'Winget Update All [Logged on User]' -Force:$($Force.IsPresent)
New-WingetTask -TaskName 'Winget Update All [System]' -Force:$($Force.IsPresent)
if ( $DisableUserTask -and ( ( Get-ScheduledTask -TaskName 'Winget Update All [Logged on User]' ).State -ne 'Disabled' ) ) {
try {
Disable-ScheduledTask -TaskName 'Winget Update All [Logged on User]' -ErrorAction Stop | Out-Null
Write-Output 'Successfully disabled the scheduled task, Winget Update All [Logged on User].'
} catch {
throw "Failed to disable the user level task. Reason: $($Error[0].Exception.Message)"
}
} elseif ( ( -not $DisableUserTask ) -and ( ( Get-ScheduledTask -TaskName 'Winget Update All [Logged on User]' ).State -eq 'Disabled' ) ) {
try {
Enable-ScheduledTask -TaskName 'Winget Update All [Logged on User]' -ErrorAction Stop | Out-Null
Write-Output 'Successfully enabled the scheduled task, Winget Update All [Logged on User].'
} catch {
throw "Failed to enable the user level task. Reason: $($Error[0].Exception.Message)"
}
}
return Get-ScheduledTask -TaskName 'Winget Update All*' | Format-List -Property TaskName, State

alt text

Mark the Continue on Failure flag for the function.

alt text

Row 9 Function: Script Log

Add a new row by clicking on the Add Row button.

alt text

Search and select the Script Log function.

alt text alt text

Add the following log in the Script Log Message field and click the Save button:

Script Result:
%Output%

alt text

Row 10 Logic: If Then Else

alt text
alt text

Row 10a Condition: Output Contains

Type Failed to in the Input Value or Variable field and press Enter.
alt text

Row 10b Function: Set Custom Field

Add a new row by clicking on Add row button.
alt text

Search and select Set Custom Field Function.
alt text
alt text

Search and select WingetUpdateAll_Task_Result in the Search Custom Field field and set Task Creation Failed in the Value field and click the Save button.
alt text
alt text

Row 10c Function: Set Custom Field

Add a new row by clicking on Add row button in the Else section.
alt text

Search and select Set Custom Field Function.
alt text
alt text

Search and select WingetUpdateAll_Task_Result in the Search Custom Field field and set Task Created in the Value field and click the Save button.
alt text
alt text

Completed Script

Completed Script 1

Completed Script 2

Completed Script 3

Click the Save button at the top right corner of the screen to save the script.

Save Script

Deployment

It is suggested to run the task once per day against the Winget Update All (Task Create) device group.

  • Go to Automation > Tasks.
  • Search for Scheduled Task Winget Update All (Create) Task.
  • Select the concerned task.
  • Click on the Schedule button to schedule the task.

Schedule Task

This screen will appear.

Schedule Confirmation

Click the Does not repeat button.

Change Schedule

This pop-up box will appear.

Click the OK button to save the schedule. Recurrence will be changed to Every day.

Save Schedule

Select the Device Groups option in the Targeted Resources section.

Select Device Groups

Search and select the Winget Update All (Task Create) device group.

Select Device Group

Now click the Run button to initiate the task.

Run Task

The task will start appearing in the Scheduled Tasks.

Scheduled Tasks

Scheduled Tasks 2

Output

  • Custom Field
  • Script Log