2023-03-07 12:28:00 -08:00
function Invoke-WinUtilTweaks {
<#
2023-10-19 17:12:55 -05:00
. SYNOPSIS
Invokes the function associated with each provided checkbox
.PARAMETER CheckBox
The checkbox to invoke
.PARAMETER undo
Indicates whether to undo the operation contained in the checkbox
2024-03-30 11:46:29 -05:00
.PARAMETER KeepServiceStartup
Indicates whether to override the startup of a service with the one given from WinUtil,
or to keep the startup of said service, if it was changed by the user, or another program, from its default value.
2023-03-07 12:28:00 -08:00
#>
param (
$CheckBox ,
2024-03-30 11:46:29 -05:00
$undo = $false ,
$KeepServiceStartup = $true
2023-03-07 12:28:00 -08:00
)
2024-01-15 11:32:19 -06:00
Write-Debug "Tweaks: $( $CheckBox ) "
2024-08-06 23:35:17 +03:00
if ( $undo ) {
2023-03-07 12:28:00 -08:00
$Values = @ {
Registry = "OriginalValue"
ScheduledTask = "OriginalState"
Service = "OriginalType"
2023-07-13 15:46:00 -05:00
ScriptType = "UndoScript"
2023-03-07 12:28:00 -08:00
}
2023-07-13 15:46:00 -05:00
2024-08-06 23:35:17 +03:00
} else {
2023-03-07 12:28:00 -08:00
$Values = @ {
Registry = "Value"
ScheduledTask = "State"
Service = "StartupType"
2024-03-30 11:46:29 -05:00
OriginalService = "OriginalType"
2023-07-13 15:46:00 -05:00
ScriptType = "InvokeScript"
2023-03-07 12:28:00 -08:00
}
}
2024-08-06 23:35:17 +03:00
if ( $sync . configs . tweaks . $CheckBox . ScheduledTask ) {
2023-03-07 12:28:00 -08:00
$sync . configs . tweaks . $CheckBox . ScheduledTask | ForEach-Object {
2024-01-15 11:32:19 -06:00
Write-Debug " $( $psitem . Name ) and state is $( $psitem . $ ( $values . ScheduledTask )) "
2023-03-07 12:28:00 -08:00
Set-WinUtilScheduledTask -Name $psitem . Name -State $psitem . $ ( $values . ScheduledTask )
}
}
2024-08-06 23:35:17 +03:00
if ( $sync . configs . tweaks . $CheckBox . service ) {
2024-03-30 11:46:29 -05:00
Write-Debug "KeepServiceStartup is $KeepServiceStartup "
2023-03-07 12:28:00 -08:00
$sync . configs . tweaks . $CheckBox . service | ForEach-Object {
2024-03-30 11:46:29 -05:00
$changeservice = $true
2024-06-29 01:15:39 +03:00
2024-08-06 23:35:17 +03:00
# The check for !($undo) is required, without it the script will throw an error for accessing unavailable memeber, which's the 'OriginalService' Property
2024-03-30 11:46:29 -05:00
if ( $KeepServiceStartup -AND !( $undo )) {
try {
# Check if the service exists
$service = Get-Service -Name $psitem . Name -ErrorAction Stop
if (!( $service . StartType . ToString () -eq $psitem . $ ( $values . OriginalService ))) {
Write-Debug "Service $( $service . Name ) was changed in the past to $( $service . StartType . ToString ()) from it's original type of $( $psitem . $ ( $values . OriginalService )) , will not change it to $( $psitem . $ ( $values . service )) "
$changeservice = $false
}
2024-08-06 23:35:17 +03:00
} catch [ System.ServiceProcess.ServiceNotFoundException ] {
2024-03-30 11:46:29 -05:00
Write-Warning "Service $( $psitem . Name ) was not found"
}
}
if ( $changeservice ) {
Write-Debug " $( $psitem . Name ) and state is $( $psitem . $ ( $values . service )) "
Set-WinUtilService -Name $psitem . Name -StartupType $psitem . $ ( $values . Service )
}
2023-03-07 12:28:00 -08:00
}
}
2024-08-06 23:35:17 +03:00
if ( $sync . configs . tweaks . $CheckBox . registry ) {
2023-07-13 15:46:00 -05:00
$sync . configs . tweaks . $CheckBox . registry | ForEach-Object {
2024-01-15 11:32:19 -06:00
Write-Debug " $( $psitem . Name ) and state is $( $psitem . $ ( $values . registry )) "
2023-07-13 15:46:00 -05:00
Set-WinUtilRegistry -Name $psitem . Name -Path $psitem . Path -Type $psitem . Type -Value $psitem . $ ( $values . registry )
}
}
2024-08-06 23:35:17 +03:00
if ( $sync . configs . tweaks . $CheckBox . $ ( $values . ScriptType )) {
2023-07-13 15:46:00 -05:00
$sync . configs . tweaks . $CheckBox . $ ( $values . ScriptType ) | ForEach-Object {
2024-01-15 11:32:19 -06:00
Write-Debug " $( $psitem ) and state is $( $psitem . $ ( $values . ScriptType )) "
2023-07-13 15:46:00 -05:00
$Scriptblock = [ scriptblock ]:: Create ( $psitem )
Invoke-WinUtilScript -ScriptBlock $scriptblock -Name $CheckBox
}
}
2023-03-07 12:28:00 -08:00
2024-08-06 23:35:17 +03:00
if (! $undo ) {
if ( $sync . configs . tweaks . $CheckBox . appx ) {
2023-03-07 12:28:00 -08:00
$sync . configs . tweaks . $CheckBox . appx | ForEach-Object {
2024-01-15 11:32:19 -06:00
Write-Debug "UNDO $( $psitem . Name ) "
2023-03-07 12:28:00 -08:00
Remove-WinUtilAPPX -Name $psitem
}
}
2023-07-13 15:46:00 -05:00
2023-03-07 12:28:00 -08:00
}
2024-03-30 11:46:29 -05:00
}