From 979ab90d8c7a77d116925497ffd10b6fda40be98 Mon Sep 17 00:00:00 2001 From: "Mr.k" Date: Mon, 26 Aug 2024 08:19:57 +0300 Subject: [PATCH] Improved 'Invoke-WinUtilTweaks' Private Function - Remove 'Set-WinUtilService' Private Function as there's no need/use for it anymore (#8) --- functions/private/Invoke-WinUtilTweaks.ps1 | 39 +++++++++++++--------- functions/private/Set-WinUtilService.ps1 | 32 ------------------ 2 files changed, 24 insertions(+), 47 deletions(-) delete mode 100644 functions/private/Set-WinUtilService.ps1 diff --git a/functions/private/Invoke-WinUtilTweaks.ps1 b/functions/private/Invoke-WinUtilTweaks.ps1 index a77b64f4..66892474 100644 --- a/functions/private/Invoke-WinUtilTweaks.ps1 +++ b/functions/private/Invoke-WinUtilTweaks.ps1 @@ -48,25 +48,34 @@ function Invoke-WinUtilTweaks { if($sync.configs.tweaks.$CheckBox.service) { Write-Debug "KeepServiceStartup is $KeepServiceStartup" $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 - 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 { - write-host "Unable to get service $($psitem.Name)" + $servicename = $psitem.Name + try { + $service = Get-Service -Name "$servicename" -ErrorAction Stop + } catch [Microsoft.PowerShell.Commands.ServiceCommandException] { + Write-Debug "[Invoke-WinUtilTweaks] Service $servicename was not found" + } catch { + Write-Debug "[Invoke-WinUtilTweaks] Unable to validate $servicename due to unhandled exception" + Write-Debug "$($psitem.Exception.Message)" + } + + # 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) { - Write-Debug "$($psitem.Name) and state is $($psitem.$($values.service))" - Set-WinUtilService -Name $psitem.Name -StartupType $psitem.$($values.Service) + if($service -AND ($canchangeservice -OR $undo)) { + Write-Host "Setting Service $servicename to $($psitem.$($values.Service))" + Set-Service -InputObject $service -StartupType $psitem.$($values.Service) } } } diff --git a/functions/private/Set-WinUtilService.ps1 b/functions/private/Set-WinUtilService.ps1 deleted file mode 100644 index 59823f1b..00000000 --- a/functions/private/Set-WinUtilService.ps1 +++ /dev/null @@ -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)" - } -}