diff --git a/Winget-AutoUpdate/Winget-Upgrade.ps1 b/Winget-AutoUpdate/Winget-Upgrade.ps1 index b82c1d1..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 @@ -92,9 +91,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/Add-ScopeMachine.ps1 b/Winget-AutoUpdate/functions/Add-ScopeMachine.ps1 index 0cf6145..0e45373 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" }) $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 -} + $ConfigFile | ConvertTo-Json -Depth 100 | Out-File $SettingsPath -Encoding utf8 -Force +} \ No newline at end of file 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 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" } }