From 81aabf731949410bdf8cb5b3a723a142b812b5c9 Mon Sep 17 00:00:00 2001 From: Romain <96626929+Romanitho@users.noreply.github.com> Date: Mon, 20 Nov 2023 16:30:46 +0100 Subject: [PATCH 1/4] WingetCmd simplified --- Winget-AutoUpdate/Winget-Upgrade.ps1 | 9 +++- Winget-AutoUpdate/functions/Get-WingetCmd.ps1 | 41 ++++++------------- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Winget-AutoUpdate/Winget-Upgrade.ps1 index b82c1d1..d262ac6 100644 --- a/Winget-AutoUpdate/Winget-Upgrade.ps1 +++ b/Winget-AutoUpdate/Winget-Upgrade.ps1 @@ -92,9 +92,14 @@ Write-ToLog "Notification Level: $($WAUConfig.WAU_NotificationLevel). Notificati #Check network connectivity if (Test-Network) { #Check if Winget is installed and get Winget cmd - $TestWinget = Get-WingetCmd + $Script:Winget = Get-WingetCmd + + if ($Winget) { + + #Log Winget installed version + $WingetVer = & $Winget --version + Write-ToLog "Winget Version: $WingetVer" - if ($TestWinget) { #Get Current Version $WAUCurrentVersion = $WAUConfig.DisplayVersion Write-ToLog "WAU current version: $WAUCurrentVersion" diff --git a/Winget-AutoUpdate/functions/Get-WingetCmd.ps1 b/Winget-AutoUpdate/functions/Get-WingetCmd.ps1 index 22d2d70..a4a6781 100644 --- a/Winget-AutoUpdate/functions/Get-WingetCmd.ps1 +++ b/Winget-AutoUpdate/functions/Get-WingetCmd.ps1 @@ -2,39 +2,22 @@ Function Get-WingetCmd { - #Get WinGet Path (if Admin context) - # Includes Workaround for ARM64 (removed X64 and replaces it with a wildcard) - $ResolveWingetPath = Resolve-Path "$env:ProgramFiles\WindowsApps\Microsoft.DesktopAppInstaller_*_*__8wekyb3d8bbwe" | Sort-Object { [version]($_.Path -replace '^[^\d]+_((\d+\.)*\d+)_.*', '$1') } + $WingetCmd = $null - if ($ResolveWingetPath) { - #If multiple version, pick last one - $WingetPath = $ResolveWingetPath[-1].Path + #Get WinGet Path + try { + #Get Admin Context Winget Location + $WingetInfo = (Get-Item "$env:ProgramFiles\WindowsApps\Microsoft.DesktopAppInstaller_*_8wekyb3d8bbwe\winget.exe").VersionInfo | Sort-Object -Property FileVersionRaw + #If multiple versions, pick most recent one + $WingetCmd = $WingetInfo[-1].FileName } - - #If running under System or Admin context obtain Winget from Program Files - if((([System.Security.Principal.WindowsIdentity]::GetCurrent().User) -eq "S-1-5-18") -or ($WingetPath)){ - if (Test-Path "$WingetPath\winget.exe") { - $Script:Winget = "$WingetPath\winget.exe" - } - }else{ - #Get Winget Location in User context + catch { + #Get User context Winget Location if (Test-Path "$env:LocalAppData\Microsoft\WindowsApps\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\winget.exe") { - $Script:Winget = "$env:LocalAppData\Microsoft\WindowsApps\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\winget.exe" + $WingetCmd = "$env:LocalAppData\Microsoft\WindowsApps\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\winget.exe" } } - If(!($Script:Winget)){ - Write-ToLog "Winget not installed or detected !" "Red" - return $false - } + return $WingetCmd - #Run winget to list apps and accept source agrements (necessary on first run) - & $Winget list --accept-source-agreements -s winget | Out-Null - - #Log Winget installed version - $WingetVer = & $Winget --version - Write-ToLog "Winget Version: $WingetVer" - - return $true - -} +} \ No newline at end of file From 6fe4999be13998691b409e143c17e360864ab844 Mon Sep 17 00:00:00 2001 From: Romain <96626929+Romanitho@users.noreply.github.com> Date: Mon, 20 Nov 2023 17:31:38 +0100 Subject: [PATCH 2/4] Review Add-scopeMachine --- Winget-AutoUpdate/Winget-Upgrade.ps1 | 3 +- .../functions/Add-ScopeMachine.ps1 | 28 +++++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Winget-AutoUpdate/Winget-Upgrade.ps1 index d262ac6..68a2d9f 100644 --- a/Winget-AutoUpdate/Winget-Upgrade.ps1 +++ b/Winget-AutoUpdate/Winget-Upgrade.ps1 @@ -81,8 +81,7 @@ if ($IsSystem) { Invoke-PostUpdateActions } #Run Scope Machine funtion if run as System - $SettingsPath = "$Env:windir\system32\config\systemprofile\AppData\Local\Microsoft\WinGet\Settings\defaultState\settings.json" - Add-ScopeMachine $SettingsPath + Add-ScopeMachine } #Get Notif Locale function diff --git a/Winget-AutoUpdate/functions/Add-ScopeMachine.ps1 b/Winget-AutoUpdate/functions/Add-ScopeMachine.ps1 index 0cf6145..9131665 100644 --- a/Winget-AutoUpdate/functions/Add-ScopeMachine.ps1 +++ b/Winget-AutoUpdate/functions/Add-ScopeMachine.ps1 @@ -1,20 +1,32 @@ #Function to configure the prefered scope option as Machine -function Add-ScopeMachine ($SettingsPath) { +function Add-ScopeMachine { + #Get Settings path for system or current user + if ([System.Security.Principal.WindowsIdentity]::GetCurrent().IsSystem) { + $SettingsPath = "$Env:windir\System32\config\systemprofile\AppData\Local\Microsoft\WinGet\Settings\settings.json" + } + else { + $SettingsPath = "$env:LOCALAPPDATA\Packages\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\LocalState\settings.json" + } + + $ConfigFile = @{} + + #Check if setting file exist, if not create it if (Test-Path $SettingsPath) { $ConfigFile = Get-Content -Path $SettingsPath | Where-Object { $_ -notmatch '//' } | ConvertFrom-Json } - if (!$ConfigFile) { - $ConfigFile = @{} + else { + New-Item -Path $SettingsPath -Force | Out-Null } - if ($ConfigFile.installBehavior.preferences.scope) { - $ConfigFile.installBehavior.preferences.scope = "Machine" + + if ($ConfigFile.installBehavior.preferences) { + Add-Member -InputObject $ConfigFile.installBehavior.preferences -MemberType NoteProperty -Name 'scope' -Value 'Machine' -Force } else { - $Scope = New-Object PSObject -Property $(@{scope = "Machine" }) + $Scope = New-Object PSObject -Property $(@{scope = 'Machine' }) $Preference = New-Object PSObject -Property $(@{preferences = $Scope }) Add-Member -InputObject $ConfigFile -MemberType NoteProperty -Name 'installBehavior' -Value $Preference -Force } - $ConfigFile | ConvertTo-Json -Depth 100 | Out-File $SettingsPath -Encoding utf8 -Force -} + $ConfigFile | ConvertTo-Json -Depth 100 | Out-File $SettingsPath -Encoding utf8 -Force +} \ No newline at end of file From 40cb5da9ecd5ace4584aaa61004f3df892857907 Mon Sep 17 00:00:00 2001 From: Romain <96626929+Romanitho@users.noreply.github.com> Date: Mon, 20 Nov 2023 17:59:49 +0100 Subject: [PATCH 3/4] logs --- Winget-AutoUpdate/functions/Update-WinGet.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Winget-AutoUpdate/functions/Update-WinGet.ps1 b/Winget-AutoUpdate/functions/Update-WinGet.ps1 index 4aa52a0..cda5d8e 100644 --- a/Winget-AutoUpdate/functions/Update-WinGet.ps1 +++ b/Winget-AutoUpdate/functions/Update-WinGet.ps1 @@ -68,12 +68,12 @@ Function Update-WinGet { #If multiple versions, pick most recent one $WingetCmd = $WingetInfo[-1].FileName & $WingetCmd source reset --force - Write-ToLog "-> WinGet sources reset." "green" + Write-ToLog "-> WinGet sources reset.`n" "green" $return = "success" } catch { - Write-ToLog "-> Failed to install WinGet MSIXBundle for App Installer..." "red" + Write-ToLog "-> Failed to install WinGet MSIXBundle for App Installer...`n" "red" #Force Store Apps to update Update-StoreApps $return = "fail" @@ -86,7 +86,7 @@ Function Update-WinGet { return $return } else { - Write-ToLog "-> WinGet is up to date: v$WinGetInstalledVersion" "Green" + Write-ToLog "-> WinGet is up to date: v$WinGetInstalledVersion`n" "Green" return "current" } } From 877f75f2f870bd6aa5d52bfd9d17bf7156dd5da2 Mon Sep 17 00:00:00 2001 From: romanitho <96626929+Romanitho@users.noreply.github.com> Date: Tue, 21 Nov 2023 12:52:47 +0100 Subject: [PATCH 4/4] minor --- Winget-AutoUpdate/functions/Add-ScopeMachine.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Winget-AutoUpdate/functions/Add-ScopeMachine.ps1 b/Winget-AutoUpdate/functions/Add-ScopeMachine.ps1 index 9131665..0e45373 100644 --- a/Winget-AutoUpdate/functions/Add-ScopeMachine.ps1 +++ b/Winget-AutoUpdate/functions/Add-ScopeMachine.ps1 @@ -20,12 +20,12 @@ function Add-ScopeMachine { } if ($ConfigFile.installBehavior.preferences) { - Add-Member -InputObject $ConfigFile.installBehavior.preferences -MemberType NoteProperty -Name 'scope' -Value 'Machine' -Force + Add-Member -InputObject $ConfigFile.installBehavior.preferences -MemberType NoteProperty -Name "scope" -Value "Machine" -Force } else { - $Scope = New-Object PSObject -Property $(@{scope = 'Machine' }) + $Scope = New-Object PSObject -Property $(@{scope = "Machine" }) $Preference = New-Object PSObject -Property $(@{preferences = $Scope }) - Add-Member -InputObject $ConfigFile -MemberType NoteProperty -Name 'installBehavior' -Value $Preference -Force + Add-Member -InputObject $ConfigFile -MemberType NoteProperty -Name "installBehavior" -Value $Preference -Force } $ConfigFile | ConvertTo-Json -Depth 100 | Out-File $SettingsPath -Encoding utf8 -Force