Skip to content

Commit

Permalink
Merge pull request #419 from KnifMelti/Update-Winget
Browse files Browse the repository at this point in the history
Better Functions
  • Loading branch information
Romanitho authored Oct 14, 2023
2 parents abef811 + cb82fb1 commit 11f3811
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 96 deletions.
61 changes: 16 additions & 45 deletions Winget-AutoUpdate-Install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ param(

<# FUNCTIONS #>

#Include external Functions
. "$PSScriptRoot\Winget-AutoUpdate\functions\Start-Init.ps1"
. "$PSScriptRoot\Winget-AutoUpdate\functions\Invoke-ModsProtect.ps1"
. "$PSScriptRoot\Winget-AutoUpdate\functions\Get-WinGetAvailableVersion.ps1"
. "$PSScriptRoot\Winget-AutoUpdate\functions\Update-WinGet.ps1"
. "$PSScriptRoot\Winget-AutoUpdate\functions\Update-StoreApps.ps1"

function Install-Prerequisites {

Write-Host "`nChecking prerequisites..." -ForegroundColor Yellow
Expand Down Expand Up @@ -163,10 +170,10 @@ function Install-Prerequisites {
Write-host "-> Installing VC_redist.$OSArch.exe..."
Start-Process -FilePath $Installer -Args "/quiet /norestart" -Wait
Remove-Item $Installer -ErrorAction Ignore
Write-host "MS Visual C++ 2015-2022 installed successfully" -ForegroundColor Green
Write-host "-> MS Visual C++ 2015-2022 installed successfully" -ForegroundColor Green
}
catch {
Write-host "MS Visual C++ 2015-2022 installation failed." -ForegroundColor Red
Write-host "-> MS Visual C++ 2015-2022 installation failed." -ForegroundColor Red
Start-Sleep 3
}
}
Expand All @@ -175,18 +182,14 @@ function Install-Prerequisites {
}
}
else {
Write-Host "Prerequisites checked. OK" -ForegroundColor Green
Write-Host "-> Prerequisites checked. OK" -ForegroundColor Green
}
}

