wingetautoupdate/Sources/Winget-AutoUpdate/mods/_Mods-Functions.ps1

232 lines
8.7 KiB
PowerShell
Raw Permalink Normal View History

2023-03-31 15:56:07 +00:00
#Common shared functions to handle the mods
2022-12-20 01:54:14 +00:00
2023-01-18 20:58:49 +00:00
function Invoke-ModsApp ($Run, $RunSwitch, $RunWait, $User) {
2024-03-19 06:59:05 +00:00
if (Test-Path "$Run") {
if (!$RunSwitch) { $RunSwitch = " " }
if (!$User) {
if (!$RunWait) {
Start-Process $Run -ArgumentList $RunSwitch
}
else {
Start-Process $Run -ArgumentList $RunSwitch -Wait
}
}
else {
Start-Process explorer $Run
}
}
Return
2023-01-09 00:25:39 +00:00
}
2022-12-20 01:54:14 +00:00
function Stop-ModsProc ($Proc) {
2024-03-19 06:59:05 +00:00
foreach ($process in $Proc) {
Stop-Process -Name $process -Force -ErrorAction SilentlyContinue | Out-Null
}
Return
2024-03-18 13:04:23 +00:00
}
function Stop-ModsSvc ($Svc) {
2024-09-02 14:10:21 +00:00
foreach ($service in $Svc) {
Stop-Service -Name $service -Force -ErrorAction SilentlyContinue | Out-Null
}
Return
2022-12-20 01:54:14 +00:00
}
2023-01-09 00:25:39 +00:00
2022-12-20 01:54:14 +00:00
function Wait-ModsProc ($Wait) {
2024-03-19 06:59:05 +00:00
foreach ($process in $Wait) {
Get-Process $process -ErrorAction SilentlyContinue | Foreach-Object { $_.WaitForExit() }
}
Return
2022-12-20 01:54:14 +00:00
}
2023-01-09 00:25:39 +00:00
2023-02-02 23:48:38 +00:00
function Install-WingetID ($WingetIDInst) {
2024-03-19 06:59:05 +00:00
foreach ($app in $WingetIDInst) {
& $Winget install --id $app -e --accept-package-agreements --accept-source-agreements -s winget -h
}
Return
2023-02-02 23:48:38 +00:00
}
function Uninstall-WingetID ($WingetIDUninst) {
2024-03-19 06:59:05 +00:00
foreach ($app in $WingetIDUninst) {
& $Winget uninstall --id $app -e --accept-source-agreements -s winget -h
}
Return
2023-02-02 23:48:38 +00:00
}
2024-09-02 14:10:21 +00:00
function Uninstall-ModsApp ($AppUninst, $AllVersions) {
foreach ($app in $AppUninst) {
# we start from scanning the x64 node in registry, if something was found, then we set x64=TRUE
[bool]$app_was_x64 = Process-installedSoftware -app $app -x64 $true;
# if nothing was found in x64 node, then we repeat that action in x86 node
2024-09-02 14:10:21 +00:00
if (!$app_was_x64) {
Process-installedSoftware -app $app | Out-Null;
}
}
Return
}
2024-09-02 14:10:21 +00:00
Function Process-installedSoftware() {
[OutputType([Bool])]
Param(
2024-09-02 14:10:21 +00:00
[parameter(Mandatory = $true)] [string]$app,
[parameter(Mandatory = $false)][bool] $x64 = $false
)
2024-09-02 14:10:21 +00:00
if ($true -eq $x64) {
[string]$path = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall";
}
2024-09-02 14:10:21 +00:00
else {
[string]$path = "HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
}
[bool]$app_was_found = $false;
[Microsoft.Win32.RegistryKey[]]$InstalledSoftware = Get-ChildItem $path;
2024-09-02 14:10:21 +00:00
foreach ($obj in $InstalledSoftware) {
if ($obj.GetValue('DisplayName') -like $App) {
$UninstallString = $obj.GetValue('UninstallString')
$CleanedUninstallString = $UninstallString.Trim([char]0x0022)
2024-09-02 14:10:21 +00:00
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!)
2024-09-02 14:10:21 +00:00
if ($Exec.ExitCode -eq 1641) {
Start-Process "C:\Windows\System32\shutdown.exe" -ArgumentList "/a"
2024-03-19 06:59:05 +00:00
}
}
2024-09-02 14:10:21 +00:00
else {
$QuietUninstallString = $obj.GetValue('QuietUninstallString')
2024-09-02 14:10:21 +00:00
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
}
2024-09-02 14:10:21 +00:00
else {
if ((Test-Path $CleanedUninstallString)) {
$NullSoft = Select-String -Path $CleanedUninstallString -Pattern "Nullsoft"
2024-03-19 06:59:05 +00:00
}
2024-09-02 14:10:21 +00:00
if ($NullSoft) {
#NSIS x64 Installer
Start-Process $UninstallString -ArgumentList "/S" -Wait
2024-03-19 06:59:05 +00:00
}
2024-09-02 14:10:21 +00:00
else {
if ((Test-Path $CleanedUninstallString)) {
$Inno = Select-String -Path $CleanedUninstallString -Pattern "Inno Setup"
2024-03-19 06:59:05 +00:00
}
2024-09-02 14:10:21 +00:00
if ($Inno) {
#Inno x64 Installer
Start-Process $UninstallString -ArgumentList "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-" -Wait
2024-03-19 06:59:05 +00:00
}
2024-09-02 14:10:21 +00:00
else {
Write-Host "$(if($true -eq $x64) {'x64'} else {'x86'}) Uninstaller unknown, trying the UninstallString from registry..."
$NativeUninstallString = Select-String "(\x22.*\x22) +(.*)" -inputobject $UninstallString
$Command = $NativeUninstallString.matches.groups[1].value
$Parameter = $NativeUninstallString.matches.groups[2].value
#All EXE x64 Installers (native defined uninstall)
Start-Process $Command -ArgumentList $Parameter -Wait
2024-03-19 06:59:05 +00:00
}
}
}
}
$app_was_found = $true
2024-09-02 14:10:21 +00:00
if (!$AllVersions) {
break
}
2024-03-19 06:59:05 +00:00
}
}
return $app_was_found;
2022-12-20 01:54:14 +00:00
}
2022-12-20 01:54:14 +00:00
function Remove-ModsLnk ($Lnk) {
2024-03-19 06:59:05 +00:00
foreach ($link in $Lnk) {
Remove-Item -Path "${env:Public}\Desktop\$link.lnk" -Force -ErrorAction SilentlyContinue | Out-Null
}
Return
2022-12-20 01:54:14 +00:00
}
2023-01-03 04:29:13 +00:00
function Add-ModsReg ($AddKey, $AddValue, $AddTypeData, $AddType) {
2024-03-19 06:59:05 +00:00
if ($AddKey -like "HKEY_LOCAL_MACHINE*") {
$AddKey = $AddKey.replace("HKEY_LOCAL_MACHINE", "HKLM:")
}
if (!(Test-Path "$AddKey")) {
New-Item $AddKey -Force -ErrorAction SilentlyContinue | Out-Null
}
New-ItemProperty $AddKey -Name $AddValue -Value $AddTypeData -PropertyType $AddType -Force | Out-Null
Return
}
function Remove-ModsReg ($DelKey, $DelValue) {
2024-03-19 06:59:05 +00:00
if ($DelKey -like "HKEY_LOCAL_MACHINE*") {
$DelKey = $DelKey.replace("HKEY_LOCAL_MACHINE", "HKLM:")
}
if (Test-Path "$DelKey") {
if (!$DelValue) {
Remove-Item $DelKey -Recurse -Force -ErrorAction SilentlyContinue | Out-Null
}
else {
Remove-ItemProperty $DelKey -Name $DelValue -Force -ErrorAction SilentlyContinue | Out-Null
}
}
Return
}
function Remove-ModsFile ($DelFile) {
2024-03-19 06:59:05 +00:00
foreach ($file in $DelFile) {
if (Test-Path "$file") {
Remove-Item -Path $file -Force -Recurse -ErrorAction SilentlyContinue | Out-Null
}
}
Return
}
2023-02-02 23:48:38 +00:00
function Rename-ModsFile ($RenFile, $NewName) {
2024-03-19 06:59:05 +00:00
if (Test-Path "$RenFile") {
Rename-Item -Path $RenFile -NewName $NewName -Force -ErrorAction SilentlyContinue | Out-Null
}
Return
2023-02-02 23:48:38 +00:00
}
function Copy-ModsFile ($CopyFile, $CopyTo) {
2024-03-19 06:59:05 +00:00
if (Test-Path "$CopyFile") {
Copy-Item -Path $CopyFile -Destination $CopyTo -Recurse -Force -ErrorAction SilentlyContinue | Out-Null
}
Return
}
2023-01-18 00:07:32 +00:00
function Edit-ModsFile ($File, $FindText, $ReplaceText) {
2024-03-19 06:59:05 +00:00
if (Test-Path "$File") {
((Get-Content -path $File -Raw) -replace "$FindText", "$ReplaceText") | Set-Content -Path $File -Force -ErrorAction SilentlyContinue | Out-Null
}
Return
2023-01-17 23:53:52 +00:00
}
function Grant-ModsPath ($GrantPath) {
2024-03-19 06:59:05 +00:00
foreach ($path in $GrantPath) {
if (Test-Path "$path") {
$NewAcl = Get-Acl -Path $path
$identity = New-Object System.Security.Principal.SecurityIdentifier S-1-5-11
if ((Get-Item $path) -is [System.IO.DirectoryInfo]) {
$fileSystemAccessRuleArgumentList = $identity, 'Modify', 'ContainerInherit, ObjectInherit', 'None', 'Allow'
}
else {
$fileSystemAccessRuleArgumentList = $identity, 'Modify', 'Allow'
}
$fileSystemAccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $fileSystemAccessRuleArgumentList
$NewAcl.SetAccessRule($fileSystemAccessRule)
2023-10-31 01:53:12 +00:00
2024-03-19 06:59:05 +00:00
# Grant delete permissions to subfolders and files
$inheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor [System.Security.AccessControl.InheritanceFlags]::ObjectInherit
$propagationFlag = [System.Security.AccessControl.PropagationFlags]::InheritOnly
$deleteAccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $identity, 'Delete', $inheritanceFlag, $propagationFlag, 'Allow'
$NewAcl.AddAccessRule($deleteAccessRule)
2023-10-31 01:53:12 +00:00
2024-03-19 06:59:05 +00:00
Set-Acl -Path $path -AclObject $NewAcl
}
}
Return
}