mirror of
https://github.com/ChrisTitusTech/winutil.git
synced 2024-11-14 06:35:51 -06:00
44 lines
1.3 KiB
PowerShell
44 lines
1.3 KiB
PowerShell
|
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
|
||
|
}
|
||
|
}
|