Created
May 11, 2015 09:11
-
-
Save aivascu/e852b891f46aa3b085ed to your computer and use it in GitHub Desktop.
Uninstalls a windows service on a local/remote machine
This file contains 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
param( | |
[Parameter(Mandatory = $true, Position = 0)] | |
[string]$ServiceName, | |
[Parameter(Mandatory = $false, Position = 1)] | |
[string]$DeploymentServer | |
) | |
function Get-RemoteService( | |
[string]$serviceName = $(throw "serviceName is required"), | |
[string]$targetServer = $(throw "targetServer is required")) | |
{ | |
$svc = Get-WmiObject -Namespace "root\cimv2" -Class "Win32_Service" -ComputerName $targetServer -Filter "Name='$serviceName'" -Impersonation 3 | |
return $svc | |
} | |
function Uninstall-RemoteService( | |
[string]$serviceName = $(throw "serviceName is required"), | |
[string]$targetServer = $(throw "targetServer is required")) | |
{ | |
$service = Get-RemoteService $serviceName $targetServer | |
if (!($service)) | |
{ | |
Write-Warning "Failed to find service $serviceName on $targetServer. Nothing to uninstall." | |
return | |
} | |
"Found service $serviceName on $targetServer; checking status" | |
if ($service.Started) | |
{ | |
"Stopping service $serviceName on $targetServer" | |
#could also use Set-Service, net stop, SC, psservice, psexec etc. | |
$result = $service.StopService() | |
Test-ServiceResult -operation "Stop service $serviceName on $targetServer" -result $result | |
} | |
"Attempting to uninstall service $serviceName on $targetServer" | |
$result = $service.Delete() | |
Test-ServiceResult -operation "Delete service $serviceName on $targetServer" -result $result | |
} | |
function Test-ServiceResult( | |
[string]$operation = $(throw "operation is required"), | |
[object]$result = $(throw "result is required"), | |
[switch]$continueOnError = $false) | |
{ | |
$retVal = -1 | |
if ($result.GetType().Name -eq "UInt32") { $retVal = $result } else {$retVal = $result.ReturnValue} | |
if ($retVal -eq 0) {return} | |
$errorcode = 'Success,Not Supported,Access Denied,Dependent Services Running,Invalid Service Control' | |
$errorcode += ',Service Cannot Accept Control, Service Not Active, Service Request Timeout' | |
$errorcode += ',Unknown Failure, Path Not Found, Service Already Running, Service Database Locked' | |
$errorcode += ',Service Dependency Deleted, Service Dependency Failure, Service Disabled' | |
$errorcode += ',Service Logon Failure, Service Marked for Deletion, Service No Thread' | |
$errorcode += ',Status Circular Dependency, Status Duplicate Name, Status Invalid Name' | |
$errorcode += ',Status Invalid Parameter, Status Invalid Service Account, Status Service Exists' | |
$errorcode += ',Service Already Paused' | |
$desc = $errorcode.Split(',')[$retVal] | |
$msg = ("{0} failed with code {1}:{2}" -f $operation, $retVal, $desc) | |
if (!$continueOnError) { Write-Error $msg } else { Write-Warning $msg } | |
} | |
$service = $null | |
if([string]::IsNullOrWhiteSpace($DeploymentServer)) | |
{ | |
$service = Get-WmiObject Win32_Service -filter "name='$ServiceName'" | |
Stop-Service $ServiceName | |
Write-Host "Waiting 5 seconds for the service to stop..." | |
Start-Sleep -s 5 | |
if($service.State -eq "Stopped") | |
{ | |
$service.Delete() | |
Write-Host "The service has been successfully removed!" | |
} | |
else | |
{ | |
Write-Error "The service could not be stopped and the removal has failed!" | |
} | |
} | |
else | |
{ | |
Uninstall-RemoteService -serviceName $ServiceName -targetServer $DeploymentServer | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment