Merge pull request #250 from KnifMelti/Template

Mods Template/Functions
pull/264/head
Romain 2022-12-26 10:16:20 +01:00 committed by GitHub
commit 9d8339f6ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 167 additions and 7 deletions

View File

@ -83,7 +83,7 @@ else {
}
#Test if there was a list_/winget_error
if (Test-Path "$WorkingDir\logs\*_error.txt") {
if (Test-Path "$WorkingDir\logs\error.txt") {
$MessageType = "error"
}
else {

View File

@ -72,8 +72,8 @@ if (Test-Network) {
}
#Delete previous list_/winget_error (if they exist) if System
if (Test-Path "$WorkingDir\logs\*_error.txt") {
Remove-Item "$WorkingDir\logs\*_error.txt" -Force
if (Test-Path "$WorkingDir\logs\error.txt") {
Remove-Item "$WorkingDir\logs\error.txt" -Force
}
#Get External ListPath if System
@ -89,7 +89,7 @@ if (Test-Network) {
}
else {
Write-Log "Critical: List doesn't exist, exiting..." "Red"
New-Item "$WorkingDir\logs\list_error.txt" -Force
New-Item "$WorkingDir\logs\error.txt" -Value "List doesn't exist!" -Force
Exit 1
}
}
@ -135,7 +135,7 @@ if (Test-Network) {
if ($outdated -like "Problem:*") {
Write-Log "Critical: An error occured, exiting..." "red"
Write-Log "$outdated" "red"
New-Item "$WorkingDir\logs\winget_error.txt" -Value "$outdated" -Force
New-Item "$WorkingDir\logs\error.txt" -Value "$outdated" -Force
Exit 1
}
@ -216,6 +216,11 @@ if (Test-Network) {
Write-Log "User context execution not installed"
}
}
else {
Write-Log "Critical: An error occured, exiting..." "red"
New-Item "$WorkingDir\logs\error.txt" -Value "Winget not installed or detected!" -Force
Exit 1
}
}
#End

View File

@ -6,7 +6,7 @@ function Invoke-PostUpdateActions {
Write-Log "Running Post Update actions..." "yellow"
#Reset Winget Sources
$ResolveWingetPath = Resolve-Path "$env:programfiles\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe\winget.exe" | Sort-Object { [version]($_.Path -replace '^[^\d]+_((\d+\.)*\d+)_.*', '$1') }
$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

View File

@ -1 +1,7 @@
Pre/During/Post install custom scripts should be placed here
Pre/During/Post install/uninstall custom scripts should be placed here.
A script Template and Mods Functions are included as example to get you started...
Scripts that are considered:
**AppID**`-preinstall.ps1`, `-upgrade.ps1`, `-install.ps1`, `-installed.ps1`, `-preuninstall.ps1`, `-uninstall.ps1` or `-uninstalled.ps1`
**AppID**`-override.txt` (the content) will be used as a native **winget --override** parameter when upgrading

View File

@ -0,0 +1,32 @@
<# ARRAYS/VARIABLES #>
#Beginning of Process Name to Stop - optional wildcard (*) after, without .exe, multiple: "proc1","proc2"
$Proc = @("")
#Beginning of Process Name to Wait for to End - optional wildcard (*) after, without .exe, multiple: "proc1","proc2"
$Wait = @("")
#Beginning of App Name string to Silently Uninstall (MSI/NSIS/INNO/EXE with defined silent uninstall in registry)
#Required wildcard (*) after, search is done with "-like"!
$App = ""
#Beginning of Desktop Link Name to Remove - optional wildcard (*) after, without .lnk, multiple: "lnk1","lnk2"
$Lnk = @("")
<# FUNCTIONS #>
. $PSScriptRoot\_Mods-Functions.ps1
<# MAIN #>
if ($Proc) {
Stop-ModsProc $Proc
}
if ($Wait) {
Wait-ModsProc $Wait
}
if ($App) {
Uninstall-ModsApp $App
}
if ($Lnk) {
Remove-ModsLnk $Lnk
}
<# EXTRAS #>

View File

@ -0,0 +1,117 @@
#Common shared functions for mods handling
function Stop-ModsProc ($Proc) {
foreach ($process in $Proc)
{
Stop-Process -Name $process -Force -ErrorAction SilentlyContinue | Out-Null
}
Return
}
function Wait-ModsProc ($Wait) {
foreach ($process in $Wait)
{
Get-Process $process -ErrorAction SilentlyContinue | Foreach-Object { $_.WaitForExit() }
}
Return
}
function Uninstall-ModsApp ($App) {
$InstalledSoftware = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall"
foreach ($obj in $InstalledSoftware){
if ($obj.GetValue('DisplayName') -like $App) {
$UninstallString = $obj.GetValue('UninstallString')
if ($UninstallString -like "MsiExec.exe*") {
$ProductCode = Select-String "{.*}" -inputobject $UninstallString
$ProductCode = $ProductCode.matches.groups[0].value
#MSI x64 Installer
$Exec = Start-Process "C:\Windows\System32\msiexec.exe" -ArgumentList "/x$ProductCode REBOOT=R /qn" -PassThru -Wait
#Stop Hard Reboot (if bad MSI!)
if ($Exec.ExitCode -eq 1641) {
Start-Process "C:\Windows\System32\shutdown.exe" -ArgumentList "/a"
}
}
else {
$QuietUninstallString = $obj.GetValue('QuietUninstallString')
if ($QuietUninstallString) {
$QuietUninstallString = Select-String "(\x22.*\x22) +(.*)" -inputobject $QuietUninstallString
$Command = $QuietUninstallString.matches.groups[1].value
$Parameter = $QuietUninstallString.matches.groups[2].value
#All EXE x64 Installers (already defined silent uninstall)
Start-Process $Command -ArgumentList $Parameter -Wait
}
else {
$NullSoft = Select-String -Path $UninstallString.Trim([char]0x0022) -Pattern "Nullsoft"
if ($NullSoft) {
#NSIS x64 Installer
Start-Process $UninstallString -ArgumentList "/S" -Wait
}
else {
$Inno = Select-String -Path $UninstallString.Trim([char]0x0022) -Pattern "Inno Setup"
if ($Inno) {
#Inno x64 Installer
Start-Process $UninstallString -ArgumentList "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-" -Wait
}
else {
Write-Host "x64 Uninstaller unknown..."
}
}
}
}
$x64 = $true
break
}
}
if (!$x64) {
$InstalledSoftware = Get-ChildItem "HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
foreach ($obj in $InstalledSoftware){
if ($obj.GetValue('DisplayName') -like $App) {
$UninstallString = $obj.GetValue('UninstallString')
if ($UninstallString -like "MsiExec.exe*") {
$ProductCode = Select-String "{.*}" -inputobject $UninstallString
$ProductCode = $ProductCode.matches.groups[0].value
#MSI x86 Installer
$Exec = Start-Process "C:\Windows\System32\msiexec.exe" -ArgumentList "/x$ProductCode REBOOT=R /qn" -PassThru -Wait
#Stop Hard Reboot (if bad MSI!)
if ($Exec.ExitCode -eq 1641) {
Start-Process "C:\Windows\System32\shutdown.exe" -ArgumentList "/a"
}
}
else {
$QuietUninstallString = $obj.GetValue('QuietUninstallString')
if ($QuietUninstallString) {
$QuietUninstallString = Select-String "(\x22.*\x22) +(.*)" -inputobject $QuietUninstallString
$Command = $QuietUninstallString.matches.groups[1].value
$Parameter = $QuietUninstallString.matches.groups[2].value
#All EXE x86 Installers (already defined silent uninstall)
Start-Process $Command -ArgumentList $Parameter -Wait
}
else {
$NullSoft = Select-String -Path $UninstallString.Trim([char]0x0022) -Pattern "Nullsoft"
if ($NullSoft) {
#NSIS x86 Installer
Start-Process $UninstallString -ArgumentList "/S" -Wait
}
else {
$Inno = Select-String -Path $UninstallString.Trim([char]0x0022) -Pattern "Inno Setup"
if ($Inno) {
#Inno x86 Installer
Start-Process $UninstallString -ArgumentList "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-" -Wait
}
else {
Write-Host "x86 Uninstaller unknown..."
}
}
}
}
break
}
}
}
Return
}
function Remove-ModsLnk ($Lnk) {
foreach ($link in $Lnk)
{
Remove-Item -Path "${env:Public}\Desktop\$link.lnk" -Force -ErrorAction SilentlyContinue | Out-Null
}
Return
}