[Windows] Unify services handling (#8871)

* [Windows] Unify services handling

* Fix Set-Service usage
This commit is contained in:
Vasilii Polikarpov
2023-11-23 18:08:20 +01:00
committed by GitHub
parent 6efbc46fc7
commit 79c347765a
8 changed files with 21 additions and 102 deletions

View File

@@ -130,85 +130,6 @@ function Install-Binary {
}
}
function Stop-SvcWithErrHandling {
<#
.DESCRIPTION
Function for stopping the Windows Service with error handling
.PARAMETER ServiceName
The name of stopping service
.PARAMETER StopOnError
Switch for stopping the script and exit from PowerShell if one service is absent
#>
Param (
[Parameter(Mandatory, ValueFromPipeLine = $true)]
[string] $ServiceName,
[switch] $StopOnError
)
Process {
$service = Get-Service $ServiceName -ErrorAction SilentlyContinue
if (-not $service) {
Write-Warning "[!] Service [$ServiceName] is not found"
if ($StopOnError) {
exit 1
}
} else {
Write-Host "Try to stop service [$ServiceName]"
try {
Stop-Service -Name $ServiceName -Force
$service.WaitForStatus("Stopped", "00:01:00")
Write-Host "Service [$ServiceName] has been stopped successfuly"
} catch {
Write-Error "[!] Failed to stop service [$ServiceName] with error:"
$_ | Out-String | Write-Error
}
}
}
}
function Set-SvcWithErrHandling {
<#
.DESCRIPTION
Function for setting the Windows Service parameter with error handling
.PARAMETER ServiceName
The name of stopping service
.PARAMETER Arguments
Hashtable for service arguments
.PARAMETER StopOnError
Switch for stopping the script and exit from PowerShell if one service is absent
#>
Param (
[Parameter(Mandatory, ValueFromPipeLine = $true)]
[string] $ServiceName,
[Parameter(Mandatory)]
[hashtable] $Arguments,
[switch] $StopOnError
)
Process {
$service = Get-Service $ServiceName -ErrorAction SilentlyContinue
if (-not $service) {
Write-Warning "[!] Service [$ServiceName] is not found"
if ($StopOnError) {
exit 1
}
} else {
try {
Set-Service $serviceName @Arguments
} catch {
Write-Error "[!] Failed to set service [$ServiceName] arguments with error:"
$_ | Out-String | Write-Error
}
}
}
}
function Start-DownloadWithRetry {
Param
(