Skip to content

Commit

Permalink
moved stopping service to new function
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkChaos committed Jun 17, 2020
1 parent 0b43563 commit dba0825
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,32 +173,7 @@ $scriptBlock = {
}
}
elseif ($serviceObject) {
if ($serviceObject.State -eq 'Running') {
$stopServiceTimer = [Diagnostics.Stopwatch]::StartNew()
Write-Output "[$env:ComputerName]: Stopping [$ServiceName]"
do {
$serviceObject = Get-WindowsService -ServiceName $ServiceName
$results = $serviceObject.StopService()

if ($stopServiceTimer.Elapsed.TotalSeconds -gt $Timeout) {
if ($StopProcess) {
Write-Verbose "[$env:ComputerName]: [$ServiceName] did not respond within [$Timeout] seconds, stopping process."
$allProcesses = Get-Process
$process = $allProcesses | Where-Object { $_.Path -like "$parentPath\*" }
if ($process) {
Write-Warning -Message "[$env:ComputerName]: Files are still in use by [$($process.ProcessName)], stopping the process!"
$process | Stop-Process -Force -ErrorAction SilentlyContinue
}
}
else {
return Write-Error -Message "[$env:ComputerName]: [$ServiceName] did not respond within [$Timeout] seconds."
}
}
$serviceObject = Get-WindowsService -ServiceName $ServiceName
}
while ($serviceObject.State -ne 'Stopped')
}

$serviceObject = Stop-WindowsService -ServiceName $ServiceName -Timeout $Timeout -StopProcess:$StopProcess
$parentPath = Get-FullExecuteablePath -StringContainingPath $serviceObject.PathName -JustParentPath
Write-Output "[$env:ComputerName]: Identified [$ServiceName] installation directory [$parentPath]"

Expand Down
51 changes: 50 additions & 1 deletion WindowsServiceManager/WindowsServiceManagerV4/utility.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,55 @@ function Start-WindowsService {
}
}

function Stop-WindowsService {
param
(
[string]
$ServiceName,

[int]
$Timeout = 30,

[switch]
$StopProcess = $false
)

$serviceObject = Get-WindowsService -ServiceName $ServiceName

if ($serviceObject.State -eq 'Running') {
$stopServiceTimer = [Diagnostics.Stopwatch]::StartNew()
Write-Output "[$env:ComputerName]: Stopping Service [$ServiceName]"
do {
$serviceObject = Get-WindowsService -ServiceName $ServiceName
$results = $serviceObject.StopService()

if ($stopServiceTimer.Elapsed.TotalSeconds -gt $Timeout) {
if ($StopProcess) {
Write-Verbose "[$env:ComputerName]: [$ServiceName] did not respond within [$Timeout] seconds, stopping process."

$fullPath = Get-FullExecuteablePath -StringContainingPath $serviceObject.PathName

$allProcesses = Get-Process
$process = $allProcesses | Where-Object { $_.Path -like "$parentPath\*" }
if ($process) {
Write-Warning -Message "[$env:ComputerName]: Files are still in use by [$($process.ProcessName)], stopping the process!"
$process | Stop-Process -Force -ErrorAction SilentlyContinue
}
}
else {
return Write-Error -Message "[$env:ComputerName]: [$ServiceName] did not respond within [$Timeout] seconds."
}
}
$serviceObject = Get-WindowsService -ServiceName $ServiceName
}
while ($serviceObject.State -ne 'Stopped')

Write-Output "[$env:ComputerName]: Stopped Service [$ServiceName]"

return $serviceObject
}
}

function Get-FullExecuteablePath {
[CmdletBinding()]
param (
Expand All @@ -62,4 +111,4 @@ function Get-FullExecuteablePath {
}

return $matchedPath
}
}

0 comments on commit dba0825

Please sign in to comment.