AutomoxAutomox

Powershell in worklet not running

Jonas Storgaard
February 16, 2024 at 12:10 pm UTC
#Windows#Worklets#powershell#winget

So I have created this worklet, but nothing seem to happen when I try to run it manually on a computer. Any idea whats wrong?

Evalution:

# Runs 'winget upgrade' to dertimne any updates and save the output
$output = winget upgrade | Out-String
 
# looking for a specific text
if ($output -match "No applicable update found.") {
    # no updates found
    exit 0
} else {
    # uådates found
    exit 1
}

 

Remediation :

# Runs a elevated PowerShell session to update all
$scriptBlock = {
    winget upgrade --all --silent --accept-package-agreements --accept-source-agreements
}
 
# Runs the script with admin priviliges 
Start-Process powershell -ArgumentList "-Command", $scriptBlock -Verb RunAs

This discussion is archived and closed to replies. Continue it in the Automox Community Slack.

Discuss on Slack

Replies (1)

MarkH-AutomoxFebruary 16, 2024
#1

Hi Jonas,

The issue here is triggering winget from an absolute path since it isn’t really something ‘system’ can invoke on your behalf. Here are two results from my Activity Log, one from indirect invocation and one from direct invocation:
 

As you can see, the first result (on bottom) has zero data in it. Here is the difference in code. The commented out line is the equivalent to what you are attempting to do, vs the active code which specifies an execution directory where winget is found:
 


Use the following code chunk to make the wheels turn:
 

powershell10 lines
$ResolveWingetPath = Resolve-Path "C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe"
    if ($ResolveWingetPath){
           $WingetPath = $ResolveWingetPath[-1].Path
    }

$config
Set-Location $wingetpath
.\winget.exe list --upgrade-available

#winget list --upgrade-available