Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update module to support running in SYSTEM context #7

Merged
merged 4 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions WingetTools.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@{

RootModule = 'WingetTools.psm1'
ModuleVersion = '1.4.0'
ModuleVersion = '1.4.1'
CompatiblePSEditions = @('Desktop', 'Core')
GUID = '1130bb85-58d1-487d-9763-c38011f9613d'
Author = 'Jeff Hicks'
Expand All @@ -25,7 +25,7 @@
'formats\wginstalled.format.ps1xml'
)
FunctionsToExport = 'Get-WGPackage', 'Install-Winget', 'Get-WGReleaseNote',
'Get-WGInstalled', 'Invoke-WGUpgrade', 'Get-WGUpgrade', 'Test-WGVersion'
'Get-WGInstalled', 'Invoke-WGUpgrade', 'Get-WGUpgrade', 'Test-WGVersion' , 'Get-WingetPath'
AliasesToExport = 'wglatest', 'iwg'
PrivateData = @{
PSData = @{
Expand Down
10 changes: 6 additions & 4 deletions functions/Get-WGInstalled.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ Function Get-WGInstalled {
Begin {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
$tmpFile = "$env:temp\wgExport_{0}.json" -f (Get-Date -Format yyyymmdd)
$Winget = Get-WingetPath
} #begin

Process {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Building list of packages with a winget source"
[void](winget export -s winget -o $tmpFile --include-versions)
[void](& $winget export -s winget -o $tmpFile --include-versions)
if (Test-Path -Path $tmpFile) {
#import the json file
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Importing temporary JSON data"
Expand All @@ -25,12 +26,13 @@ Function Get-WGInstalled {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Found $count packages. Getting details."

$jobList = [system.collections.generic.list[object]]::new()
$wingetPath = $winget
foreach ($pkg in $json.Sources.packages) {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] $($pkg.PackageIdentifier)"
$i++
#9 August 2022 Using Threadjobs to get online package details for better scaling - JDH
$sb = {
Param([object]$pkg, [string]$source)
Param([object]$pkg, [string]$source, [string]$wingetPath)
Function _parseVersion {
#parse out odd characters from version strings
[cmdletbinding()]
Expand All @@ -49,7 +51,7 @@ Function Get-WGInstalled {
}

try {
$show = winget show --id $pkg.PackageIdentifier --source $source
$show = & $wingetPath show --id $pkg.PackageIdentifier --source $source

[regex]$rxname = "(?<=\w\s).*(?=\s\[[\S\.]+\])"
$installed = _parseVersion $pkg.version
Expand All @@ -75,7 +77,7 @@ Function Get-WGInstalled {
}
}

$joblist.add($(Start-ThreadJob -ScriptBlock $sb -ArgumentList $pkg, $source -Name wg))
$joblist.add($(Start-ThreadJob -ScriptBlock $sb -ArgumentList $pkg, $source, $wingetPath -Name wg))

} #foreach pkg

Expand Down
3 changes: 2 additions & 1 deletion functions/Get-WGPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ Function Get-WGPackage {

Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Using source $source"
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Using Parameter set $($PSCmdlet.ParameterSetName)"
$cmdText = "winget show --source $source"
$winget = Get-WingetPath
$cmdText = "$winget show --source $source"

Switch ($PSCmdlet.ParameterSetName) {
"Name" {
Expand Down
12 changes: 12 additions & 0 deletions functions/Get-WingetPath.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function Get-WingetPath {
$currentUser = whoami # get account running function
if ($currentUser -eq "nt authority\system")
{
# check if running in SYSTEM context set path to WINGET executable
$winget = Get-ChildItem -path "$env:ProgramFiles\WindowsApps" -Recurse -File | where-object { $_.name -like "AppInstallerCLI.exe" -or $_.name -like "WinGet.exe" } | Select-Object -ExpandProperty fullname
# If there are multiple versions, select latest
if ($winget.count -gt 1) { $winget = $winget[-1] }
}
else { $winget = "$env:localappdata\Microsoft\WindowsApps\Winget.exe" }
return $winget
}
3 changes: 2 additions & 1 deletion functions/Get-WingetUpgrade.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ Function Get-WGUpgrade {

Write-Verbose "Starting $($MyInvocation.MyCommand)"
#create a list to hold the results
$Winget = Get-WingetPath
$list = [System.Collections.Generic.list[object]]::New()
$up = (winget upgrade).trim() | Where-Object { $_ -match "\d+\.\d+\s(?!MB)" }
$up = (& $winget upgrade).trim() | Where-Object { $_ -match "\d+\.\d+\s(?!MB)" }

Write-Verbose "Found $($up.count) available upgrades"

Expand Down
9 changes: 5 additions & 4 deletions functions/Invoke-WingetUpgrade.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Function Invoke-WGUpgrade {

Begin {
Write-Verbose "Starting $($MyInvocation.MyCommand)"
$winget = Get-WingetPath
} #begin
Process {
#set a flag indicating items were process in this script block
Expand All @@ -42,22 +43,22 @@ Function Invoke-WGUpgrade {
foreach ($item in $InputObject) {
if ($pscmdlet.ShouldProcess($item.id, "Upgrade from $($item.version) to $($item.Available)")) {
if ($item.source) {
winget upgrade --id $item.id --source $item.source --silent --accept-package-agreements --accept-source-agreements
& $winget upgrade --id $item.id --source $item.source --silent --accept-package-agreements --accept-source-agreements
}
else {
winget upgrade --id $item.id --silent --accept-package-agreements --accept-source-agreements
& $winget upgrade --id $item.id --silent --accept-package-agreements --accept-source-agreements
}
}
} #foreach
}
elseif ($pscmdlet.ParameterSetName -eq 'all' -AND $IncludeUnknown) {
if ($pscmdlet.ShouldProcess("all installed and unknown packages")) {
winget upgrade --all --include-unknown --silent
& $winget upgrade --all --include-unknown --silent
}
}
elseif ($pscmdlet.ParameterSetName -eq 'all') {
if ($pscmdlet.ShouldProcess("all installed")) {
winget upgrade --all --silent
& $winget upgrade --all --silent
}
}
else {
Expand Down