Files
winutil/functions/private/Set-WinUtilService.ps1
T

34 lines
895 B
PowerShell
Raw Normal View History

+3
2023-03-07 12:28:00 -08:00
Function Set-WinUtilService {
<#
.DESCRIPTION
This function will change the startup type of services and start/stop them as needed
.EXAMPLE
Set-WinUtilService -Name "HomeGroupListener" -StartupType "Manual"
#>
param (
$Name,
$StartupType
)
2023-07-15 10:42:11 -05:00
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
+3
2023-03-07 12:28:00 -08:00
}
2023-07-15 10:42:11 -05:00
catch [System.ServiceProcess.ServiceNotFoundException] {
Write-Warning "Service $Name was not found"
+3
2023-03-07 12:28:00 -08:00
}
2023-07-15 10:42:11 -05:00
catch {
+3
2023-03-07 12:28:00 -08:00
Write-Warning "Unable to set $Name due to unhandled exception"
2023-07-15 10:42:11 -05:00
Write-Warning $_.Exception.Message
+3
2023-03-07 12:28:00 -08:00
}
2023-07-15 10:42:11 -05:00
+3
2023-03-07 12:28:00 -08:00
}