[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

@@ -105,10 +105,10 @@ $servicesToDisable = @(
'gupdate'
'gupdatem'
'StorSvc'
)
$servicesToDisable | Stop-SvcWithErrHandling
$servicesToDisable | Set-SvcWithErrHandling -Arguments @{StartupType = "Disabled"}
) | Get-Service -ErrorAction SilentlyContinue
Stop-Service $servicesToDisable
$servicesToDisable.WaitForStatus('Stopped', "00:01:00")
$servicesToDisable | Set-Service -StartupType Disabled
# Disable scheduled tasks
$allTasksInTaskPath = @(

View File

@@ -4,7 +4,7 @@
################################################################################
# Stop w3svc service
Stop-Service -Name w3svc | Out-Null
Stop-Service -Name w3svc
# Install latest apache in chocolatey
$installDir = "C:\tools"
@@ -12,10 +12,10 @@ Install-ChocoPackage apache-httpd -ArgumentList "--force", "--params", "/install
# Stop and disable Apache service
Stop-Service -Name Apache
Set-Service Apache -StartupType Disabled
Set-Service -Name Apache -StartupType Disabled
# Start w3svc service
Start-Service -Name w3svc | Out-Null
Start-Service -Name w3svc
# Invoke Pester Tests
Invoke-PesterTests -TestFile "Apache"

View File

@@ -12,9 +12,10 @@ Install-Binary `
Write-Host "Adding the firewall rule for Google update blocking..."
New-NetFirewallRule -DisplayName "BlockGoogleUpdate" -Direction Outbound -Action Block -Program "C:\Program Files (x86)\Google\Update\GoogleUpdate.exe"
$GoogleSvcs = ('gupdate','gupdatem')
$GoogleSvcs | Stop-SvcWithErrHandling -StopOnError
$GoogleSvcs | Set-SvcWithErrHandling -Arguments @{StartupType = "Disabled"}
$googleServices = @('gupdate', 'gupdatem') | Get-Service
Stop-Service $googleServices
$googleServices.WaitForStatus('Stopped', "00:01:00")
$googleServices | Set-Service -StartupType Disabled
$regGoogleUpdatePath = "HKLM:\SOFTWARE\Policies\Google\Update"
$regGoogleUpdateChrome = "HKLM:\SOFTWARE\Policies\Google\Chrome"

View File

@@ -26,17 +26,16 @@ Install-Binary `
-ExpectedSignature (Get-ToolsetContent).mongodb.signature
# Add mongodb to the PATH
$mongodbService = "mongodb"
$mongoPath = (Get-CimInstance Win32_Service -Filter "Name LIKE '$mongodbService'").PathName
$mongoPath = (Get-CimInstance Win32_Service -Filter "Name LIKE 'mongodb'").PathName
$mongoBin = Split-Path -Path $mongoPath.split('"')[1]
Add-MachinePathItem "$mongoBin"
# Wait for mongodb service running
$svc = Get-Service $mongodbService
$svc.WaitForStatus('Running','00:01:00')
$mongodbService = Get-Service "mongodb"
$mongodbService.WaitForStatus('Running', '00:01:00')
# Stop and disable mongodb service
Stop-Service -Name $mongodbService
Set-Service $mongodbService -StartupType Disabled
Stop-Service $mongodbService
$mongodbService | Set-Service -StartupType Disabled
Invoke-PesterTests -TestFile "Databases" -TestName "MongoDB"

View File

@@ -4,7 +4,7 @@
################################################################################
# Stop w3svc service
Stop-Service -Name w3svc | Out-Null
Stop-Service -Name w3svc
# Install latest nginx in chocolatey
$installDir = "C:\tools"
@@ -12,10 +12,10 @@ Install-ChocoPackage nginx -ArgumentList "--force", "--params", "/installLocatio
# Stop and disable Nginx service
Stop-Service -Name nginx
Set-Service nginx -StartupType Disabled
Set-Service -Name nginx -StartupType Disabled
# Start w3svc service
Start-Service -Name w3svc | Out-Null
Start-Service -Name w3svc
# Invoke Pester Tests
Invoke-PesterTests -TestFile "Nginx"

View File

@@ -78,7 +78,7 @@ if ($exitCode -ne 0) {
# Stop and disable PostgreSQL service
$pgService = Get-Service -Name postgresql*
Stop-Service -InputObject $pgService
Set-Service -InputObject $pgService -StartupType Disabled
Stop-Service $pgService
$pgService | Set-Service -StartupType Disabled
Invoke-PesterTests -TestFile "Databases" -TestName "PostgreSQL"

View File

@@ -25,8 +25,6 @@ Export-ModuleMember -Function @(
'Get-ToolsetContent'
'Get-TCToolVersionPath'
'Get-TCToolPath'
'Stop-SvcWithErrHandling'
'Set-SvcWithErrHandling'
'Start-DownloadWithRetry'
'Get-VsixExtenstionFromMarketplace'
'Install-VSIXFromFile'

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
(