mirror of
https://github.com/ChrisTitusTech/winutil.git
synced 2025-01-15 09:20:36 -06:00
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:
parent
8b158df8c4
commit
979ab90d8c
@ -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)) {
|
||||
$servicename = $psitem.Name
|
||||
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
|
||||
}
|
||||
$service = Get-Service -Name "$servicename" -ErrorAction Stop
|
||||
} catch [Microsoft.PowerShell.Commands.ServiceCommandException] {
|
||||
Write-Debug "[Invoke-WinUtilTweaks] Service $servicename was not found"
|
||||
} catch {
|
||||
write-host "Unable to get service $($psitem.Name)"
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user