Improved 'Invoke-WinUtilTweaks' Private Function - Remove 'Set-WinUtilService' Private Function as there's no need/use for it anymore (#8)

This commit is contained in:
Mr.k 2024-08-26 08:19:57 +03:00 committed by GitHub
parent 8b158df8c4
commit 979ab90d8c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 47 deletions

View File

@ -48,25 +48,34 @@ function Invoke-WinUtilTweaks {
if($sync.configs.tweaks.$CheckBox.service) { if($sync.configs.tweaks.$CheckBox.service) {
Write-Debug "KeepServiceStartup is $KeepServiceStartup" Write-Debug "KeepServiceStartup is $KeepServiceStartup"
$sync.configs.tweaks.$CheckBox.service | ForEach-Object { $sync.configs.tweaks.$CheckBox.service | ForEach-Object {
$changeservice = $true # Reset variable on each iteraction
$canchangeservice = $false
$service = $null
# The check for !($undo) is required, without it the script will throw an error for accessing unavailable memeber, which's the 'OriginalService' Property $servicename = $psitem.Name
if($KeepServiceStartup -AND !($undo)) { try {
try { $service = Get-Service -Name "$servicename" -ErrorAction Stop
# Check if the service exists } catch [Microsoft.PowerShell.Commands.ServiceCommandException] {
$service = Get-Service -Name $psitem.Name -ErrorAction Stop Write-Debug "[Invoke-WinUtilTweaks] Service $servicename was not found"
if(!($service.StartType.ToString() -eq $psitem.$($values.OriginalService))) { } catch {
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))" Write-Debug "[Invoke-WinUtilTweaks] Unable to validate $servicename due to unhandled exception"
$changeservice = $false Write-Debug "$($psitem.Exception.Message)"
} }
} catch {
write-host "Unable to get service $($psitem.Name)" # Note:
# The check for !($undo) is required, without it the script will throw an error for accessing unavailable memeber,
# which's the 'OriginalService' Property
if($service -AND $KeepServiceStartup -AND !$undo) {
if($service.StartType.ToString() -ne $psitem.$($values.OriginalService)) {
Write-Debug "Service $servicename 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))"
} else {
$canchangeservice = $true
} }
} }
if($changeservice) { if($service -AND ($canchangeservice -OR $undo)) {
Write-Debug "$($psitem.Name) and state is $($psitem.$($values.service))" Write-Host "Setting Service $servicename to $($psitem.$($values.Service))"
Set-WinUtilService -Name $psitem.Name -StartupType $psitem.$($values.Service) Set-Service -InputObject $service -StartupType $psitem.$($values.Service)
} }
} }
} }

View File

@ -1,32 +0,0 @@
Function Set-WinUtilService {
<#
.SYNOPSIS
Changes the startup type of the given service
.PARAMETER Name
The name of the service to modify
.PARAMETER StartupType
The startup type to set the service to
.EXAMPLE
Set-WinUtilService -Name "HomeGroupListener" -StartupType "Manual"
#>
param (
$Name,
$StartupType
)
try {
Write-Host "Setting Service $Name to $StartupType"
# Check if the service exists
$service = Get-Service -Name $Name -ErrorAction Stop
# Service exists, proceed with changing properties
$service | Set-Service -StartupType $StartupType -ErrorAction Stop
} catch {
write-host "Unable to get service $($Name)"
}
}