Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Added WinGet-WrapperRequirements.ps1 
# Requirements script to check if desired application is installed. To be used when only wanting to update the application if already installed.  (UpdateOnly)

WinGet-WrapperDetection.ps1
# Version 2.0 - 21-08-2023 SOLU - Removing AutoUpdate completely. Feature does not support InTune with "Available" deployments, as detection rules only runs periodically for "Required" deployments.
# Version 2.1 - 22-08-2023 SOLU - Adding UpdateOnly. To be used when only wanting to update apps but not install if not detected.
  • Loading branch information
SorenLundt authored Aug 22, 2023
1 parent a4b75e6 commit 707fa3a
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 42 deletions.
Binary file modified WinGet-Wrapper.intunewin
Binary file not shown.
66 changes: 24 additions & 42 deletions WinGet-WrapperDetection.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
# Version 1.7 - 14-03-2023 SOLU - Added functionality to auto update - fixes issue #1 on https://github.com/SorenLundt/WinGet-Wrapper.
# Version 1.8 - 26-05-2023 SOLU - Added support for context choice by adding $Context variable
# Version 1.9 - 30-05-2023 SOLU - Fixed issues with running in user context (Winget path issues) + Updated version regex to be more precise
# Version 2.0 - 21-08-2023 SOLU - AutoUpdate Feature does not work well with Available deployment type in InTune, due to InTune not
# Version 2.0 - 21-08-2023 SOLU - Removing AutoUpdate completely. Feature does not support InTune with "Available" deployments, as detection rules only runs periodically for "Required" deployments.
# Version 2.1 - 22-08-2023 SOLU - Adding UpdateOnly. To be used when only wanting to update apps but not install if not detected.

# Settings
$id = "Exact WinGet package ID" # WinGet Package ID - ex. VideoLAN.VLC
$id = "Exact WinGet Package ID" # WinGet Package ID - ex. VideoLAN.VLC
$TargetVersion = "" # Set if specific version is desired (Optional)
$AcceptNewerVersion = $True # Allows locally installed versions to be newer than $TargetVersion or available WinGet package version
$AutoUpdate = $False # If $True will update application if newer available on winget. Modify arguments below in $AutoUpdateArgumentList
$AutoUpdateArgumentList = "update --exact --id $id --silent --disable-interactivity --accept-package-agreements --accept-source-agreements --scope Machine"
$AutoUpdateStopProcess = "" # Stop-process if set, blank no process is stopped before update
$UpdateOnly = $False # If set to $True will only update application if installed. If set to $False will install regardless
$Context = "System" # Set to either System or User

# Create log folder
Expand Down Expand Up @@ -113,55 +112,38 @@ try {
else {
$searchString = winget.exe list "$id" --exact --accept-source-agreements
}
$versions = [regex]::Matches($searchString, "(?m)^.*$id\s*(?:[>]?[\s]*)([\d.]+).*?$").Groups[1].Value
$versions = [regex]::Matches($searchString, "(?m)^.*$id\s*(?:[<>]?[\s]*)([\d.]+).*?$").Groups[1].Value


if ($versions) {
$InstalledVersion = ($versions | sort {[version]$_} | select -Last 1)
Write-Output "Installed version: $InstalledVersion"
}
else {
Write-Output "Package not found - exit 1"
Write-Output "Package not found - #exit 1"
exit 1
}
} catch {
if ($UpdateOnly -eq $True) {
Write-Output "Application '$id' not installed. Nothing to update"
# Exit 0 - Not installed, but updateonly so report 0
exit 0
} elseif ($UpdateOnly -ne $True) {
Write-Output "Application '$id' not installed. Reporting 'Not Installed'"
# Exit 1 - Report Not Installed
exit 1
} else {
Write-Output "Failed to get installed version: $($_.Exception.Message)"
# Exit 1 - Report Not Installed
exit 1
}
}
catch {
Write-Output "Failed to get installed version: $($_.Exception.Message)"
exit 1
}


