Skip to main content

Winget Install Application

Summary

Attempts to install or update an application via Winget. To get the PackageId and source, you can search in cmd using "winget search appname" or by browsing to winget.run

Sample Run

Image1

Example 1: Installing application without any optional parameters

Image2

Example 2: Installing application with optional parameter

Image3

Image4

Dependencies

Invoke-WingetProcessor

User Parameters

NameExampleAccepted ValuesRequiredDefaultTypeDescription
PackageIdAgileBits.1PasswordYesText Stringwinget application ID
Sourcewingetwinget, msstoreYesText StringSpecifies winget or msstore as the source for package install
OptionalParameter--Scope machine --Override "/qn /norestart MANAGED_UPDATE=1"NoText StringSpecifies option parameters to deploy the application

Task Creation

Script Details

Step 1

Navigate to AutomationTasks
step1

Step 2

Create a new Script Editor style task by choosing the Script Editor option from the Add dropdown menu
step2

The New Script page will appear on clicking the Script Editor button:
step3

Step 3

Fill in the following details in the Description section:

Name: Winget Install Application
Description:

Attempts to install or update an application via Winget
Parameter:
PackageId = winget application ID (Example: Google.Chrome)
Source could be either 'winget', 'msstore'
To get the PackageId and source, you can search in cmd using "winget search appname" or by browsing to winget.run

Category: Application

Image5

Parameters

PackageId

Locate the Add Parameter button on the right-hand side of the screen and click on it to create a new parameter.
AddParameter

The Add New Script Parameter page will appear on clicking the Add Parameter button.
AddNewScriptParameter

Configure the parameter as described below:
Parameter Name: PackageId
Required Field: True
Parameter Type: Text String
Default Value: False

Click the Save button to add the parameter.
Image6

Read the message that will appear after clicking the Save button and click the Confirm button to save the changes.
Image7

Source

Locate the Add Parameter button on the right-hand side of the screen and click on it to create a new parameter.
AddParameter

The Add New Script Parameter page will appear on clicking the Add Parameter button.
AddNewScriptParameter

Configure the parameter as described below:
Parameter Name: Source
Required Field: True
Parameter Type: Text String
Default Value: False

Click the Save button to add the parameter.
Image8

Read the message that will appear after clicking the Save button and click the Confirm button to save the changes.
Image7

OptionalParameter

Locate the Add Parameter button on the right-hand side of the screen and click on it to create a new parameter.
AddParameter

The Add New Script Parameter page will appear on clicking the Add Parameter button.
AddNewScriptParameter

Configure the parameter as described below:
Parameter Name: OptionalParameter
Required Field: False
Parameter Type: Text String
Default Value: False

Click the Save button to add the parameter.
Image9

Read the message that will appear after clicking the Save button and click the Confirm button to save the changes.
Image7

Script Editor

Click the Add Row button in the Script Editor section to start creating the script
AddRow

A blank function will appear:
BlankFunction

Row 1 Function: PowerShell script

Search and select the PowerShell Script function.
AddPowerShell
AddedPowerShell

The following function will pop up on the screen:
BlankPowerShell

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

#region parameters
$packageId = '@PackageId@'
$source = '@Source@'
$optionalParameter = '@OptionalParameter@'

$Parameters = @{
'Install' = $true
'AllowUpdate' = $true
'PackageId' = $packageId
'Source' = $source
}

if (-not [String]::IsNullOrEmpty($optionalParameter) -and $optionalParameter -notmatch 'OptionalParameter') {
$optionalParameter = $optionalParameter -replace '\s{1,}', ' '
$pattern = '("[^"]+"|\S+)'
$optionalParamArray = ([regex]::matches($optionalParameter, $pattern)).Value
$Parameters.Add('OptionalParameter', $optionalParamArray)
}
#endRegion
#region Setup - Variables
$ProjectName = 'Invoke-WingetProcessor'
[Net.ServicePointManager]::SecurityProtocol = [enum]::ToObject([Net.SecurityProtocolType], 3072)
$BaseURL = 'https://file.provaltech.com/repo'
$PS1URL = "$BaseURL/script/$ProjectName.ps1"
$WorkingDirectory = "C:\ProgramData\_automation\script\$ProjectName"
$PS1Path = "$WorkingDirectory\$ProjectName.ps1"
$Workingpath = $WorkingDirectory
$LogPath = "$WorkingDirectory\$ProjectName-log.txt"
$ErrorLogPath = "$WorkingDirectory\$ProjectName-Error.txt"
#endRegion
#region Setup - Folder Structure
New-Item -Path $WorkingDirectory -ItemType Directory -ErrorAction SilentlyContinue | Out-Null
$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
[System.IO.File]::WriteAllLines($PS1Path, $response.Content)
}
if (!(Test-Path -Path $PS1Path)) {
throw 'An error occurred and the script was unable to be downloaded. Exiting.'
}
#endRegion
#region Execution
if ($Parameters) {
Write-Information ('Parameters Used: {0}' -f ($Parameters | Out-String)) -InformationAction Continue
& $PS1Path @Parameters
} else {
& $PS1Path
}
#endRegion
#region log verification
if ( !(Test-Path $LogPath) ) {
throw 'PowerShell Failure. A Security application seems to have restricted the execution of the PowerShell Script.'
}
if ( Test-Path $ErrorLogPath ) {
$ErrorContent = ( Get-Content -Path $ErrorLogPath )
throw ('Error Content: {0}' -f ($ErrorContent | Out-String))
}
$content = Get-Content -Path $LogPath
$logContent = $content[ $($($content.IndexOf($($content -match "$($ProjectName)$")[-1])) + 1)..$($Content.length - 1) ]
return ('Log Content: {0}' -f ($logContent | Out-String))
#endRegion

Image10

Row 2 Function: Script Log

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

A blank function will appear.
BlankFunction

Search and select the Script Log function.
AddScriptLogFunction

The following function will pop up on the screen:
BlankScriptLogFunction

In the script log message, simply type %Output% and click the Save button.
OutputScriptLogFunction

Save Task

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

Completed Task

Image11

Output

  • Script Log