38 lines
1.1 KiB
PowerShell
38 lines
1.1 KiB
PowerShell
|
function Disable-Service {
|
||
|
<#
|
||
|
.SYNOPSIS
|
||
|
Sets a Service to Disabled Start Type
|
||
|
|
||
|
.NOTES
|
||
|
Service must be stopped separately
|
||
|
#>
|
||
|
|
||
|
[CmdletBinding()]
|
||
|
param(
|
||
|
[Alias("ServiceObject")]
|
||
|
[Parameter(Mandatory=$true)]
|
||
|
[object]$service
|
||
|
)
|
||
|
|
||
|
$logLead = (Get-LogLeadName);
|
||
|
$service.Refresh()
|
||
|
|
||
|
if ($service.StartType -eq "Disabled")
|
||
|
{
|
||
|
Write-Host ("$logLead : Service '{0}' is already disabled, no action necessary" -f $service.Name)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
Write-Host ("$logLead : Preparing to disable service '{0}'." -f $service.Name)
|
||
|
Write-Verbose ("$logLead : Status before changes:{0}" -f ($service | Select-Object Name, StartType, Status))
|
||
|
|
||
|
Set-Service -Name $service.Name -StartupType Disabled
|
||
|
$service = Get-Service $service.Name
|
||
|
|
||
|
if ($service.StartType -ne "Disabled") {
|
||
|
Write-Warning ("$logLead : Unable to disable the '{0}' Service" -f $service.Name)
|
||
|
} else {
|
||
|
Write-Host ("$logLead : Service disabled. Status after changes: {0}" -f ($service | Select-Object Name, StartType, Status))
|
||
|
}
|
||
|
}
|