if ($InstalledVersion -ge $TargetVersion -or ($AcceptNewerVersion -and $InstalledVersion -gt $TargetVersion)){
Write-Output "Exit 0 - Report Installed"
exit 0 # exit 0 - report installed
}
elseif ($AutoUpdate -eq $True){

#Stop process
if (-not ($AutoUpdateStopProcess -eq $null) -and $AutoUpdateStopProcess -ne "") {
Stop-Process -Name $AutoUpdateStopProcess -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
Write-Host "Stopped process: $AutoUpdateStopProcess"
}

$stdout = "$env:ProgramData\WinGet-WrapperLogs\StdOut-$timestamp.txt"
$errout = "$env:ProgramData\WinGet-WrapperLogs\ErrOut-$timestamp.txt"

Write-Host "Executing Winget.exe $AutoUpdateArgumentList"
if ($Context -contains "System"){
Start-Process "$WingetPath\winget.exe" -ArgumentList "$AutoUpdateArgumentList" -PassThru -Wait -RedirectStandardOutput "$stdout" -RedirectStandardError "$errout"
}
else {
Start-Process "winget.exe" -ArgumentList "$AutoUpdateArgumentList" -PassThru -Wait -RedirectStandardOutput "$stdout" -RedirectStandardError "$errout"
}

get-content "$stdout"
get-content "$errout"
Remove-item -Path "$stdout"
Remove-item -Path "$errout"
Write-Output "Exit 0 - Report Installed"
exit 0 # exit 0 - updated to latest version
Write-Output "#exit 0 - Report Installed"
exit 0 ##exit 0 - report installed
}
else {
Write-Output "Exit 1 - Report Not Installed"
exit 1 # exit 1 - report not installed
Write-Output "#exit 1 - Report Not Installed"
exit 1 # #exit 1 - report not installed
}
99 changes: 99 additions & 0 deletions WinGet-WrapperRequirements.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Soren Lundt - 22-08-2023 - https://github.com/SorenLundt/WinGet-Wrapper
# Requirements script to check if desired application is installed. To be used when only wanting to update the application if already installed. (UpdateOnly)
#
# Version History:
# Version 1.0 - 22-08-2023 SOLU - Initial version.

# Settings
$id = "Exact WinGet Package ID" # WinGet Package ID - ex. VideoLAN.VLC
$Context = "System" # Set to either System or User

# Create log folder
$logPath = "$env:ProgramData\WinGet-WrapperLogs"
if (!(Test-Path -Path $logPath)) {
try {
New-Item -Path $logPath -Force -ItemType Directory | Out-Null
}
catch {
Write-Output "Failed to create log directory: $($_.Exception.Message)"
exit 1
}
}

# Set up log file
$timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
$logFile = "$logPath\$($id)_WinGet_RequirementsUpdateOnly_$($timestamp).log"
try {
Start-Transcript -Path $logFile -Append
}
catch {
Write-Output "Failed to start transcript: $($_.Exception.Message)"
exit 1
}

#Write useful variables to log
Write-OutPut "ID = $id"

# Clean log files older than X days
$daysToKeep = 60
$filesToDelete = Get-ChildItem $logPath -Recurse -Include *.log | Where-Object LastWriteTime -lt (Get-Date).AddDays(-$daysToKeep)
try {
$count = $filesToDelete.Count
$filesToDelete | Remove-Item -Force | Out-Null
Write-Output "Cleaned up a total of $count old logs older than $daysToKeep days."
}
catch {
Write-Output "Failed to delete old log files: $($_.Exception.Message)"
}

# Find WinGet.exe Location
if ($Context -contains "System"){
Write-Output "Running in System Context"
try {
$resolveWingetPath = Resolve-Path "C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe"
if ($resolveWingetPath) {
$wingetPath = $resolveWingetPath[-1].Path
Set-Location $wingetPath
$wingetPath = $wingetPath + "\winget.exe"
Write-Output "WinGet full path: $wingetPath"
}
else {
Write-Output "Failed to find WinGet path"
exit 1
}
}
catch {
Write-Output "Failed to find WinGet path: $($_.Exception.Message)"
exit 1
}
}
else{
Write-Output "Running in User Context"
$wingetPath = "winget.exe"
}

# Get version installed locally on machine
$InstalledVersion = $null # Clear Variable
try {
if ($Context -contains "System"){
$searchString = .\winget.exe list "$id" --exact --accept-source-agreements
}
else {
$searchString = winget.exe list "$id" --exact --accept-source-agreements
}
$versions = [regex]::Matches($searchString, "(?m)^.*$id\s*(?:[<>]?[\s]*)([\d.]+).*?$").Groups[1].Value


if ($versions) {
$InstalledVersion = ($versions | sort {[version]$_} | select -Last 1)
Write-Output "Installed version: $InstalledVersion"
}
else {
Write-Output "Package not found - exit 1"
exit 1
}
} catch {
Write-Output "Package not found - exit 1 - $($_.Exception.Message)"
# Exit 1 - Report Not Installed
exit 1
}

0 comments on commit 707fa3a

Please sign in to comment.