[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
@@ -105,10 +105,10 @@ $servicesToDisable = @(
'gupdate' 'gupdate'
'gupdatem' 'gupdatem'
'StorSvc' 'StorSvc'
) ) | Get-Service -ErrorAction SilentlyContinue
Stop-Service $servicesToDisable
$servicesToDisable | Stop-SvcWithErrHandling $servicesToDisable.WaitForStatus('Stopped', "00:01:00")
$servicesToDisable | Set-SvcWithErrHandling -Arguments @{StartupType = "Disabled"} $servicesToDisable | Set-Service -StartupType Disabled
# Disable scheduled tasks # Disable scheduled tasks
$allTasksInTaskPath = @( $allTasksInTaskPath = @(
@@ -4,7 +4,7 @@
################################################################################ ################################################################################
# Stop w3svc service # Stop w3svc service
Stop-Service -Name w3svc | Out-Null Stop-Service -Name w3svc
# Install latest apache in chocolatey # Install latest apache in chocolatey
$installDir = "C:\tools" $installDir = "C:\tools"
@@ -12,10 +12,10 @@ Install-ChocoPackage apache-httpd -ArgumentList "--force", "--params", "/install
# Stop and disable Apache service # Stop and disable Apache service
Stop-Service -Name Apache Stop-Service -Name Apache
Set-Service Apache -StartupType Disabled Set-Service -Name Apache -StartupType Disabled
# Start w3svc service # Start w3svc service
Start-Service -Name w3svc | Out-Null Start-Service -Name w3svc
# Invoke Pester Tests # Invoke Pester Tests
Invoke-PesterTests -TestFile "Apache" Invoke-PesterTests -TestFile "Apache"
@@ -12,9 +12,10 @@ Install-Binary `
Write-Host "Adding the firewall rule for Google update blocking..." 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" New-NetFirewallRule -DisplayName "BlockGoogleUpdate" -Direction Outbound -Action Block -Program "C:\Program Files (x86)\Google\Update\GoogleUpdate.exe"
$GoogleSvcs = ('gupdate','gupdatem') $googleServices = @('gupdate', 'gupdatem') | Get-Service
$GoogleSvcs | Stop-SvcWithErrHandling -StopOnError Stop-Service $googleServices
$GoogleSvcs | Set-SvcWithErrHandling -Arguments @{StartupType = "Disabled"} $googleServices.WaitForStatus('Stopped', "00:01:00")
$googleServices | Set-Service -StartupType Disabled
$regGoogleUpdatePath = "HKLM:\SOFTWARE\Policies\Google\Update" $regGoogleUpdatePath = "HKLM:\SOFTWARE\Policies\Google\Update"
$regGoogleUpdateChrome = "HKLM:\SOFTWARE\Policies\Google\Chrome" $regGoogleUpdateChrome = "HKLM:\SOFTWARE\Policies\Google\Chrome"
@@ -26,17 +26,16 @@ Install-Binary `
-ExpectedSignature (Get-ToolsetContent).mongodb.signature -ExpectedSignature (Get-ToolsetContent).mongodb.signature
# Add mongodb to the PATH # Add mongodb to the PATH
$mongodbService = "mongodb" $mongoPath = (Get-CimInstance Win32_Service -Filter "Name LIKE 'mongodb'").PathName
$mongoPath = (Get-CimInstance Win32_Service -Filter "Name LIKE '$mongodbService'").PathName
$mongoBin = Split-Path -Path $mongoPath.split('"')[1] $mongoBin = Split-Path -Path $mongoPath.split('"')[1]
Add-MachinePathItem "$mongoBin" Add-MachinePathItem "$mongoBin"
# Wait for mongodb service running # Wait for mongodb service running
$svc = Get-Service $mongodbService $mongodbService = Get-Service "mongodb"
$svc.WaitForStatus('Running','00:01:00') $mongodbService.WaitForStatus('Running', '00:01:00')
# Stop and disable mongodb service # Stop and disable mongodb service
Stop-Service -Name $mongodbService Stop-Service $mongodbService
Set-Service $mongodbService -StartupType Disabled $mongodbService | Set-Service -StartupType Disabled
Invoke-PesterTests -TestFile "Databases" -TestName "MongoDB" Invoke-PesterTests -TestFile "Databases" -TestName "MongoDB"
@@ -4,7 +4,7 @@
################################################################################ ################################################################################
# Stop w3svc service # Stop w3svc service
Stop-Service -Name w3svc | Out-Null Stop-Service -Name w3svc
# Install latest nginx in chocolatey # Install latest nginx in chocolatey
$installDir = "C:\tools" $installDir = "C:\tools"
@@ -12,10 +12,10 @@ Install-ChocoPackage nginx -ArgumentList "--force", "--params", "/installLocatio
# Stop and disable Nginx service # Stop and disable Nginx service
Stop-Service -Name nginx Stop-Service -Name nginx
Set-Service nginx -StartupType Disabled Set-Service -Name nginx -StartupType Disabled
# Start w3svc service # Start w3svc service
Start-Service -Name w3svc | Out-Null Start-Service -Name w3svc
# Invoke Pester Tests # Invoke Pester Tests
Invoke-PesterTests -TestFile "Nginx" Invoke-PesterTests -TestFile "Nginx"
@@ -78,7 +78,7 @@ if ($exitCode -ne 0) {
# Stop and disable PostgreSQL service # Stop and disable PostgreSQL service
$pgService = Get-Service -Name postgresql* $pgService = Get-Service -Name postgresql*
Stop-Service -InputObject $pgService Stop-Service $pgService
Set-Service -InputObject $pgService -StartupType Disabled $pgService | Set-Service -StartupType Disabled
Invoke-PesterTests -TestFile "Databases" -TestName "PostgreSQL" Invoke-PesterTests -TestFile "Databases" -TestName "PostgreSQL"
@@ -25,8 +25,6 @@ Export-ModuleMember -Function @(
'Get-ToolsetContent' 'Get-ToolsetContent'
'Get-TCToolVersionPath' 'Get-TCToolVersionPath'
'Get-TCToolPath' 'Get-TCToolPath'
'Stop-SvcWithErrHandling'
'Set-SvcWithErrHandling'
'Start-DownloadWithRetry' 'Start-DownloadWithRetry'
'Get-VsixExtenstionFromMarketplace' 'Get-VsixExtenstionFromMarketplace'
'Install-VSIXFromFile' 'Install-VSIXFromFile'
@@ -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 { function Start-DownloadWithRetry {
Param Param
( (