function Install-WinGet {

Write-Host "`nChecking if WinGet is installed/up to date" -ForegroundColor Yellow

#Include external Functions
. "$PSScriptRoot\Winget-AutoUpdate\functions\Get-WinGetAvailableVersion.ps1"
. "$PSScriptRoot\Winget-AutoUpdate\functions\Get-StoreApps.ps1"

#Check available WinGet version, if fail set version to the latest version as of 2023-10-08
$WinGetAvailableVersion = Get-WinGetAvailableVersion
if (!$WinGetAvailableVersion) {
Expand Down Expand Up @@ -225,10 +228,10 @@ function Install-WinGet {
try {
Write-Host "-> Installing Microsoft.UI.Xaml.2.7..."
Add-AppxProvisionedPackage -Online -PackagePath "$WingetUpdatePath\extracted\tools\AppX\x64\Release\Microsoft.UI.Xaml.2.7.appx" -SkipLicense | Out-Null
Write-host "Microsoft.UI.Xaml.2.7 installed successfully" -ForegroundColor Green
Write-host "-> Microsoft.UI.Xaml.2.7 installed successfully" -ForegroundColor Green
}
catch {
Write-Host "Failed to intall Wicrosoft.UI.Xaml.2.7..." -ForegroundColor Red
Write-Host "-> Failed to intall Wicrosoft.UI.Xaml.2.7..." -ForegroundColor Red
}
Remove-Item -Path $UiXamlZip -Force
Remove-Item -Path "$WingetUpdatePath\extracted" -Force -Recurse
Expand All @@ -242,50 +245,20 @@ function Install-WinGet {
try {
Write-Host "-> Installing Microsoft.VCLibs.140.00.UWPDesktop..."
Add-AppxProvisionedPackage -Online -PackagePath $VCLibsFile -SkipLicense | Out-Null
Write-host "Microsoft.VCLibs.140.00.UWPDesktop installed successfully" -ForegroundColor Green
Write-host "-> Microsoft.VCLibs.140.00.UWPDesktop installed successfully" -ForegroundColor Green
}
catch {
Write-Host "Failed to intall Microsoft.VCLibs.140.00.UWPDesktop..." -ForegroundColor Red
Write-Host "-> Failed to intall Microsoft.VCLibs.140.00.UWPDesktop..." -ForegroundColor Red
}
Remove-Item -Path $VCLibsFile -Force
}

#Download WinGet MSIXBundle
Write-Host "-> Downloading WinGet MSIXBundle for App Installer..."
$WinGetURL = "https://github.com/microsoft/winget-cli/releases/download/v$WinGetAvailableVersion/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle"
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile($WinGetURL, "$WingetUpdatePath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle")

#Install WinGet MSIXBundle in SYSTEM context
try {
Write-Host "-> Installing WinGet MSIXBundle for App Installer..."
Add-AppxProvisionedPackage -Online -PackagePath "$WingetUpdatePath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -SkipLicense | Out-Null
Write-host "WinGet MSIXBundle (v$WinGetAvailableVersion) for App Installer installed successfully" -ForegroundColor Green

#Reset WinGet Sources
$ResolveWingetPath = Resolve-Path "$env:programfiles\WindowsApps\Microsoft.DesktopAppInstaller_*_*__8wekyb3d8bbwe\winget.exe" | Sort-Object { [version]($_.Path -replace '^[^\d]+_((\d+\.)*\d+)_.*', '$1') }
if ($ResolveWingetPath) {
#If multiple version, pick last one
$WingetPath = $ResolveWingetPath[-1].Path
& $WingetPath source reset --force
#log
Write-Host "-> WinGet sources reset." -ForegroundColor Green
}
}
catch {
Write-Host "Failed to intall WinGet MSIXBundle for App Installer..." -ForegroundColor Red
}
Update-WinGet $WinGetAvailableVersion $WingetUpdatePath

#Remove WinGet MSIXBundle
Remove-Item -Path "$WingetUpdatePath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -Force -ErrorAction Continue
}
elseif ($WinGetAvailableVersion -match "-pre") {
Write-Host "-> WinGet is probably up to date (v$WinGetInstalledVersion) - v$WinGetAvailableVersion is available but only as a Pre-release" -ForegroundColor Yellow
#If not WSB or Server, upgrade Microsoft Store Apps!
if (!(Test-Path "${env:SystemDrive}\Users\WDAGUtilityAccount") -and (Get-CimInstance Win32_OperatingSystem).Caption -notmatch "Windows Server") {
Write-Host "-> Forcing an upgrade of Store Apps (this can take a minute)..." -ForegroundColor Yellow
Get-StoreApps
}
Update-StoreApps
}
else {
Write-Host "-> WinGet is up to date: v$WinGetInstalledVersion" -ForegroundColor Green
Expand Down Expand Up @@ -479,12 +452,10 @@ function Install-WingetAutoUpdate {


#Log file and symlink initialization
. "$WingetUpdatePath\functions\Start-Init.ps1"
Start-Init

#Security check
Write-host "`nChecking Mods Directory:" -ForegroundColor Yellow
. "$WingetUpdatePath\functions\Invoke-ModsProtect.ps1"
$Protected = Invoke-ModsProtect "$WingetUpdatePath\mods"
if ($Protected -eq $True) {
Write-Host "The mods directory is now secured!`n" -ForegroundColor Green
Expand Down
14 changes: 0 additions & 14 deletions Winget-AutoUpdate/functions/Get-StoreApps.ps1

This file was deleted.

41 changes: 4 additions & 37 deletions Winget-AutoUpdate/functions/Invoke-PostUpdateActions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ function Invoke-PostUpdateActions {

# Check if Intune Management Extension Logs folder and WAU-updates.log exists, make symlink
if ((Test-Path -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs" -ErrorAction SilentlyContinue) -and !(Test-Path -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-updates.log" -ErrorAction SilentlyContinue)) {
Write-ToLog -LogMsg '-> Creating SymLink for log file in Intune Management Extension log folder' -LogColor 'yellow'
Write-ToLog "-> Creating SymLink for log file (WAU-updates) in Intune Management Extension log folder" "yellow"
$null = New-Item -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-updates.log" -ItemType SymbolicLink -Value $LogFile -Force -ErrorAction SilentlyContinue
}

# Check if Intune Management Extension Logs folder and WAU-install.log exists, make symlink
if ((Test-Path -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs" -ErrorAction SilentlyContinue) -and (Test-Path -Path ('{0}\logs\install.log' -f $WorkingDir) -ErrorAction SilentlyContinue) -and !(Test-Path -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-install.log" -ErrorAction SilentlyContinue)) {
Write-Host -Object "`nCreating SymLink for log file (WAU-install) in Intune Management Extension log folder" -ForegroundColor Yellow
Write-ToLog "-> Creating SymLink for log file (WAU-install) in Intune Management Extension log folder" "yellow"
$null = (New-Item -Path "${env:ProgramData}\Microsoft\IntuneManagementExtension\Logs\WAU-install.log" -ItemType SymbolicLink -Value ('{0}\logs\install.log' -f $WorkingDir) -Force -Confirm:$False -ErrorAction SilentlyContinue)
}

Expand All @@ -36,45 +36,12 @@ function Invoke-PostUpdateActions {

#Check if the current available WinGet isn't a Pre-release and if it's newer than the installed
if (!($WinGetAvailableVersion -match "-pre") -and ($WinGetAvailableVersion -gt $WinGetInstalledVersion)) {

Write-ToLog "-> WinGet is not installed/up to date (v$WinGetInstalledVersion) - v$WinGetAvailableVersion is available:" "red"

#Download WinGet MSIXBundle
Write-ToLog "-> Downloading WinGet MSIXBundle for App Installer..."
$WinGetURL = "https://github.com/microsoft/winget-cli/releases/download/v$WinGetAvailableVersion/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle"
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile($WinGetURL, "$($WAUConfig.InstallLocation)\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle")

#Install WinGet MSIXBundle in SYSTEM context
try {
Write-ToLog "-> Installing WinGet MSIXBundle for App Installer..."
Add-AppxProvisionedPackage -Online -PackagePath "$($WAUConfig.InstallLocation)\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -SkipLicense | Out-Null
Write-ToLog "-> WinGet MSIXBundle (v$WinGetAvailableVersion) for App Installer installed successfully" "green"

#Reset WinGet Sources
$ResolveWingetPath = Resolve-Path "$env:programfiles\WindowsApps\Microsoft.DesktopAppInstaller_*_*__8wekyb3d8bbwe\winget.exe" | Sort-Object { [version]($_.Path -replace '^[^\d]+_((\d+\.)*\d+)_.*', '$1') }
if ($ResolveWingetPath) {
#If multiple version, pick last one
$WingetPath = $ResolveWingetPath[-1].Path
& $WingetPath source reset --force
#log
Write-ToLog "-> WinGet sources reset." "green"
}
}
catch {
Write-ToLog "-> Failed to intall WinGet MSIXBundle for App Installer..." "red"
}

#Remove WinGet MSIXBundle
Remove-Item -Path "$($WAUConfig.InstallLocation)\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -Force -ErrorAction Continue
Update-WinGet $WinGetAvailableVersion $($WAUConfig.InstallLocation) $true
}
elseif ($WinGetAvailableVersion -match "-pre") {
Write-ToLog "-> WinGet is probably up to date (v$WinGetInstalledVersion) - v$WinGetAvailableVersion is available but only as a Pre-release" "yellow"
#If not WSB or Server, upgrade Microsoft Store Apps!
if (!(Test-Path "${env:SystemDrive}\Users\WDAGUtilityAccount") -and (Get-CimInstance Win32_OperatingSystem).Caption -notmatch "Windows Server") {
Write-ToLog "-> Forcing an upgrade of Store Apps (this can take a minute)..." "yellow"
Get-StoreApps
}
Update-StoreApps $true
}
else {
Write-ToLog "-> WinGet is up to date: v$WinGetInstalledVersion" "green"
Expand Down
29 changes: 29 additions & 0 deletions Winget-AutoUpdate/functions/Update-StoreApps.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#Function to force an upgrade of Store Apps

Function Update-StoreApps ($Log = $false) {

$force_string = "-> Forcing an upgrade of Store Apps (this can take a minute)..."
$fail_string = "-> ...something went wrong!"

#If not WSB or Server, upgrade Microsoft Store Apps!
if (!(Test-Path "${env:SystemDrive}\Users\WDAGUtilityAccount") -and (Get-CimInstance Win32_OperatingSystem).Caption -notmatch "Windows Server") {
switch ($Log) {
$true {Write-ToLog $force_string "yellow"}
Default {Write-Host $force_string -ForegroundColor Yellow}
}
try {
$namespaceName = "root\cimv2\mdm\dmmap"
$className = "MDM_EnterpriseModernAppManagement_AppManagement01"
$wmiObj = Get-WmiObject -Namespace $namespaceName -Class $className
$wmiObj.UpdateScanMethod() | Out-Null
return $true
}
catch {
switch ($Log) {
$true {Write-ToLog $fail_string "red"}
Default {Write-Host $fail_string -ForegroundColor Red}
}
return $false
}
}
}
54 changes: 54 additions & 0 deletions Winget-AutoUpdate/functions/Update-WinGet.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#Function to download and update WinGet

Function Update-WinGet ($WinGetAvailableVersion, $DownloadPath, $Log = $false) {

$download_string = "-> Downloading WinGet MSIXBundle for App Installer..."
$install_string = "-> Installing WinGet MSIXBundle for App Installer..."
$success_string = "-> WinGet MSIXBundle (v$WinGetAvailableVersion) for App Installer installed successfully"
$reset_string = "-> WinGet sources reset."
$fail_string = "-> Failed to intall WinGet MSIXBundle for App Installer..."

#Download WinGet MSIXBundle
switch ($Log) {
$true {Write-ToLog $download_string}
Default {Write-Host $download_string}
}
$WinGetURL = "https://github.com/microsoft/winget-cli/releases/download/v$WinGetAvailableVersion/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle"
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile($WinGetURL, "$DownloadPath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle")

#Install WinGet MSIXBundle in SYSTEM context
try {
switch ($Log) {
$true {Write-ToLog $install_string}
Default {Write-Host $install_string}
}
Add-AppxProvisionedPackage -Online -PackagePath "$DownloadPath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -SkipLicense | Out-Null
switch ($Log) {
$true {Write-ToLog $success_string "green"}
Default {Write-host $success_string -ForegroundColor Green}
}

#Reset WinGet Sources
$ResolveWingetPath = Resolve-Path "$env:programfiles\WindowsApps\Microsoft.DesktopAppInstaller_*_*__8wekyb3d8bbwe\winget.exe" | Sort-Object { [version]($_.Path -replace '^[^\d]+_((\d+\.)*\d+)_.*', '$1') }
if ($ResolveWingetPath) {
switch ($Log) {
$true {Write-ToLog $reset_string "green"}
Default {Write-Host $reset_string -ForegroundColor Green}
}
#If multiple version, pick last one
$WingetPath = $ResolveWingetPath[-1].Path
& $WingetPath source reset --force
}
}
catch {
switch ($Log) {
$true {Write-ToLog $fail_string "red"}
Default {Write-Host $fail_string -ForegroundColor Red}
}
Update-StoreApps
}

#Remove WinGet MSIXBundle
Remove-Item -Path "$DownloadPath\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -Force -ErrorAction Continue
}

0 comments on commit 11f3811

Please sign in to comment.