Chrome Installation has been rewriten for using the installer from Chrome web site. Total refactoring (adding the error handling and elimination of 'copy-past'

This commit is contained in:
Andy Mishechkin
2019-12-19 13:49:39 +04:00
parent 9786a9946a
commit 37527bf54a
2 changed files with 128 additions and 16 deletions

View File

@@ -2,25 +2,135 @@
## File: Install-Chrome.ps1
## Desc: Install Google Chrome
################################################################################
function Stop-SvcWithErrHandling
{
param (
[Parameter(Mandatory, ValueFromPipeLine = $true)] [string] $ServiceName
)
Import-Module -Name ImageHelpers -Force
Begin {
Write-Debug "Function [Stop-SvcWithErrHnadlig] is started";
}
Process {
$Service = Get-Service $ServiceName -ErrorAction SilentlyContinue
if(-not $Service) {
Write-Warning "[!] Service [$ServiceName] is not found";
}
else {
Write-Debug "Try to stop service [$ServiceName]";
try {
Stop-Service -Name $ServiceName -Force;
$Service.WaitForStatus("Stopped", "00:01:00");
Write-Debug "Service [$ServiceName] has been stoppet successfuly";
}
catch {
Write-Error "[!] Failed to stop service [$ServiceName] with error:"
$_ | Out-String | Write-Error;
}
}
}
End {
Write-Debug "Function [Stop-SvcWithErrHnadlig] is stopped";
}
}
$temp_install_dir = 'C:\Windows\Installer'
New-Item -Path $temp_install_dir -ItemType Directory -Force
function Set-SvcWithErrHandling
{
param (
[Parameter(Mandatory, ValueFromPipeLine = $true)] [string] $ServiceName,
[Parameter(Mandatory)] [hashtable] $Arguments
)
Begin {
Write-Debug "Function [Set-SvcWithErrHnadlig] is started";
}
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;
}
}
End {
Write-Debug "Function [Stop-SvcWithErrHnadlig] is stopped";
}
}
Install-MSI -MsiUrl "https://seleniumwebdrivers.blob.core.windows.net/knownchromeversion/googlechromestandaloneenterprise64.msi" -MsiName "googlechromestandaloneenterprise64.msi"
function New-ItemWithErrHandling {
param (
[Parameter(Mandatory)] [hashtable] $Arguments
)
Write-Debug "Creation of [$($Arguments.Name)] item";
try {
New-ItemProperty @Arguments;
}
catch {
Write-Warning "[!] Failed to create [$($Arguments.Name)] registry parameter";
}
}
New-NetFirewallRule -DisplayName "BlockGoogleUpdate" -Direction Outbound -Action Block -Program "C:\Program Files (x86)\Google\Update\GoogleUpdate.exe"
$TempPath = $env:TEMP;
$ChromeInstallerFile = "chrome_installer.exe";
$ChromeInstallerUri = "https://dl.google.com/chrome/install/375.126/chrome_installer.exe";
$IntallationError = $false;
Stop-Service -Name gupdate -Force
Set-Service -Name gupdate -StartupType "Disabled"
Stop-Service -Name gupdatem -Force
Set-Service -Name gupdatem -StartupType "Disabled"
try {
Write-Debug "Getting the Chrome installer: [$TempPath\$ChromeInstallerFile]";
Invoke-WebRequest -Uri $ChromeInstallerUri -OutFile "$TempPath\$ChromeInstallerFile";
Write-Debug "Run the Chrome installer."
Start-Process -FilePath "$TempPath\$ChromeInstallerFile" -ArgumentList "/silent /install" -Wait;
Write-Debug "Chrome installation complette";
}
catch {
Write-Error "[!] Failed to install Google Chrome: [$($_.Exception.Message)]";
$IntallationError = $true;
}
finally{
Write-Debug "Removing the Chrome installer file."
Remove-Item "$TempPath\$ChromeInstallerFile" -Force;
}
if($IntallationError) {
exit;
}
Write-Debug "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";
('gupdate','gupdatem') | Stop-SvcWithErrHandling;
('gupdate','gupdatem') | Set-SvcWithErrHandling -Arguments @{StartupType = "Disabled"};
$regGoogleUpdatePath = "HKLM:\SOFTWARE\Policies\Google\Update";
$regGoogleUpdateChrome = "HKLM:\SOFTWARE\Policies\Google\Chrome";
($regGoogleUpdatePath, $regGoogleUpdateChrome) | ForEach-Object {
Write-Debug "Creation of [$_] registry key";
try {
New-Item -Path $_ -Force;
}
catch {
Write-Warning "[!] Failed to create [$_] registry key";
}
}
$regGoogleUpdateParameters = @(
@{ Name = "AutoUpdateCheckPeriodMinutes"; Value = 00000000},
@{ Name = "UpdateDefault"; Value = 00000000 },
@{ Name = "DisableAutoUpdateChecksCheckboxValuet"; Value = 00000000 },
@{ Name = "Update{8A69D345-D564-463C-AFF1-A69D9E530F96}"; Value = 00000000 }
)
$regGoogleUpdateParameters | ForEach-Object {
$Arguments = $_;
$Arguments.Add("Path", $regGoogleUpdatePath);
$Arguments.Add("Force", $true);
New-ItemWithErrHandling -Arguments $Arguments
}
$Arguments = @{ Path = $regGoogleUpdateChrome; Name = "AutoUpdateCheckPeriodMinutes"; Value = 00000000; Force = $true };
New-ItemWithErrHandling -Arguments $Arguments;
New-Item -Path "HKLM:\SOFTWARE\Policies\Google\Update" -Force
New-ItemProperty "HKLM:\SOFTWARE\Policies\Google\Update" -Name "AutoUpdateCheckPeriodMinutes" -Value 00000000 -Force
New-ItemProperty "HKLM:\SOFTWARE\Policies\Google\Update" -Name "UpdateDefault" -Value 00000000 -Force
New-ItemProperty "HKLM:\SOFTWARE\Policies\Google\Update" -Name "DisableAutoUpdateChecksCheckboxValue" -Value 00000001 -Force
New-ItemProperty "HKLM:\SOFTWARE\Policies\Google\Update" -Name "Update{8A69D345-D564-463C-AFF1-A69D9E530F96}" -Value 00000000 -Force
New-Item -Path "HKLM:\SOFTWARE\Policies\Google\Chrome" -Force
New-ItemProperty "HKLM:\SOFTWARE\Policies\Google\Chrome" -Name "DefaultBrowserSettingEnabled" -Value 00000000 -Force

View File

@@ -2,6 +2,8 @@
## File: Install-SeleniumWebDrivers.ps1
## Desc: Install Selenium Web Drivers
################################################################################
$ChromePath =
Invoke-WebRequest -UseBasicParsing -Uri "https://seleniumwebdrivers.blob.core.windows.net/seleniumwebdrivers/SeleniumWebDrivers.zip" -OutFile SeleniumWebDrivers.zip