From 7ad7e32149cedfcba23346157e52db73ef1d5883 Mon Sep 17 00:00:00 2001 From: Andy Mishechkin Date: Mon, 23 Dec 2019 17:41:24 +0400 Subject: [PATCH] Functions [Stop-SvcWithErrHandling] and [Set-SvcWithErrHandling] have been moved to ImageHelpers.psm1 --- .../scripts/ImageHelpers/ImageHelpers.psm1 | 2 + .../scripts/ImageHelpers/InstallHelpers.ps1 | 79 +++++++++++++++++++ .../win/scripts/Installers/Install-Chrome.ps1 | 52 +----------- 3 files changed, 83 insertions(+), 50 deletions(-) diff --git a/images/win/scripts/ImageHelpers/ImageHelpers.psm1 b/images/win/scripts/ImageHelpers/ImageHelpers.psm1 index 12df84e7a..6e091e7e7 100644 --- a/images/win/scripts/ImageHelpers/ImageHelpers.psm1 +++ b/images/win/scripts/ImageHelpers/ImageHelpers.psm1 @@ -16,4 +16,6 @@ Export-ModuleMember -Function @( 'Install-EXE' 'Add-ContentToMarkdown' 'Add-SoftwareDetailsToMarkdown' + 'Stop-SvcWithErrHandling' + 'Set-SvcWithErrHandling' ) diff --git a/images/win/scripts/ImageHelpers/InstallHelpers.ps1 b/images/win/scripts/ImageHelpers/InstallHelpers.ps1 index 12bb373fc..788e60043 100644 --- a/images/win/scripts/ImageHelpers/InstallHelpers.ps1 +++ b/images/win/scripts/ImageHelpers/InstallHelpers.ps1 @@ -81,3 +81,82 @@ function Install-EXE return -1 } } + +function Stop-SvcWithErrHandling +<# +.DESCRIPTION +Function for stopping the Windows Service with error handling + +.AUTHOR +Andrey Mishechkin v-andmis@microsoft.com + +.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, + [Parameter()] [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 + +.AUTHOR +Andrey Mishechkin v-andmis@microsoft.com + +.PARAMETER -ServiceName +The name of stopping service + +.PARAMETER -Arguments +Hashtable for service arguments +#> +{ + + param ( + [Parameter(Mandatory, ValueFromPipeLine = $true)] [string] $ServiceName, + [Parameter(Mandatory)] [hashtable] $Arguments + ) + + Process { + $Service = Get-Service $ServiceName -ErrorAction SilentlyContinue + if (-not $Service) { + Write-Warning "[!] Service [$ServiceName] is not found"; + } + try { + Set-Service $ServiceName @Arguments; + } + catch { + Write-Error "[!] Failed to set service [$ServiceName] arguments with error:" + $_ | Out-String | Write-Error; + } + } +} diff --git a/images/win/scripts/Installers/Install-Chrome.ps1 b/images/win/scripts/Installers/Install-Chrome.ps1 index 1e4239b10..c730ac040 100644 --- a/images/win/scripts/Installers/Install-Chrome.ps1 +++ b/images/win/scripts/Installers/Install-Chrome.ps1 @@ -2,56 +2,8 @@ ## File: Install-Chrome.ps1 ## Desc: Install Google Chrome ################################################################################ -function Stop-SvcWithErrHandling -{ - param ( - [Parameter(Mandatory, ValueFromPipeLine = $true)] [string] $ServiceName - ) - Process { - $Service = Get-Service $ServiceName -ErrorAction SilentlyContinue - if (-not $Service) { - Write-Error "[!] Service [$ServiceName] is not found"; - 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 -{ - param ( - [Parameter(Mandatory, ValueFromPipeLine = $true)] [string] $ServiceName, - [Parameter(Mandatory)] [hashtable] $Arguments - ) - - Process { - $Service = Get-Service $ServiceName -ErrorAction SilentlyContinue - if (-not $Service) { - Write-Warning "[!] Service [$ServiceName] is not found"; - } - try { - Set-Service $ServiceName @Arguments; - } - catch { - Write-Error "[!] Failed to set service [$ServiceName] arguments with error:" - $_ | Out-String | Write-Error; - } - } -} - -Import-Module -Name ImageHelpers -Force; +Import-Module -Name "D:\GitHub\Akvelon\Forks\virtual-environments\images\win\scripts\ImageHelpers\ImageHelpers.psm1" -Force; $ChromeInstallerFile = "chrome_installer.exe"; $ChromeInstallerUri = "https://dl.google.com/chrome/install/375.126/${ChromeInstallerFile}"; @@ -61,7 +13,7 @@ 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; +$GoogleSvcs | Stop-SvcWithErrHandling -StopOnError; $GoogleSvcs | Set-SvcWithErrHandling -Arguments @{StartupType = "Disabled"}; $regGoogleUpdatePath = "HKLM:\SOFTWARE\Policies\Google\Update";