diff --git a/images/windows/scripts/build/Configure-System.ps1 b/images/windows/scripts/build/Configure-System.ps1 index 481d52b3..08d09f14 100644 --- a/images/windows/scripts/build/Configure-System.ps1 +++ b/images/windows/scripts/build/Configure-System.ps1 @@ -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 = @( diff --git a/images/windows/scripts/build/Install-Apache.ps1 b/images/windows/scripts/build/Install-Apache.ps1 index e2d37dcc..d5e85a6b 100644 --- a/images/windows/scripts/build/Install-Apache.ps1 +++ b/images/windows/scripts/build/Install-Apache.ps1 @@ -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" diff --git a/images/windows/scripts/build/Install-Chrome.ps1 b/images/windows/scripts/build/Install-Chrome.ps1 index 05d1ce3e..e07920ff 100644 --- a/images/windows/scripts/build/Install-Chrome.ps1 +++ b/images/windows/scripts/build/Install-Chrome.ps1 @@ -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" diff --git a/images/windows/scripts/build/Install-MongoDB.ps1 b/images/windows/scripts/build/Install-MongoDB.ps1 index 35cfd76f..da080a58 100644 --- a/images/windows/scripts/build/Install-MongoDB.ps1 +++ b/images/windows/scripts/build/Install-MongoDB.ps1 @@ -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" diff --git a/images/windows/scripts/build/Install-Nginx.ps1 b/images/windows/scripts/build/Install-Nginx.ps1 index 5d2dd0d2..4b6f1bd5 100644 --- a/images/windows/scripts/build/Install-Nginx.ps1 +++ b/images/windows/scripts/build/Install-Nginx.ps1 @@ -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" diff --git a/images/windows/scripts/build/Install-PostgreSQL.ps1 b/images/windows/scripts/build/Install-PostgreSQL.ps1 index c6803662..94ca9703 100644 --- a/images/windows/scripts/build/Install-PostgreSQL.ps1 +++ b/images/windows/scripts/build/Install-PostgreSQL.ps1 @@ -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" diff --git a/images/windows/scripts/helpers/ImageHelpers.psm1 b/images/windows/scripts/helpers/ImageHelpers.psm1 index 750ccf51..1db9f5b5 100644 --- a/images/windows/scripts/helpers/ImageHelpers.psm1 +++ b/images/windows/scripts/helpers/ImageHelpers.psm1 @@ -25,8 +25,6 @@ Export-ModuleMember -Function @( 'Get-ToolsetContent' 'Get-TCToolVersionPath' 'Get-TCToolPath' - 'Stop-SvcWithErrHandling' - 'Set-SvcWithErrHandling' 'Start-DownloadWithRetry' 'Get-VsixExtenstionFromMarketplace' 'Install-VSIXFromFile' diff --git a/images/windows/scripts/helpers/InstallHelpers.ps1 b/images/windows/scripts/helpers/InstallHelpers.ps1 index f2804564..24db81e8 100644 --- a/images/windows/scripts/helpers/InstallHelpers.ps1 +++ b/images/windows/scripts/helpers/InstallHelpers.ps1 @@ -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 (