From f5613f6cf9e9bf8109fef7eb5ed020c43d47adc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20R=C3=BCegsegger?= Date: Mon, 18 Mar 2024 14:04:23 +0100 Subject: [PATCH 1/5] feat: add function to stop service --- .../mods/_AppID-template.ps1 | 36 +- .../mods/_Mods-Functions.ps1 | 406 +++++++++--------- 2 files changed, 227 insertions(+), 215 deletions(-) diff --git a/Sources/WAU/Winget-AutoUpdate/mods/_AppID-template.ps1 b/Sources/WAU/Winget-AutoUpdate/mods/_AppID-template.ps1 index 8ace279a..c086c42e 100644 --- a/Sources/WAU/Winget-AutoUpdate/mods/_AppID-template.ps1 +++ b/Sources/WAU/Winget-AutoUpdate/mods/_AppID-template.ps1 @@ -10,6 +10,9 @@ $RunWait = $True #Beginning of Process Name to Stop - optional wildcard (*) after, without .exe, multiple: "proc1","proc2" $Proc = @("") +#Beginning of Service Name to Stop - optional wildcard (*) after, without .exe, multiple: "proc1","proc2" +$Svc = @("") + #Beginning of Process Name to Wait for to End - optional wildcard (*) after, without .exe, multiple: "proc1","proc2" $Wait = @("") @@ -84,49 +87,52 @@ $User = $True <# MAIN #> if ($RunSystem) { - Invoke-ModsApp $RunSystem $RunSwitch $RunWait "" + Invoke-ModsApp $RunSystem $RunSwitch $RunWait "" } if ($Proc) { - Stop-ModsProc $Proc + Stop-ModsProc $Proc +} +if ($Svc) { + Stop-ModsProc $Svc } if ($Wait) { - Wait-ModsProc $Wait + Wait-ModsProc $Wait } if ($WingetIDInst) { - Install-WingetID $WingetIDInst + Install-WingetID $WingetIDInst } if ($WingetIDUninst) { - Uninstall-WingetID $WingetIDUninst + Uninstall-WingetID $WingetIDUninst } if ($AppUninst) { - Uninstall-ModsApp $AppUninst $AllVersions + Uninstall-ModsApp $AppUninst $AllVersions } if ($Lnk) { - Remove-ModsLnk $Lnk + Remove-ModsLnk $Lnk } if ($AddKey -and $AddValue -and $AddTypeData -and $AddType) { - Add-ModsReg $AddKey $AddValue $AddTypeData $AddType + Add-ModsReg $AddKey $AddValue $AddTypeData $AddType } if ($DelKey) { - Remove-ModsReg $DelKey $DelValue + Remove-ModsReg $DelKey $DelValue } if ($DelFile) { - Remove-ModsFile $DelFile + Remove-ModsFile $DelFile } if ($RenFile -and $NewName) { - Rename-ModsFile $RenFile $NewName + Rename-ModsFile $RenFile $NewName } if ($CopyFile -and $CopyTo) { - Copy-ModsFile $CopyFile $CopyTo + Copy-ModsFile $CopyFile $CopyTo } if ($File -and $FindText -and $ReplaceText) { - Edit-ModsFile $File $FindText $ReplaceText + Edit-ModsFile $File $FindText $ReplaceText } if ($GrantPath) { - Grant-ModsPath $GrantPath + Grant-ModsPath $GrantPath } if ($RunUser) { - Invoke-ModsApp $RunUser "" "" $User + Invoke-ModsApp $RunUser "" "" $User } <# EXTRAS #> diff --git a/Sources/WAU/Winget-AutoUpdate/mods/_Mods-Functions.ps1 b/Sources/WAU/Winget-AutoUpdate/mods/_Mods-Functions.ps1 index eab46812..7d874eed 100644 --- a/Sources/WAU/Winget-AutoUpdate/mods/_Mods-Functions.ps1 +++ b/Sources/WAU/Winget-AutoUpdate/mods/_Mods-Functions.ps1 @@ -1,258 +1,264 @@ #Common shared functions to handle the mods function Invoke-ModsApp ($Run, $RunSwitch, $RunWait, $User) { - 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 - } + if (Test-Path "$Run") { + if (!$RunSwitch) { $RunSwitch = " " } + if (!$User) { + if (!$RunWait) { + Start-Process $Run -ArgumentList $RunSwitch + } + else { + Start-Process $Run -ArgumentList $RunSwitch -Wait + } } - Return + else { + Start-Process explorer $Run + } + } + Return } function Stop-ModsProc ($Proc) { - foreach ($process in $Proc) { - Stop-Process -Name $process -Force -ErrorAction SilentlyContinue | Out-Null - } - Return + foreach ($process in $Proc) { + Stop-Process -Name $process -Force -ErrorAction SilentlyContinue | Out-Null + } + Return +} +function Stop-ModsSvc ($Svc) { + foreach ($service in $Svc) { + Stop-Service -Name $service -Force -ErrorAction SilentlyContinue | Out-Null + } + Return } function Wait-ModsProc ($Wait) { - foreach ($process in $Wait) { - Get-Process $process -ErrorAction SilentlyContinue | Foreach-Object { $_.WaitForExit() } - } - Return + foreach ($process in $Wait) { + Get-Process $process -ErrorAction SilentlyContinue | Foreach-Object { $_.WaitForExit() } + } + Return } function Install-WingetID ($WingetIDInst) { - foreach ($app in $WingetIDInst) { - & $Winget install --id $app -e --accept-package-agreements --accept-source-agreements -s winget -h - } - Return + foreach ($app in $WingetIDInst) { + & $Winget install --id $app -e --accept-package-agreements --accept-source-agreements -s winget -h + } + Return } function Uninstall-WingetID ($WingetIDUninst) { - foreach ($app in $WingetIDUninst) { - & $Winget uninstall --id $app -e --accept-source-agreements -s winget -h - } - Return + foreach ($app in $WingetIDUninst) { + & $Winget uninstall --id $app -e --accept-source-agreements -s winget -h + } + Return } function Uninstall-ModsApp ($AppUninst, $AllVersions) { - foreach ($app in $AppUninst) { - $InstalledSoftware = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall" - 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 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 { - if ((Test-Path $CleanedUninstallString)) { - $NullSoft = Select-String -Path $CleanedUninstallString -Pattern "Nullsoft" - } - if ($NullSoft) { - #NSIS x64 Installer - Start-Process $UninstallString -ArgumentList "/S" -Wait - } - else { - if ((Test-Path $CleanedUninstallString)) { - $Inno = Select-String -Path $CleanedUninstallString -Pattern "Inno Setup" - } - if ($Inno) { - #Inno x64 Installer - 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 - } - } - } - } - $x64 = $true - if (!$AllVersions) { - break - } + foreach ($app in $AppUninst) { + $InstalledSoftware = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall" + 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 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 { + if ((Test-Path $CleanedUninstallString)) { + $NullSoft = Select-String -Path $CleanedUninstallString -Pattern "Nullsoft" + } + if ($NullSoft) { + #NSIS x64 Installer + Start-Process $UninstallString -ArgumentList "/S" -Wait } + else { + if ((Test-Path $CleanedUninstallString)) { + $Inno = Select-String -Path $CleanedUninstallString -Pattern "Inno Setup" + } + if ($Inno) { + #Inno x64 Installer + 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 + } + } + } + } + $x64 = $true + if (!$AllVersions) { + 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') - $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 - } - } - } - } - if (!$AllVersions) { - 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') + $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 + } + } } + } + if (!$AllVersions) { + break + } } + } } - Return + } + Return } function Remove-ModsLnk ($Lnk) { - foreach ($link in $Lnk) { - Remove-Item -Path "${env:Public}\Desktop\$link.lnk" -Force -ErrorAction SilentlyContinue | Out-Null - } - Return + foreach ($link in $Lnk) { + Remove-Item -Path "${env:Public}\Desktop\$link.lnk" -Force -ErrorAction SilentlyContinue | Out-Null + } + Return } function Add-ModsReg ($AddKey, $AddValue, $AddTypeData, $AddType) { - 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 + 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) { - if ($DelKey -like "HKEY_LOCAL_MACHINE*") { - $DelKey = $DelKey.replace("HKEY_LOCAL_MACHINE", "HKLM:") + 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 } - 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 - } + else { + Remove-ItemProperty $DelKey -Name $DelValue -Force -ErrorAction SilentlyContinue | Out-Null } - Return + } + Return } function Remove-ModsFile ($DelFile) { - foreach ($file in $DelFile) { - if (Test-Path "$file") { - Remove-Item -Path $file -Force -Recurse -ErrorAction SilentlyContinue | Out-Null - } + foreach ($file in $DelFile) { + if (Test-Path "$file") { + Remove-Item -Path $file -Force -Recurse -ErrorAction SilentlyContinue | Out-Null } - Return + } + Return } function Rename-ModsFile ($RenFile, $NewName) { - if (Test-Path "$RenFile") { - Rename-Item -Path $RenFile -NewName $NewName -Force -ErrorAction SilentlyContinue | Out-Null - } - Return + 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 + if (Test-Path "$CopyFile") { + Copy-Item -Path $CopyFile -Destination $CopyTo -Recurse -Force -ErrorAction SilentlyContinue | Out-Null + } + Return } function Edit-ModsFile ($File, $FindText, $ReplaceText) { - if (Test-Path "$File") { + if (Test-Path "$File") { ((Get-Content -path $File -Raw) -replace "$FindText", "$ReplaceText") | Set-Content -Path $File -Force -ErrorAction SilentlyContinue | Out-Null - } - Return + } + Return } function Grant-ModsPath ($GrantPath) { - 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) + 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) - # 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) + # 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) - Set-Acl -Path $path -AclObject $NewAcl - } + Set-Acl -Path $path -AclObject $NewAcl } - Return + } + Return } From 8bf26f912b92015a8af4f7e09e9e5848566816ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20R=C3=BCegsegger?= Date: Mon, 18 Mar 2024 14:11:03 +0100 Subject: [PATCH 2/5] fix: updated template --- Sources/WAU/Winget-AutoUpdate/mods/_AppID-template.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/WAU/Winget-AutoUpdate/mods/_AppID-template.ps1 b/Sources/WAU/Winget-AutoUpdate/mods/_AppID-template.ps1 index c086c42e..d62f73cf 100644 --- a/Sources/WAU/Winget-AutoUpdate/mods/_AppID-template.ps1 +++ b/Sources/WAU/Winget-AutoUpdate/mods/_AppID-template.ps1 @@ -10,7 +10,7 @@ $RunWait = $True #Beginning of Process Name to Stop - optional wildcard (*) after, without .exe, multiple: "proc1","proc2" $Proc = @("") -#Beginning of Service Name to Stop - optional wildcard (*) after, without .exe, multiple: "proc1","proc2" +#Beginning of Service Name to Stop - multiple: "service1.exe","service2.exe" $Svc = @("") #Beginning of Process Name to Wait for to End - optional wildcard (*) after, without .exe, multiple: "proc1","proc2" From 093a712e8099e4e60ddd82d3d602a45379e50510 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20R=C3=BCegsegger?= Date: Mon, 18 Mar 2024 14:22:12 +0100 Subject: [PATCH 3/5] fix: update Stop-ModsSvc in template --- Sources/WAU/Winget-AutoUpdate/mods/_AppID-template.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/WAU/Winget-AutoUpdate/mods/_AppID-template.ps1 b/Sources/WAU/Winget-AutoUpdate/mods/_AppID-template.ps1 index d62f73cf..c20c0df3 100644 --- a/Sources/WAU/Winget-AutoUpdate/mods/_AppID-template.ps1 +++ b/Sources/WAU/Winget-AutoUpdate/mods/_AppID-template.ps1 @@ -93,7 +93,7 @@ if ($Proc) { Stop-ModsProc $Proc } if ($Svc) { - Stop-ModsProc $Svc + Stop-ModsSvc $Svc } if ($Wait) { Wait-ModsProc $Wait From ea0113744a2d8084e7679fc73314a8895e8ac02d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20R=C3=BCegsegger?= Date: Tue, 19 Mar 2024 07:51:16 +0100 Subject: [PATCH 4/5] fix: indents --- .../mods/_AppID-template.ps1 | 32 +- .../mods/_Mods-Functions.ps1 | 308 +++++++++--------- 2 files changed, 170 insertions(+), 170 deletions(-) diff --git a/Sources/WAU/Winget-AutoUpdate/mods/_AppID-template.ps1 b/Sources/WAU/Winget-AutoUpdate/mods/_AppID-template.ps1 index c20c0df3..85b09b70 100644 --- a/Sources/WAU/Winget-AutoUpdate/mods/_AppID-template.ps1 +++ b/Sources/WAU/Winget-AutoUpdate/mods/_AppID-template.ps1 @@ -87,52 +87,52 @@ $User = $True <# MAIN #> if ($RunSystem) { - Invoke-ModsApp $RunSystem $RunSwitch $RunWait "" + Invoke-ModsApp $RunSystem $RunSwitch $RunWait "" } if ($Proc) { - Stop-ModsProc $Proc + Stop-ModsProc $Proc } if ($Svc) { Stop-ModsSvc $Svc } if ($Wait) { - Wait-ModsProc $Wait + Wait-ModsProc $Wait } if ($WingetIDInst) { - Install-WingetID $WingetIDInst + Install-WingetID $WingetIDInst } if ($WingetIDUninst) { - Uninstall-WingetID $WingetIDUninst + Uninstall-WingetID $WingetIDUninst } if ($AppUninst) { - Uninstall-ModsApp $AppUninst $AllVersions + Uninstall-ModsApp $AppUninst $AllVersions } if ($Lnk) { - Remove-ModsLnk $Lnk + Remove-ModsLnk $Lnk } if ($AddKey -and $AddValue -and $AddTypeData -and $AddType) { - Add-ModsReg $AddKey $AddValue $AddTypeData $AddType + Add-ModsReg $AddKey $AddValue $AddTypeData $AddType } if ($DelKey) { - Remove-ModsReg $DelKey $DelValue + Remove-ModsReg $DelKey $DelValue } if ($DelFile) { - Remove-ModsFile $DelFile + Remove-ModsFile $DelFile } if ($RenFile -and $NewName) { - Rename-ModsFile $RenFile $NewName + Rename-ModsFile $RenFile $NewName } if ($CopyFile -and $CopyTo) { - Copy-ModsFile $CopyFile $CopyTo + Copy-ModsFile $CopyFile $CopyTo } if ($File -and $FindText -and $ReplaceText) { - Edit-ModsFile $File $FindText $ReplaceText + Edit-ModsFile $File $FindText $ReplaceText } if ($GrantPath) { - Grant-ModsPath $GrantPath + Grant-ModsPath $GrantPath } if ($RunUser) { - Invoke-ModsApp $RunUser "" "" $User + Invoke-ModsApp $RunUser "" "" $User } -<# EXTRAS #> +<# EXTRAS #> \ No newline at end of file diff --git a/Sources/WAU/Winget-AutoUpdate/mods/_Mods-Functions.ps1 b/Sources/WAU/Winget-AutoUpdate/mods/_Mods-Functions.ps1 index 7d874eed..a7edd0b1 100644 --- a/Sources/WAU/Winget-AutoUpdate/mods/_Mods-Functions.ps1 +++ b/Sources/WAU/Winget-AutoUpdate/mods/_Mods-Functions.ps1 @@ -2,18 +2,18 @@ function Invoke-ModsApp ($Run, $RunSwitch, $RunWait, $User) { if (Test-Path "$Run") { - if (!$RunSwitch) { $RunSwitch = " " } - if (!$User) { - if (!$RunWait) { - Start-Process $Run -ArgumentList $RunSwitch + if (!$RunSwitch) { $RunSwitch = " " } + if (!$User) { + if (!$RunWait) { + Start-Process $Run -ArgumentList $RunSwitch + } + else { + Start-Process $Run -ArgumentList $RunSwitch -Wait + } } else { - Start-Process $Run -ArgumentList $RunSwitch -Wait + Start-Process explorer $Run } - } - else { - Start-Process explorer $Run - } } Return } @@ -21,7 +21,7 @@ function Invoke-ModsApp ($Run, $RunSwitch, $RunWait, $User) { function Stop-ModsProc ($Proc) { foreach ($process in $Proc) { - Stop-Process -Name $process -Force -ErrorAction SilentlyContinue | Out-Null + Stop-Process -Name $process -Force -ErrorAction SilentlyContinue | Out-Null } Return } @@ -34,158 +34,158 @@ function Stop-ModsSvc ($Svc) { function Wait-ModsProc ($Wait) { foreach ($process in $Wait) { - Get-Process $process -ErrorAction SilentlyContinue | Foreach-Object { $_.WaitForExit() } + Get-Process $process -ErrorAction SilentlyContinue | Foreach-Object { $_.WaitForExit() } } Return } function Install-WingetID ($WingetIDInst) { foreach ($app in $WingetIDInst) { - & $Winget install --id $app -e --accept-package-agreements --accept-source-agreements -s winget -h + & $Winget install --id $app -e --accept-package-agreements --accept-source-agreements -s winget -h } Return } function Uninstall-WingetID ($WingetIDUninst) { foreach ($app in $WingetIDUninst) { - & $Winget uninstall --id $app -e --accept-source-agreements -s winget -h + & $Winget uninstall --id $app -e --accept-source-agreements -s winget -h } Return } function Uninstall-ModsApp ($AppUninst, $AllVersions) { foreach ($app in $AppUninst) { - $InstalledSoftware = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall" - 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 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 { - if ((Test-Path $CleanedUninstallString)) { - $NullSoft = Select-String -Path $CleanedUninstallString -Pattern "Nullsoft" - } - if ($NullSoft) { - #NSIS x64 Installer - Start-Process $UninstallString -ArgumentList "/S" -Wait - } - else { - if ((Test-Path $CleanedUninstallString)) { - $Inno = Select-String -Path $CleanedUninstallString -Pattern "Inno Setup" - } - if ($Inno) { - #Inno x64 Installer - Start-Process $UninstallString -ArgumentList "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-" -Wait + $InstalledSoftware = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall" + 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 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 { - 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 + $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 { + if ((Test-Path $CleanedUninstallString)) { + $NullSoft = Select-String -Path $CleanedUninstallString -Pattern "Nullsoft" + } + if ($NullSoft) { + #NSIS x64 Installer + Start-Process $UninstallString -ArgumentList "/S" -Wait + } + else { + if ((Test-Path $CleanedUninstallString)) { + $Inno = Select-String -Path $CleanedUninstallString -Pattern "Inno Setup" + } + if ($Inno) { + #Inno x64 Installer + 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 + } + } + } + } + $x64 = $true + if (!$AllVersions) { + break } - } } - } - $x64 = $true - if (!$AllVersions) { - 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') - $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 + 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') + $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 + } + } + } + } + if (!$AllVersions) { + break + } } - 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 - } - } - } - } - if (!$AllVersions) { - break } - } } - } } Return } function Remove-ModsLnk ($Lnk) { foreach ($link in $Lnk) { - Remove-Item -Path "${env:Public}\Desktop\$link.lnk" -Force -ErrorAction SilentlyContinue | Out-Null + Remove-Item -Path "${env:Public}\Desktop\$link.lnk" -Force -ErrorAction SilentlyContinue | Out-Null } Return } function Add-ModsReg ($AddKey, $AddValue, $AddTypeData, $AddType) { if ($AddKey -like "HKEY_LOCAL_MACHINE*") { - $AddKey = $AddKey.replace("HKEY_LOCAL_MACHINE", "HKLM:") + $AddKey = $AddKey.replace("HKEY_LOCAL_MACHINE", "HKLM:") } if (!(Test-Path "$AddKey")) { - New-Item $AddKey -Force -ErrorAction SilentlyContinue | Out-Null + New-Item $AddKey -Force -ErrorAction SilentlyContinue | Out-Null } New-ItemProperty $AddKey -Name $AddValue -Value $AddTypeData -PropertyType $AddType -Force | Out-Null Return @@ -193,72 +193,72 @@ function Add-ModsReg ($AddKey, $AddValue, $AddTypeData, $AddType) { function Remove-ModsReg ($DelKey, $DelValue) { if ($DelKey -like "HKEY_LOCAL_MACHINE*") { - $DelKey = $DelKey.replace("HKEY_LOCAL_MACHINE", "HKLM:") + $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 - } + 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) { foreach ($file in $DelFile) { - if (Test-Path "$file") { - Remove-Item -Path $file -Force -Recurse -ErrorAction SilentlyContinue | Out-Null - } + if (Test-Path "$file") { + Remove-Item -Path $file -Force -Recurse -ErrorAction SilentlyContinue | Out-Null + } } Return } function Rename-ModsFile ($RenFile, $NewName) { if (Test-Path "$RenFile") { - Rename-Item -Path $RenFile -NewName $NewName -Force -ErrorAction SilentlyContinue | Out-Null + 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 + Copy-Item -Path $CopyFile -Destination $CopyTo -Recurse -Force -ErrorAction SilentlyContinue | Out-Null } Return } function Edit-ModsFile ($File, $FindText, $ReplaceText) { if (Test-Path "$File") { - ((Get-Content -path $File -Raw) -replace "$FindText", "$ReplaceText") | Set-Content -Path $File -Force -ErrorAction SilentlyContinue | Out-Null + ((Get-Content -path $File -Raw) -replace "$FindText", "$ReplaceText") | Set-Content -Path $File -Force -ErrorAction SilentlyContinue | Out-Null } Return } function Grant-ModsPath ($GrantPath) { 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) + 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) - # 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) + # 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) - Set-Acl -Path $path -AclObject $NewAcl - } + Set-Acl -Path $path -AclObject $NewAcl + } } Return } From 5176f6c3bc8b7c57aebb1a5dbf8b5f5d53435ac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20R=C3=BCegsegger?= Date: Tue, 19 Mar 2024 07:59:05 +0100 Subject: [PATCH 5/5] fix: indents --- .../mods/_Mods-Functions.ps1 | 424 +++++++++--------- 1 file changed, 212 insertions(+), 212 deletions(-) diff --git a/Sources/WAU/Winget-AutoUpdate/mods/_Mods-Functions.ps1 b/Sources/WAU/Winget-AutoUpdate/mods/_Mods-Functions.ps1 index a7edd0b1..45288488 100644 --- a/Sources/WAU/Winget-AutoUpdate/mods/_Mods-Functions.ps1 +++ b/Sources/WAU/Winget-AutoUpdate/mods/_Mods-Functions.ps1 @@ -1,29 +1,29 @@ #Common shared functions to handle the mods function Invoke-ModsApp ($Run, $RunSwitch, $RunWait, $User) { - 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 + 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 } function Stop-ModsProc ($Proc) { - foreach ($process in $Proc) { - Stop-Process -Name $process -Force -ErrorAction SilentlyContinue | Out-Null - } - Return + foreach ($process in $Proc) { + Stop-Process -Name $process -Force -ErrorAction SilentlyContinue | Out-Null + } + Return } function Stop-ModsSvc ($Svc) { foreach ($service in $Svc) { @@ -33,232 +33,232 @@ function Stop-ModsSvc ($Svc) { } function Wait-ModsProc ($Wait) { - foreach ($process in $Wait) { - Get-Process $process -ErrorAction SilentlyContinue | Foreach-Object { $_.WaitForExit() } - } - Return + foreach ($process in $Wait) { + Get-Process $process -ErrorAction SilentlyContinue | Foreach-Object { $_.WaitForExit() } + } + Return } function Install-WingetID ($WingetIDInst) { - foreach ($app in $WingetIDInst) { - & $Winget install --id $app -e --accept-package-agreements --accept-source-agreements -s winget -h - } - Return + foreach ($app in $WingetIDInst) { + & $Winget install --id $app -e --accept-package-agreements --accept-source-agreements -s winget -h + } + Return } function Uninstall-WingetID ($WingetIDUninst) { - foreach ($app in $WingetIDUninst) { - & $Winget uninstall --id $app -e --accept-source-agreements -s winget -h - } - Return + foreach ($app in $WingetIDUninst) { + & $Winget uninstall --id $app -e --accept-source-agreements -s winget -h + } + Return } function Uninstall-ModsApp ($AppUninst, $AllVersions) { - foreach ($app in $AppUninst) { - $InstalledSoftware = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall" - 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 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 { - if ((Test-Path $CleanedUninstallString)) { - $NullSoft = Select-String -Path $CleanedUninstallString -Pattern "Nullsoft" - } - if ($NullSoft) { - #NSIS x64 Installer - Start-Process $UninstallString -ArgumentList "/S" -Wait - } - else { - if ((Test-Path $CleanedUninstallString)) { - $Inno = Select-String -Path $CleanedUninstallString -Pattern "Inno Setup" - } - if ($Inno) { - #Inno x64 Installer - 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 - } - } - } - } - $x64 = $true - if (!$AllVersions) { - 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') - $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 - } - } - } - } - if (!$AllVersions) { - break - } - } - } - } - } - Return + foreach ($app in $AppUninst) { + $InstalledSoftware = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall" + 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 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 { + if ((Test-Path $CleanedUninstallString)) { + $NullSoft = Select-String -Path $CleanedUninstallString -Pattern "Nullsoft" + } + if ($NullSoft) { + #NSIS x64 Installer + Start-Process $UninstallString -ArgumentList "/S" -Wait + } + else { + if ((Test-Path $CleanedUninstallString)) { + $Inno = Select-String -Path $CleanedUninstallString -Pattern "Inno Setup" + } + if ($Inno) { + #Inno x64 Installer + 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 + } + } + } + } + $x64 = $true + if (!$AllVersions) { + 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') + $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 + } + } + } + } + if (!$AllVersions) { + break + } + } + } + } + } + Return } function Remove-ModsLnk ($Lnk) { - foreach ($link in $Lnk) { - Remove-Item -Path "${env:Public}\Desktop\$link.lnk" -Force -ErrorAction SilentlyContinue | Out-Null - } - Return + foreach ($link in $Lnk) { + Remove-Item -Path "${env:Public}\Desktop\$link.lnk" -Force -ErrorAction SilentlyContinue | Out-Null + } + Return } function Add-ModsReg ($AddKey, $AddValue, $AddTypeData, $AddType) { - 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 + 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) { - 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 + 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) { - foreach ($file in $DelFile) { - if (Test-Path "$file") { - Remove-Item -Path $file -Force -Recurse -ErrorAction SilentlyContinue | Out-Null - } - } - Return + foreach ($file in $DelFile) { + if (Test-Path "$file") { + Remove-Item -Path $file -Force -Recurse -ErrorAction SilentlyContinue | Out-Null + } + } + Return } function Rename-ModsFile ($RenFile, $NewName) { - if (Test-Path "$RenFile") { - Rename-Item -Path $RenFile -NewName $NewName -Force -ErrorAction SilentlyContinue | Out-Null - } - Return + 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 + if (Test-Path "$CopyFile") { + Copy-Item -Path $CopyFile -Destination $CopyTo -Recurse -Force -ErrorAction SilentlyContinue | Out-Null + } + Return } function Edit-ModsFile ($File, $FindText, $ReplaceText) { - if (Test-Path "$File") { - ((Get-Content -path $File -Raw) -replace "$FindText", "$ReplaceText") | Set-Content -Path $File -Force -ErrorAction SilentlyContinue | Out-Null - } - Return + if (Test-Path "$File") { + ((Get-Content -path $File -Raw) -replace "$FindText", "$ReplaceText") | Set-Content -Path $File -Force -ErrorAction SilentlyContinue | Out-Null + } + Return } function Grant-ModsPath ($GrantPath) { - 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) + 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) - # 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) + # 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) - Set-Acl -Path $path -AclObject $NewAcl - } - } - Return + Set-Acl -Path $path -AclObject $NewAcl + } + } + Return }