-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeleteService.ps1
41 lines (34 loc) · 1.06 KB
/
DeleteService.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<#
Deletes a service whihc is passed as a parameter
#>
Param(
[string]$ServiceDisplayName
)
echo "Service Name: $ServiceDisplayName"
function Confirm-WindowsServiceExists ($name)
{
if (Get-Service -Displayname $name -ErrorAction SilentlyContinue)
{
return $true
}
return $false
}
echo "Checking if Service Exists......."
if (Confirm-WindowsServiceExists $ServiceDisplayName) {
echo "$ServiceName Service Exists. Will attempt to Stop & Remove service"
$ServiceName = Get-Service -Displayname $ServiceDisplayName | select -first 1 -ExpandProperty Name
Get-Service -Displayname $ServiceDisplayName | Stop-Service
C:\Windows\System32\sc.exe delete $ServiceName
if (Confirm-WindowsServiceExists $ServiceDisplayName) {
#Problem Service has not been Deleted
echo "error: Problem Service not successfully Deleted"
Write-Error ("Problem Service not successfully Deleted")
Exit 1
}
else {
echo "Service Deleted Successfully"
}
}
else{
echo "Exit Success -> Service does not exist"
}