Skip to content
31 changes: 15 additions & 16 deletions Sources/Winget-AutoUpdate/Winget-Install.ps1
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<#
.SYNOPSIS
Install apps with Winget through Intune or SCCM.
(Can be used standalone.) - Deprecated in favor of Winget-AutoUpdate.

.DESCRIPTION
Allow to run Winget in System Context to install your apps.
(https://github.com/Romanitho/Winget-Install) - Deprecated in favor of Winget-AutoUpdate.

.PARAMETER AppIDs
Forward Winget App ID to install. For multiple apps, separate with ",". Case sensitive.
Expand Down Expand Up @@ -48,7 +46,8 @@ param(
[Parameter(Mandatory = $False)] [Switch] $Uninstall,
[Parameter(Mandatory = $False)] [String] $LogPath,
[Parameter(Mandatory = $False)] [Switch] $WAUWhiteList,
[Parameter(Mandatory = $False)] [Switch] $AllowUpgrade
[Parameter(Mandatory = $False)] [Switch] $AllowUpgrade,
[Parameter(Mandatory = $False)] [String] $Source = "winget"
)


Expand Down Expand Up @@ -76,7 +75,7 @@ else {
#Check if App exists in Winget Repository
function Confirm-Exist ($AppID) {
#Check is app exists in the winget repository
$WingetApp = & $winget show --Id $AppID -e --accept-source-agreements -s winget | Out-String
$WingetApp = & $winget show --Id $AppID -e --accept-source-agreements -s $Source | Out-String

#Return if AppID exists
if ($WingetApp -match [regex]::Escape($AppID)) {
Expand Down Expand Up @@ -139,8 +138,8 @@ function Test-ModsUninstall ($AppID) {
}

#Install function
function Install-App ($AppID, $AppArgs) {
$IsInstalled = Confirm-Installation $AppID
function Install-App ($AppID, $AppArgs, $Source = 'winget') {
$IsInstalled = Confirm-Installation $AppID -src $Source
Comment thread
Salamek marked this conversation as resolved.
if (!($IsInstalled) -or $AllowUpgrade ) {
#Check if mods exist (or already exist) for preinstall/override/custom/arguments/install/installed
$ModsPreInstall, $ModsOverride, $ModsCustom, $ModsArguments, $ModsInstall, $ModsInstalled = Test-ModsInstall $($AppID)
Expand All @@ -159,21 +158,21 @@ function Install-App ($AppID, $AppArgs) {
Write-ToLog "-> Installing $AppID..." "DarkYellow"
if ($ModsOverride) {
Write-ToLog "-> Arguments (overriding default): $ModsOverride" # Without -h (user overrides default)
$WingetArgs = "install --id $AppID -e --accept-package-agreements --accept-source-agreements -s winget --override $ModsOverride" -split " "
$WingetArgs = @("install", "--id", $AppID, "-e", "--accept-package-agreements", "--accept-source-agreements", "-s", $Source, "--override", $ModsOverride)
}
elseif ($ModsCustom) {
Write-ToLog "-> Arguments (customizing default): $ModsCustom" # With -h (user customizes default)
$WingetArgs = "install --id $AppID -e --accept-package-agreements --accept-source-agreements -s winget -h --custom $ModsCustom" -split " "
$WingetArgs = @("install", "--id", $AppID, "-e", "--accept-package-agreements", "--accept-source-agreements", "-s", $Source, "-h", "--custom", $ModsCustom)
}
elseif ($ModsArguments -or (-not [string]::IsNullOrWhiteSpace($AppArgs))) {
# Prioritize ModsArguments from file over AppArgs from command line
$finalArgs = if ($ModsArguments) { $ModsArguments } else { $AppArgs }
Write-ToLog "-> Arguments (winget-level): $finalArgs" # Winget parameters with -h
$argArray = ConvertTo-WingetArgumentArray $finalArgs
$WingetArgs = @("install", "--id", $AppID, "-e", "--accept-package-agreements", "--accept-source-agreements", "-s", "winget") + $argArray + @("-h")
$WingetArgs = @("install", "--id", $AppID, "-e", "--accept-package-agreements", "--accept-source-agreements", "-s", $Source) + $argArray + @("-h")
}
else {
$WingetArgs = "install --id $AppID -e --accept-package-agreements --accept-source-agreements -s winget -h" -split " "
$WingetArgs = @("install", "--id", $AppID, "-e", "--accept-package-agreements", "--accept-source-agreements", "-s", $Source, "-h")
}

Write-ToLog "-> Running: `"$Winget`" $WingetArgs"
Expand All @@ -185,7 +184,7 @@ function Install-App ($AppID, $AppArgs) {
}

#Check if install is ok
$IsInstalled = Confirm-Installation $AppID
$IsInstalled = Confirm-Installation $AppID -src $Source
if ($IsInstalled) {
Write-ToLog "-> $AppID successfully installed." "Green"

Expand All @@ -209,8 +208,8 @@ function Install-App ($AppID, $AppArgs) {
}

#Uninstall function
function Uninstall-App ($AppID, $AppArgs) {
$IsInstalled = Confirm-Installation $AppID
function Uninstall-App ($AppID, $AppArgs, $Source = 'winget') {
$IsInstalled = Confirm-Installation $AppID -src $Source
if ($IsInstalled) {
#Check if mods exist (or already exist) for preuninstall/uninstall/uninstalled
$ModsPreUninstall, $ModsUninstall, $ModsUninstalled = Test-ModsUninstall $AppID
Expand Down Expand Up @@ -244,7 +243,7 @@ function Uninstall-App ($AppID, $AppArgs) {
}

#Check if uninstall is ok
$IsInstalled = Confirm-Installation $AppID
$IsInstalled = Confirm-Installation $AppID -src $Source
if (!($IsInstalled)) {
Write-ToLog "-> $AppID successfully uninstalled." "Green"
if ($ModsUninstalled) {
Expand Down Expand Up @@ -399,14 +398,14 @@ if ($Winget) {

#Install or Uninstall command
if ($Uninstall) {
Uninstall-App $AppID $AppArgs
Uninstall-App $AppID $AppArgs $Source
}
else {
#Check if app exists on Winget Repo
$Exists = Confirm-Exist $AppID
if ($Exists) {
#Install
Install-App $AppID $AppArgs
Install-App $AppID $AppArgs $Source
}
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/Winget-AutoUpdate/Winget-Upgrade.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,12 @@ if (Test-Network) {
}
#if app is in "include list", update it
elseif ($toUpdate -contains $app.Id) {
Update-App $app
Update-App $app -src $Script:WingetSourceCustom
}
#if app with wildcard is in "include list", update it
elseif ($toUpdate | Where-Object { $app.Id -like $_ }) {
Write-ToLog "$($app.Name) is wildcard in the include list."
Update-App $app
Update-App $app -src $Script:WingetSourceCustom
}
Comment thread
Salamek marked this conversation as resolved.
#else, skip it
else {
Expand All @@ -338,7 +338,7 @@ if (Test-Network) {
}
# else, update it
else {
Update-App $app
Update-App $app -src $Script:WingetSourceCustom
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions Sources/Winget-AutoUpdate/functions/Confirm-Installation.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
.PARAMETER AppVer
Expected version prefix.

.PARAMETER src
The WinGet source to query (e.g. "winget", "msstore"). Required; passed
directly to `winget export -s`.

.OUTPUTS
Boolean: True if installed at version.
#>
Function Confirm-Installation ($AppName, $AppVer) {
Function Confirm-Installation ($AppName, $AppVer, $src) {
Comment thread
Salamek marked this conversation as resolved.

$JsonFile = "$env:TEMP\InstalledApps.json"
& $Winget export -s winget -o $JsonFile --include-versions | Out-Null
& $Winget export -s $src -o $JsonFile --include-versions | Out-Null

$Packages = (Get-Content $JsonFile -Raw | ConvertFrom-Json).Sources.Packages
$match = $Packages | Where-Object { $_.PackageIdentifier -eq $AppName -and $_.Version -like "$AppVer*" }
Comment thread
Salamek marked this conversation as resolved.
Expand Down
12 changes: 9 additions & 3 deletions Sources/Winget-AutoUpdate/functions/Get-AppInfo.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,25 @@
.PARAMETER AppID
The WinGet package identifier to query.

.PARAMETER src
The WinGet source to query (e.g. "winget", "msstore").

.OUTPUTS
String containing the release notes URL, or empty if not found.

.EXAMPLE
$releaseUrl = Get-AppInfo "Microsoft.PowerShell"
$releaseUrl = Get-AppInfo "Microsoft.PowerShell" "winget"

.NOTES
Uses WinGet show command with source agreements accepted.
#>
Function Get-AppInfo ($AppID) {
Function Get-AppInfo ($AppID, $src = 'winget') {

if ([string]::IsNullOrWhiteSpace($src)) {
$src = 'winget'
}
# Query WinGet for application details
$String = & $winget show $AppID --accept-source-agreements -s winget | Out-String
$String = & $winget show $AppID --accept-source-agreements -s $src | Out-String

# Extract Release Notes URL using regex
$ReleaseNote = [regex]::match($String, "(?<=Release Notes Url: )(.*)(?=\n)").Groups[0].Value
Expand Down
13 changes: 8 additions & 5 deletions Sources/Winget-AutoUpdate/functions/Update-App.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@

.PARAMETER app
PSCustomObject with Name, Id, Version, AvailableVersion properties.

.PARAMETER src
The WinGet source to use (e.g. 'winget', 'msstore'). Defaults to 'winget'.
#>
Function Update-App ($app) {
Function Update-App ($app, $src = "winget") {

Comment thread
Salamek marked this conversation as resolved.
# Helper function to build winget command parameters
function Get-WingetParams ($Command, $ModsOverride, $ModsCustom, $ModsArguments) {
$params = @($Command, "--id", $app.Id, "-e", "--accept-package-agreements", "--accept-source-agreements", "-s", "winget")
$params = @($Command, "--id", $app.Id, "-e", "--accept-package-agreements", "--accept-source-agreements", "-s", $src)
if ($Command -eq "install") { $params += "--force" }

if ($ModsOverride) {
Expand All @@ -31,7 +34,7 @@ Function Update-App ($app) {
}

# Get release notes for notification button
$ReleaseNoteURL = Get-AppInfo $app.Id
$ReleaseNoteURL = Get-AppInfo $app.Id $src
$Button1Text = if ($ReleaseNoteURL) { $NotifLocale.local.outputs.output[10].message } else { $null }

# Send "updating" notification
Expand Down Expand Up @@ -64,7 +67,7 @@ Function Update-App ($app) {
& $ModsUpgrade
}

$ConfirmInstall = Confirm-Installation $app.Id $app.AvailableVersion
$ConfirmInstall = Confirm-Installation $app.Id $app.AvailableVersion $src

# Fallback to install if upgrade failed
if (-not $ConfirmInstall) {
Expand All @@ -82,7 +85,7 @@ Function Update-App ($app) {
& $ModsInstall
}

$ConfirmInstall = Confirm-Installation $app.Id $app.AvailableVersion
$ConfirmInstall = Confirm-Installation $app.Id $app.AvailableVersion $src
}
}

Expand Down
24 changes: 12 additions & 12 deletions Sources/Winget-AutoUpdate/mods/_Mods-Functions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ function Wait-ModsProc ($Wait) {
Return
}

function Install-WingetID ($WingetIDInst) {
function Install-WingetID ($WingetIDInst, $src = 'winget') {
foreach ($app in $WingetIDInst) {
& $Winget install --id $app -e --accept-package-agreements --accept-source-agreements -s winget -h
& $Winget install --id $app -e --accept-package-agreements --accept-source-agreements -s $src -h
}
Return
}

function Uninstall-WingetID ($WingetIDUninst) {
function Uninstall-WingetID ($WingetIDUninst, $src = 'winget') {
foreach ($app in $WingetIDUninst) {
& $Winget uninstall --id $app -e --accept-source-agreements -s winget -h
& $Winget uninstall --id $app -e --accept-source-agreements -s $src -h
}
Return
}
Expand Down Expand Up @@ -188,37 +188,37 @@ function Remove-ModsLnk ($Lnk) {
function Add-ProgramsShortcuts ($Shortcuts, $ShortcutsTargets) {
$programsPath = "${env:ProgramData}\Microsoft\Windows\Start Menu\Programs"
$createdCount = 0

# Validate arrays match in length and are not just empty placeholders
if ($Shortcuts.Count -ne $ShortcutsTargets.Count -or ($Shortcuts.Count -eq 1 -and [string]::IsNullOrEmpty($Shortcuts[0]))) {
Return $createdCount
}

# Create WScript.Shell COM object
$WshShell = New-Object -ComObject WScript.Shell -ErrorAction SilentlyContinue
if (!$WshShell) {
Return $createdCount
}

# Iterate through shortcuts
for ($i = 0; $i -lt $Shortcuts.Count; $i++) {
$shortcutName = $Shortcuts[$i]
$targetPath = $ShortcutsTargets[$i]

# Skip empty entries
if ([string]::IsNullOrEmpty($shortcutName) -or [string]::IsNullOrEmpty($targetPath)) {
continue
}

# Construct full shortcut path
$shortcutPath = Join-Path $programsPath "$shortcutName.lnk"

# Create parent directory if it doesn't exist
$shortcutDir = Split-Path $shortcutPath -Parent
if (!(Test-Path $shortcutDir)) {
New-Item -Path $shortcutDir -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null
}

# Verify target exists
if (Test-Path $targetPath) {
# Create shortcut
Expand All @@ -232,7 +232,7 @@ function Add-ProgramsShortcuts ($Shortcuts, $ShortcutsTargets) {
$createdCount++
}
}

Return $createdCount
}

Expand Down
Loading