Functions [Stop-SvcWithErrHandling] and [Set-SvcWithErrHandling] have been moved to ImageHelpers.psm1

This commit is contained in:
Andy Mishechkin
2019-12-23 17:41:24 +04:00
parent b5bfd51bdf
commit 7ad7e32149
3 changed files with 83 additions and 50 deletions

View File

@@ -16,4 +16,6 @@ Export-ModuleMember -Function @(
'Install-EXE'
'Add-ContentToMarkdown'
'Add-SoftwareDetailsToMarkdown'
'Stop-SvcWithErrHandling'
'Set-SvcWithErrHandling'
)

View File

@@ -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;
}
}
}

View File

@@ -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";