Last active
March 22, 2016 11:42
-
-
Save otakusid/d621fde09e57bfd2b59f to your computer and use it in GitHub Desktop.
AWS Code Deploy - delete windows service during 'ApplicationStop' event
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
$serviceName = '[[[service.name]]]' # in format 'Namespace.ServiceName' | |
$service = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" | |
$i = $serviceName.LastIndexOf('.'); | |
$serviceShortName = $serviceName.Substring($i + 1); | |
$eventSourceName = "CodeDeploy.$serviceShortName.ApplicationStop" | |
if(-Not [System.Diagnostics.EventLog]::SourceExists($eventSourceName)) { | |
New-EventLog –LogName "Application" –Source $eventSourceName | |
} | |
Write-EventLog –LogName Application –Source $eventSourceName –EntryType Information –EventID 1 –Message "script v.: [[[application.version]]]" | |
if($service) { | |
$serviceProcessId = $service.ProcessId; | |
$stopResponce = $service.StopService() | |
$deleteResponce = $service.delete() | |
Write-EventLog –LogName Application –Source $eventSourceName –EntryType Information –EventID 1 –Message "Service '$serviceName' marked for deletion" | |
Write-Host "service '$serviceName' marked for deletion" | |
Write-EventLog –LogName Application –Source $eventSourceName –EntryType Information –EventID 1 –Message "responces: stop '$stopResponce'; delete '$deleteResponce'" | |
Write-Host "responces: stop '$stopResponce'; delete '$deleteResponce'" | |
Write-EventLog –LogName Application –Source $eventSourceName –EntryType Information –EventID 1 –Message "Process with ID '$serviceProcessId' still works. Wait some time" | |
Write-Host "process with ID '$serviceProcessId' still works. Wait some time" | |
Wait-Process -Id $serviceProcessId -ErrorAction SilentlyContinue | |
Write-EventLog –LogName Application –Source $eventSourceName –EntryType Information –EventID 1 –Message "Process with ID '$serviceProcessId' not exist" | |
Write-Host "process with ID '$serviceProcessId' not exist" | |
} else { | |
Write-EventLog –LogName Application –Source $eventSourceName –EntryType Information –EventID 1 –Message "Service '$serviceName' does not exist" | |
Write-Host "service '$serviceName' does not exist" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment