Skip to content

Commit

Permalink
Get-WmiObject - Invalid class "Win32_Service" - Workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
stryngs committed Oct 12, 2022
1 parent bd081b6 commit 257f588
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
17 changes: 13 additions & 4 deletions dev-tools/packaging/templates/windows/install-service.ps1.tmpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
# Delete and stop the service if it already exists.
if (Get-Service {{.BeatName}} -ErrorAction SilentlyContinue) {
$service = Get-WmiObject -Class Win32_Service -Filter "name='{{.BeatName}}'"
$service.StopService()
Start-Sleep -s 1
$service.delete()
try {
$service = Get-WmiObject -Class Win32_Service -Filter "name='{{.BeatName}}'"
$service.StopService()
(Get-Service {{.BeatName}}).WaitForStatus('Stopped')
Start-Sleep -s 1
$service.delete()
}
catch {
Stop-Service {{.BeatName}}
(Get-Service {{.BeatName}}).WaitForStatus('Stopped')
Start-Sleep -s 1
sc.exe delete {{.BeatName}}
}
}

$workdir = Split-Path $MyInvocation.MyCommand.Path
Expand Down
17 changes: 13 additions & 4 deletions dev-tools/packaging/templates/windows/uninstall-service.ps1.tmpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# Delete and stop the service if it already exists.
if (Get-Service {{.BeatName}} -ErrorAction SilentlyContinue) {
$service = Get-WmiObject -Class Win32_Service -Filter "name='{{.BeatName}}'"
$service.StopService()
Start-Sleep -s 1
$service.delete()
try {
$service = Get-WmiObject -Class Win32_Service -Filter "name='{{.BeatName}}'"
$service.StopService()
(Get-Service {{.BeatName}}).WaitForStatus('Stopped')
Start-Sleep -s 1
$service.delete()
}
catch {
Stop-Service {{.BeatName}}
(Get-Service {{.BeatName}}).WaitForStatus('Stopped')
Start-Sleep -s 1
sc.exe delete {{.BeatName}}
}
}

0 comments on commit 257f588

Please sign in to comment.