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

247 lines
11 KiB
PowerShell
Raw 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) {
2023-01-09 00:25:39 +00:00
if (Test-Path "$Run") {
2023-03-31 15:56:07 +00:00
if (!$RunSwitch) { $RunSwitch = " " }
if (!$User) {
if (!$RunWait) {
Start-Process $Run -ArgumentList $RunSwitch
}
else {
Start-Process $Run -ArgumentList $RunSwitch -Wait
}
}
else {
Start-Process explorer $Run
}
2023-01-09 00:25:39 +00:00
}
Return
}
2022-12-20 01:54:14 +00:00
function Stop-ModsProc ($Proc) {
2023-03-31 15:56:07 +00:00
foreach ($process in $Proc) {
2022-12-20 01:54:14 +00:00
Stop-Process -Name $process -Force -ErrorAction SilentlyContinue | Out-Null
}
Return
}
2023-01-09 00:25:39 +00:00
2022-12-20 01:54:14 +00:00
function Wait-ModsProc ($Wait) {
2023-03-31 15:56:07 +00:00
foreach ($process in $Wait) {
2022-12-20 01:54:14 +00:00
Get-Process $process -ErrorAction SilentlyContinue | Foreach-Object { $_.WaitForExit() }
}
Return
}
2023-01-09 00:25:39 +00:00
2023-02-02 23:48:38 +00:00
function Install-WingetID ($WingetIDInst) {
2023-03-31 15:56:07 +00:00
foreach ($app in $WingetIDInst) {
2023-09-18 21:15:56 +00:00
& $Winget install --id $app -e --accept-package-agreements --accept-source-agreements -s winget -h
2023-02-02 23:48:38 +00:00
}
Return
}
function Uninstall-WingetID ($WingetIDUninst) {
2023-03-31 15:56:07 +00:00
foreach ($app in $WingetIDUninst) {
2023-09-18 21:15:56 +00:00
& $Winget uninstall --id $app -e --accept-source-agreements -s winget -h
2023-02-02 23:48:38 +00:00
}
Return
}
function Uninstall-ModsApp ($AppUninst) {
2023-03-31 15:56:07 +00:00
foreach ($app in $AppUninst) {
$InstalledSoftware = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall"
2023-03-31 15:56:07 +00:00
foreach ($obj in $InstalledSoftware) {
2022-12-20 01:54:14 +00:00
if ($obj.GetValue('DisplayName') -like $App) {
$UninstallString = $obj.GetValue('UninstallString')
2023-01-01 00:58:43 +00:00
$CleanedUninstallString = $UninstallString.Trim([char]0x0022)
2022-12-20 01:54:14 +00:00
if ($UninstallString -like "MsiExec.exe*") {
$ProductCode = Select-String "{.*}" -inputobject $UninstallString
$ProductCode = $ProductCode.matches.groups[0].value
#MSI x64 Installer
2022-12-20 01:54:14 +00:00
$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)
2022-12-20 01:54:14 +00:00
Start-Process $Command -ArgumentList $Parameter -Wait
}
else {
2023-01-01 00:58:43 +00:00
if ((Test-Path $CleanedUninstallString)) {
$NullSoft = Select-String -Path $CleanedUninstallString -Pattern "Nullsoft"
2023-01-01 00:21:42 +00:00
}
2022-12-20 01:54:14 +00:00
if ($NullSoft) {
#NSIS x64 Installer
2022-12-20 01:54:14 +00:00
Start-Process $UninstallString -ArgumentList "/S" -Wait
}
else {
2023-01-01 00:58:43 +00:00
if ((Test-Path $CleanedUninstallString)) {
$Inno = Select-String -Path $CleanedUninstallString -Pattern "Inno Setup"
2023-01-01 00:21:42 +00:00
}
2022-12-20 01:54:14 +00:00
if ($Inno) {
#Inno x64 Installer
2022-12-20 01:54:14 +00:00
Start-Process $UninstallString -ArgumentList "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-" -Wait
}
else {
Write-Host "x64 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
2022-12-20 01:54:14 +00:00
}
}
}
}
$x64 = $true
2022-12-20 01:54:14 +00:00
break
}
}
if (!$x64) {
$InstalledSoftware = Get-ChildItem "HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
2023-03-31 15:56:07 +00:00
foreach ($obj in $InstalledSoftware) {
if ($obj.GetValue('DisplayName') -like $App) {
$UninstallString = $obj.GetValue('UninstallString')
$CleanedUninstallString = $UninstallString.Trim([char]0x0022)
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 {
if ((Test-Path $CleanedUninstallString)) {
$NullSoft = Select-String -Path $CleanedUninstallString -Pattern "Nullsoft"
}
if ($NullSoft) {
#NSIS x86 Installer
Start-Process $UninstallString -ArgumentList "/S" -Wait
}
else {
if ((Test-Path $CleanedUninstallString)) {
$Inno = Select-String -Path $CleanedUninstallString -Pattern "Inno Setup"
}
if ($Inno) {
#Inno x86 Installer
Start-Process $UninstallString -ArgumentList "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-" -Wait
}
else {
Write-Host "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 x86 Installers (native defined uninstall)
Start-Process $Command -ArgumentList $Parameter -Wait
}
}
}
}
break
}
}
}
2022-12-20 01:54:14 +00:00
}
Return
}
function Remove-ModsLnk ($Lnk) {
2023-03-31 15:56:07 +00:00
foreach ($link in $Lnk) {
2022-12-20 01:54:14 +00:00
Remove-Item -Path "${env:Public}\Desktop\$link.lnk" -Force -ErrorAction SilentlyContinue | Out-Null
}
Return
}
2023-01-03 04:29:13 +00:00
function Add-ModsReg ($AddKey, $AddValue, $AddTypeData, $AddType) {
2023-01-23 22:33:20 +00:00
if ($AddKey -like "HKEY_LOCAL_MACHINE*") {
2023-03-31 15:56:07 +00:00
$AddKey = $AddKey.replace("HKEY_LOCAL_MACHINE", "HKLM:")
2023-01-23 22:33:20 +00:00
}
if (!(Test-Path "$AddKey")) {
2023-01-03 05:16:19 +00:00
New-Item $AddKey -Force -ErrorAction SilentlyContinue | Out-Null
}
2023-01-03 05:16:19 +00:00
New-ItemProperty $AddKey -Name $AddValue -Value $AddTypeData -PropertyType $AddType -Force | Out-Null
Return
}
function Remove-ModsReg ($DelKey, $DelValue) {
2023-01-23 22:57:40 +00:00
if ($DelKey -like "HKEY_LOCAL_MACHINE*") {
2023-03-31 15:56:07 +00:00
$DelKey = $DelKey.replace("HKEY_LOCAL_MACHINE", "HKLM:")
2023-01-23 22:33:20 +00:00
}
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) {
2023-03-31 15:56:07 +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) {
if (Test-Path "$RenFile") {
Rename-Item -Path $RenFile -NewName $NewName -Force -ErrorAction SilentlyContinue | Out-Null
}
Return
}
function Copy-ModsFile ($CopyFile, $CopyTo) {
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) {
if (Test-Path "$File") {
2023-03-31 15:56:07 +00:00
((Get-Content -path $File -Raw) -replace "$FindText", "$ReplaceText") | Set-Content -Path $File -Force -ErrorAction SilentlyContinue | Out-Null
2023-01-17 23:53:52 +00:00
}
Return
}
function Grant-ModsPath ($GrantPath) {
2023-03-31 15:56:07 +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', 'InheritOnly', 'Allow'
}
else {
$fileSystemAccessRuleArgumentList = $identity, 'Modify', 'Allow'
}
$fileSystemAccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $fileSystemAccessRuleArgumentList
$NewAcl.SetAccessRule($fileSystemAccessRule)
Set-Acl -Path $path -AclObject $NewAcl
}
}
Return
}