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

44 lines
1.3 KiB
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
)
Try{
Write-Host "Setting Services $Name to $StartupType"
Set-Service -Name $Name -StartupType $StartupType -ErrorAction Stop
if($StartupType -eq "Disabled"){
Write-Host "Stopping $Name"
Stop-Service -Name $Name -Force -ErrorAction Stop
}
if($StartupType -eq "Enabled"){
Write-Host "Starting $Name"
Start-Service -Name $Name -Force -ErrorAction Stop
}
}
Catch [System.Exception]{
if($psitem.Exception.Message -like "*Cannot find any service with service name*" -or
$psitem.Exception.Message -like "*was not found on computer*"){
Write-Warning "Service $name was not Found"
}
Else{
Write-Warning "Unable to set $Name due to unhandled exception"
Write-Warning $psitem.Exception.Message
}
}
Catch{
Write-Warning "Unable to set $Name due to unhandled exception"
Write-Warning $psitem.Exception.StackTrace
}
}