Last active
June 22, 2017 12:13
-
-
Save indented-automation/47d486dee18245387c9144bee892ac22 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Remove-Service { | |
[CmdletBinding(SupportsShouldProcess = $true, DefaultParameterSetName = 'ByName')] | |
param ( | |
[Parameter(Mandatory = $true, Position = 1, ParameterSetName = 'ByName')] | |
[String]$Name, | |
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'FromPipeline')] | |
[PSTypeName('Microsoft.Management.Infrastructure.CimInstance#root/cimv2/Win32_Service')] | |
[CimInstance]$InputObject, | |
[CimSession]$CimSession | |
) | |
begin { | |
$params = @{} | |
if ($CimSession) { | |
$params.CimSession = $CimSession | |
} | |
if ($pscmdlet.ParameterSetName -eq 'ByName') { | |
Get-CimInstance Win32_Service -Filter ('Name="{0}"' -f $Name) @params | Remove-Service @params | |
} | |
} | |
process { | |
if ($InputObject -and $pscmdlet.ShouldProcess(('Removing service {0}' -f $InputObject.Name))) { | |
if ($InputObject.State -ne 'Stopped') { | |
Write-Verbose ('Stopping {0}' -f $InputObject.Name) | |
$return = $InputObject | Invoke-CimMethod -MethodName StopService @params | |
} | |
# If the service is already stopped, or the attempt to stop the service succeeded, delete the service. | |
if ($InputObject.State -eq 'Stopped' -or $return.ReturnValue -eq 0) { | |
Write-Verbose ('Deleting {0}' -f $InputObject.Name) | |
$return = $InputObject | Invoke-CimMethod -MethodName Delete @params | |
} | |
if ($return.ReturnValue -ne 0) { | |
$message = switch ($return.ReturnValue) { | |
1 { 'The request is not supported' } | |
2 { 'The user did not have the necessary access' } | |
3 { 'The service cannot be stopped because other services that are running are dependent on it.' } | |
4 { 'The requested control code is not valid, or it is unacceptable to the service.' } | |
5 { 'The requested control code cannot be sent to the service because the state of the service is equal to Stopped, Starting, or Stopping.' } | |
6 { 'The service has not been started' } | |
7 { 'The service did not respond to the start request in a timely fashion' } | |
8 { 'Unknown failure when starting the service' } | |
9 { 'The directory path to the service executable file was not found' } | |
10 { 'The service is already running' } | |
11 { 'The database to add a new service is locked' } | |
12 { 'A dependency this service relies on has been removed from the system' } | |
13 { 'The service failed to find the service needed from a dependent service' } | |
14 { 'The service has been disabled from the system' } | |
15 { 'The service does not have the correct authentication to run on the system' } | |
16 { 'This service is being removed from the system' } | |
17 { 'The service has no execution thread' } | |
18 { 'The service has circular dependencies when it starts' } | |
19 { 'A service is running under the same name' } | |
20 { 'The service name has invalid characters' } | |
21 { 'Invalid parameters have been passed to the service' } | |
22 { 'The account under which this service runs is either invalid or lacks the permissions to run the service' } | |
23 { 'The service exists in the database of services available from the system' } | |
24 { 'The service is currently paused in the system' } | |
} | |
Write-Error -Message $message -Category InvalidOperation | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment