Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Simple Feature to keep the Service Startup upon Applying Service Tweaks, but not when Undoing it #1760

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions functions/private/Invoke-WinUtilTweaks.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ function Invoke-WinUtilTweaks {
.PARAMETER undo
Indicates whether to undo the operation contained in the checkbox

.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.
#>

param(
$CheckBox,
$undo = $false
$undo = $false,
$KeepServiceStartup = $true
)

Write-Debug "Tweaks: $($CheckBox)"
Expand All @@ -32,6 +36,7 @@ function Invoke-WinUtilTweaks {
Registry = "Value"
ScheduledTask = "State"
Service = "StartupType"
OriginalService = "OriginalType"
ScriptType = "InvokeScript"
}
}
Expand All @@ -42,9 +47,29 @@ function Invoke-WinUtilTweaks {
}
}
if($sync.configs.tweaks.$CheckBox.service){
Write-Debug "KeepServiceStartup is $KeepServiceStartup"
$sync.configs.tweaks.$CheckBox.service | ForEach-Object {
Write-Debug "$($psitem.Name) and state is $($psitem.$($values.service))"
Set-WinUtilService -Name $psitem.Name -StartupType $psitem.$($values.Service)
$changeservice = $true

# The check for !($undo) is required, without it the script will throw an error for accessing unavailable memeber, which's the 'OriginalService' Property
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
}
}
catch [System.ServiceProcess.ServiceNotFoundException] {
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)
}
}
}
if($sync.configs.tweaks.$CheckBox.registry){
Expand All @@ -70,4 +95,4 @@ function Invoke-WinUtilTweaks {
}

}
}
}