From 37527bf54a23319896d51ccad371a5fc4a42986a Mon Sep 17 00:00:00 2001 From: Andy Mishechkin Date: Thu, 19 Dec 2019 13:49:39 +0400 Subject: [PATCH 01/16] Chrome Installation has been rewriten for using the installer from Chrome web site. Total refactoring (adding the error handling and elimination of 'copy-past' --- .../win/scripts/Installers/Install-Chrome.ps1 | 142 ++++++++++++++++-- .../Installers/Install-SeleniumWebDrivers.ps1 | 2 + 2 files changed, 128 insertions(+), 16 deletions(-) diff --git a/images/win/scripts/Installers/Install-Chrome.ps1 b/images/win/scripts/Installers/Install-Chrome.ps1 index 15a2e1e28..737d07234 100644 --- a/images/win/scripts/Installers/Install-Chrome.ps1 +++ b/images/win/scripts/Installers/Install-Chrome.ps1 @@ -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 diff --git a/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 b/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 index 3c08977a5..0b25d97c2 100644 --- a/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 +++ b/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 @@ -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 From 89f3857d4ba00f0974221770e2fd4db832ff6c2b Mon Sep 17 00:00:00 2001 From: Andy Mishechkin Date: Thu, 19 Dec 2019 16:17:13 +0400 Subject: [PATCH 02/16] Rewriten Chrome driver Installation logic to use Chrome version --- .../Installers/Install-SeleniumWebDrivers.ps1 | 43 +++++++++++++++---- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 b/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 index 0b25d97c2..45e60d01e 100644 --- a/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 +++ b/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 @@ -2,18 +2,45 @@ ## File: Install-SeleniumWebDrivers.ps1 ## Desc: Install Selenium Web Drivers ################################################################################ -$ChromePath = +$DestinationPath = "C:\"; +Write-Debug "Destination path: [$DestinationPath]"; +Write-Debug "Selenium drivers download and install..."; +Invoke-WebRequest -UseBasicParsing -Uri "https://seleniumwebdrivers.blob.core.windows.net/seleniumwebdrivers/SeleniumWebDrivers.zip" -OutFile SeleniumWebDrivers.zip; +Expand-Archive -Path SeleniumWebDrivers.zip -DestinationPath $DestinationPath -Force; +Remove-Item SeleniumWebDrivers.zip; +$ChromeDriverPath = "$DestinationPath\SeleniumWebDrivers\ChromeDriver"; +Write-Debug "Chrome driver path: [$ChromeDriverPath]"; +Remove-Item -Path "$ChromeDriverPath\*" -Force; -Invoke-WebRequest -UseBasicParsing -Uri "https://seleniumwebdrivers.blob.core.windows.net/seleniumwebdrivers/SeleniumWebDrivers.zip" -OutFile SeleniumWebDrivers.zip +$ChromePath = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(default)'; +[version]$ChromeVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($ChromePath).ProductVersion; +Write-Debug "Chrome version: [$ChromeVersion]"; -Expand-Archive -Path SeleniumWebDrivers.zip -DestinationPath "C:\" -Force +$ChromeDriverVersionUri = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$($ChromeVersion.Major).$($ChromeVersion.Minor).$($ChromeVersion.Build)"; +Write-Debug "Chrome driver version Uri [$ChromeDriverVersionUri]"; +Write-Debug "Getting the Chrome driver version..."; +$ChromeDriverVersion = Invoke-WebRequest -Uri $ChromeDriverVersionUri; +Write-Debug "Current Chrome driver version: [$ChromeDriverVersion]"; -Remove-Item SeleniumWebDrivers.zip +$ChromeDriverZipDownloadUri = "https://chromedriver.storage.googleapis.com/$($ChromeDriverVersion.ToString())/chromedriver_win32.zip"; +Write-Debug "Chrome driver zip file download Uri: [$ChromeDriverZipDownloadUri]"; -setx IEWebDriver "C:\SeleniumWebDrivers\IEDriver" /M -setx GeckoWebDriver "C:\SeleniumWebDrivers\GeckoDriver" /M -setx ChromeWebDriver "C:\SeleniumWebDrivers\ChromeDriver" /M +$DestFile= "$ChromeDriverPath\chromedriver_win32.zip"; +$ChromeDriverVersion.Content | Out-File -FilePath "$ChromeDriverPath\versioninfo.txt" -Force; -exit 0 +Write-Debug "Chrome driver download...."; +Invoke-WebRequest -Uri $ChromeDriverZipDownloadUri -OutFile $DestFile; + +Write-Debug "Chrome driver install...."; +Expand-Archive -Path "$ChromeDriverPath\chromedriver_win32.zip" -DestinationPath $ChromeDriverPath -Force; +Remove-Item -Path "$ChromeDriverPath\chromedriver_win32.zip" -Force; + +Write-Debug "Setting the environment variables"; + +setx IEWebDriver "C:\SeleniumWebDrivers\IEDriver" /M; +setx GeckoWebDriver "C:\SeleniumWebDrivers\GeckoDriver" /M; +setx ChromeWebDriver "C:\SeleniumWebDrivers\ChromeDriver" /M; + +exit 0; From 537e9da92924cf17e3aec1171b9b407c1171ce0e Mon Sep 17 00:00:00 2001 From: Ivan Nosar Date: Thu, 19 Dec 2019 16:19:58 +0300 Subject: [PATCH 03/16] Install Selenium (draft) --- images/win/Windows2016-Azure.json | 6 ++++++ images/win/Windows2019-Azure.json | 6 ++++++ .../scripts/Installers/Install-Selenium.ps1 | 21 +++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 images/win/scripts/Installers/Install-Selenium.ps1 diff --git a/images/win/Windows2016-Azure.json b/images/win/Windows2016-Azure.json index c51dcdfe5..a0ceb902c 100644 --- a/images/win/Windows2016-Azure.json +++ b/images/win/Windows2016-Azure.json @@ -336,6 +336,12 @@ "{{ template_dir }}/scripts/Installers/Install-Firefox.ps1" ] }, + { + "type": "powershell", + "scripts":[ + "{{ template_dir }}/scripts/Installers/Install-Selenium.ps1" + ] + }, { "type": "powershell", "scripts":[ diff --git a/images/win/Windows2019-Azure.json b/images/win/Windows2019-Azure.json index 6875b7f07..2424320ae 100644 --- a/images/win/Windows2019-Azure.json +++ b/images/win/Windows2019-Azure.json @@ -306,6 +306,12 @@ "{{ template_dir }}/scripts/Installers/Install-Firefox.ps1" ] }, + { + "type": "powershell", + "scripts":[ + "{{ template_dir }}/scripts/Installers/Install-Selenium.ps1" + ] + }, { "type": "powershell", "scripts":[ diff --git a/images/win/scripts/Installers/Install-Selenium.ps1 b/images/win/scripts/Installers/Install-Selenium.ps1 new file mode 100644 index 000000000..6dcb63ecc --- /dev/null +++ b/images/win/scripts/Installers/Install-Selenium.ps1 @@ -0,0 +1,21 @@ +################################################################################ +## File: Install-Selenium.ps1 +## Desc: Install Selenium Server standalone +################################################################################ + +# Acquire latest Selenium release number from GitHub API +$latestReleaseUrl = "https://api.github.com/repos/SeleniumHQ/selenium/releases/latest" +$latestReleaseInfo = Invoke-RestMethod -Uri $latestReleaseUrl +$seleniumVersionString = $latestReleaseInfo.name.Split(" ")[1] +$seleniumVersion = [version]::Parse($seleniumVersionString) + +# Download Selenium +Write-Host "Downloading selenium-server-standalone v$seleniumVersion..." + +$seleniumReleaseUrl = "https://selenium-release.storage.googleapis.com/$($seleniumVersion.ToString(2))/selenium-server-standalone-$($seleniumVersion.ToString(3)).jar" +$seleniumBinPath = "C:\selenium\selenium-server-standalone.jar" +Invoke-WebRequest -UseBasicParsing -Uri $seleniumReleaseUrl -OutFile $seleniumBinPath + +setx "CLASSPATH" "$($seleniumBinPath):$($env:CLASSPATH)" /M + +exit 0 From a2b624fefc4a2b4b0ab046808b67159ba883d72a Mon Sep 17 00:00:00 2001 From: Ivan Nosar Date: Thu, 19 Dec 2019 16:23:57 +0300 Subject: [PATCH 04/16] Add mkdir --- images/win/scripts/Installers/Install-Selenium.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/images/win/scripts/Installers/Install-Selenium.ps1 b/images/win/scripts/Installers/Install-Selenium.ps1 index 6dcb63ecc..c0a8d371f 100644 --- a/images/win/scripts/Installers/Install-Selenium.ps1 +++ b/images/win/scripts/Installers/Install-Selenium.ps1 @@ -13,6 +13,7 @@ $seleniumVersion = [version]::Parse($seleniumVersionString) Write-Host "Downloading selenium-server-standalone v$seleniumVersion..." $seleniumReleaseUrl = "https://selenium-release.storage.googleapis.com/$($seleniumVersion.ToString(2))/selenium-server-standalone-$($seleniumVersion.ToString(3)).jar" +New-Item -ItemType directory -Path "C:\selenium\" $seleniumBinPath = "C:\selenium\selenium-server-standalone.jar" Invoke-WebRequest -UseBasicParsing -Uri $seleniumReleaseUrl -OutFile $seleniumBinPath From ccd22c49b8cba4a6b8806ee236632fb55dcc6229 Mon Sep 17 00:00:00 2001 From: Andy Mishechkin Date: Thu, 19 Dec 2019 17:29:56 +0400 Subject: [PATCH 05/16] Postreview changes for using Install-Exe and bugfixing --- .../win/scripts/Installers/Install-Chrome.ps1 | 27 +++---------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/images/win/scripts/Installers/Install-Chrome.ps1 b/images/win/scripts/Installers/Install-Chrome.ps1 index 737d07234..970e56d11 100644 --- a/images/win/scripts/Installers/Install-Chrome.ps1 +++ b/images/win/scripts/Installers/Install-Chrome.ps1 @@ -74,31 +74,12 @@ function New-ItemWithErrHandling { } } -$TempPath = $env:TEMP; +Import-Module -Name ImageHelpers -Force; + $ChromeInstallerFile = "chrome_installer.exe"; $ChromeInstallerUri = "https://dl.google.com/chrome/install/375.126/chrome_installer.exe"; -$IntallationError = $false; +Install-Exe -Url $ChromeInstallerUri -Name $ChromeInstallerFile -ArgumentList ("/silent", "/install") -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"; @@ -131,6 +112,6 @@ $regGoogleUpdateParameters | ForEach-Object { New-ItemWithErrHandling -Arguments $Arguments } -$Arguments = @{ Path = $regGoogleUpdateChrome; Name = "AutoUpdateCheckPeriodMinutes"; Value = 00000000; Force = $true }; +$Arguments = @{ Path = $regGoogleUpdateChrome; Name = "DefaultBrowserSettingEnabled"; Value = 00000000; Force = $true }; New-ItemWithErrHandling -Arguments $Arguments; From 34fa1cbdbcd6ab70a88d29ee45f21072a7a83f0e Mon Sep 17 00:00:00 2001 From: Ivan Nosar Date: Thu, 19 Dec 2019 17:27:56 +0300 Subject: [PATCH 06/16] Finalized installing Selenium --- .../scripts/Installers/Install-Selenium.ps1 | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/images/win/scripts/Installers/Install-Selenium.ps1 b/images/win/scripts/Installers/Install-Selenium.ps1 index c0a8d371f..819fe1607 100644 --- a/images/win/scripts/Installers/Install-Selenium.ps1 +++ b/images/win/scripts/Installers/Install-Selenium.ps1 @@ -5,8 +5,15 @@ # Acquire latest Selenium release number from GitHub API $latestReleaseUrl = "https://api.github.com/repos/SeleniumHQ/selenium/releases/latest" -$latestReleaseInfo = Invoke-RestMethod -Uri $latestReleaseUrl +try { + $latestReleaseInfo = Invoke-RestMethod -Uri $latestReleaseUrl +} catch { + Write-Error $_ + exit 1 +} +Write-Debug $latestReleaseInfo $seleniumVersionString = $latestReleaseInfo.name.Split(" ")[1] +Write-Debug $seleniumVersionString $seleniumVersion = [version]::Parse($seleniumVersionString) # Download Selenium @@ -15,8 +22,14 @@ Write-Host "Downloading selenium-server-standalone v$seleniumVersion..." $seleniumReleaseUrl = "https://selenium-release.storage.googleapis.com/$($seleniumVersion.ToString(2))/selenium-server-standalone-$($seleniumVersion.ToString(3)).jar" New-Item -ItemType directory -Path "C:\selenium\" $seleniumBinPath = "C:\selenium\selenium-server-standalone.jar" -Invoke-WebRequest -UseBasicParsing -Uri $seleniumReleaseUrl -OutFile $seleniumBinPath +try { + Invoke-WebRequest -UseBasicParsing -Uri $seleniumReleaseUrl -OutFile $seleniumBinPath +} catch { + Write-Error $_ + exit 1 +} -setx "CLASSPATH" "$($seleniumBinPath):$($env:CLASSPATH)" /M +Write-Host "Add selenium to CLASSPATH..." +setx "CLASSPATH" "$($seleniumBinPath);$($env:CLASSPATH)" /M exit 0 From 166982b4c42d2cdb615c2c450b885b67ad4dab9c Mon Sep 17 00:00:00 2001 From: Ivan Nosar Date: Fri, 20 Dec 2019 13:05:06 +0300 Subject: [PATCH 07/16] Add debug info about classpath value --- images/win/scripts/Installers/Install-Selenium.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/images/win/scripts/Installers/Install-Selenium.ps1 b/images/win/scripts/Installers/Install-Selenium.ps1 index 819fe1607..9818f4142 100644 --- a/images/win/scripts/Installers/Install-Selenium.ps1 +++ b/images/win/scripts/Installers/Install-Selenium.ps1 @@ -31,5 +31,6 @@ try { Write-Host "Add selenium to CLASSPATH..." setx "CLASSPATH" "$($seleniumBinPath);$($env:CLASSPATH)" /M +Write-Host "CLASSPATH: $($seleniumBinPath);$($env:CLASSPATH)" exit 0 From 347d8dd3b6a22fbe84c1687250528ddc720d0ddd Mon Sep 17 00:00:00 2001 From: Andy Mishechkin Date: Fri, 20 Dec 2019 14:36:00 +0400 Subject: [PATCH 08/16] Post-review code modification --- .../win/scripts/Installers/Install-Chrome.ps1 | 71 ++++++------------- .../Installers/Install-SeleniumWebDrivers.ps1 | 36 ++++++---- 2 files changed, 44 insertions(+), 63 deletions(-) diff --git a/images/win/scripts/Installers/Install-Chrome.ps1 b/images/win/scripts/Installers/Install-Chrome.ps1 index 970e56d11..1e4239b10 100644 --- a/images/win/scripts/Installers/Install-Chrome.ps1 +++ b/images/win/scripts/Installers/Install-Chrome.ps1 @@ -8,20 +8,18 @@ function Stop-SvcWithErrHandling [Parameter(Mandatory, ValueFromPipeLine = $true)] [string] $ServiceName ) - 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"; + if (-not $Service) { + Write-Error "[!] Service [$ServiceName] is not found"; + exit 1; } else { - Write-Debug "Try to stop service [$ServiceName]"; + Write-Host "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"; + Write-Host "Service [$ServiceName] has been stopped successfuly"; } catch { Write-Error "[!] Failed to stop service [$ServiceName] with error:" @@ -29,9 +27,6 @@ function Stop-SvcWithErrHandling } } } - End { - Write-Debug "Function [Stop-SvcWithErrHnadlig] is stopped"; - } } function Set-SvcWithErrHandling @@ -40,12 +35,10 @@ function Set-SvcWithErrHandling [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) { + if (-not $Service) { Write-Warning "[!] Service [$ServiceName] is not found"; } try { @@ -56,62 +49,42 @@ function Set-SvcWithErrHandling $_ | Out-String | Write-Error; } } - End { - Write-Debug "Function [Stop-SvcWithErrHnadlig] is stopped"; - } -} - -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"; - } } Import-Module -Name ImageHelpers -Force; $ChromeInstallerFile = "chrome_installer.exe"; -$ChromeInstallerUri = "https://dl.google.com/chrome/install/375.126/chrome_installer.exe"; +$ChromeInstallerUri = "https://dl.google.com/chrome/install/375.126/${ChromeInstallerFile}"; Install-Exe -Url $ChromeInstallerUri -Name $ChromeInstallerFile -ArgumentList ("/silent", "/install") -Write-Debug "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"; -('gupdate','gupdatem') | Stop-SvcWithErrHandling; -('gupdate','gupdatem') | Set-SvcWithErrHandling -Arguments @{StartupType = "Disabled"}; +$GoogleSvcs = ('gupdate','gupdatem'); +$GoogleSvcs | Stop-SvcWithErrHandling; +$GoogleSvcs | 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"; - } + New-Item -Path $_ -Force; } -$regGoogleUpdateParameters = @( +$regGoogleParameters = @( @{ Name = "AutoUpdateCheckPeriodMinutes"; Value = 00000000}, @{ Name = "UpdateDefault"; Value = 00000000 }, - @{ Name = "DisableAutoUpdateChecksCheckboxValuet"; Value = 00000000 }, - @{ Name = "Update{8A69D345-D564-463C-AFF1-A69D9E530F96}"; Value = 00000000 } + @{ Name = "DisableAutoUpdateChecksCheckboxValue"; Value = 00000001 }, + @{ Name = "Update{8A69D345-D564-463C-AFF1-A69D9E530F96}"; Value = 00000000 }, + @{ Path = $regGoogleUpdateChrome; Name = "DefaultBrowserSettingEnabled"; Value = 00000000 } ) -$regGoogleUpdateParameters | ForEach-Object { +$regGoogleParameters | ForEach-Object { $Arguments = $_; - $Arguments.Add("Path", $regGoogleUpdatePath); + if (-not ($Arguments.Path)) { + $Arguments.Add("Path", $regGoogleUpdatePath); + } $Arguments.Add("Force", $true); - New-ItemWithErrHandling -Arguments $Arguments + New-ItemProperty @Arguments; } -$Arguments = @{ Path = $regGoogleUpdateChrome; Name = "DefaultBrowserSettingEnabled"; Value = 00000000; Force = $true }; -New-ItemWithErrHandling -Arguments $Arguments; diff --git a/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 b/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 index 45e60d01e..93d9d1ade 100644 --- a/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 +++ b/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 @@ -3,40 +3,48 @@ ## Desc: Install Selenium Web Drivers ################################################################################ $DestinationPath = "C:\"; -Write-Debug "Destination path: [$DestinationPath]"; -Write-Debug "Selenium drivers download and install..."; -Invoke-WebRequest -UseBasicParsing -Uri "https://seleniumwebdrivers.blob.core.windows.net/seleniumwebdrivers/SeleniumWebDrivers.zip" -OutFile SeleniumWebDrivers.zip; -Expand-Archive -Path SeleniumWebDrivers.zip -DestinationPath $DestinationPath -Force; -Remove-Item SeleniumWebDrivers.zip; +$DriversZipFile = "SeleniumWebDrivers.zip" +Write-Host "Destination path: [$DestinationPath]"; +Write-Host "Selenium drivers download and install..."; +try { + Invoke-WebRequest -UseBasicParsing -Uri "https://seleniumwebdrivers.blob.core.windows.net/seleniumwebdrivers/${DriversZipFile}" -OutFile $DriversZipFile; +} +catch { + Write-Error "[!] Failed to download $DriverZipFile"; + exit 1; +} + +Expand-Archive -Path $DriversZipFile -DestinationPath $DestinationPath -Force; +Remove-Item $DriversZipFile; $ChromeDriverPath = "$DestinationPath\SeleniumWebDrivers\ChromeDriver"; -Write-Debug "Chrome driver path: [$ChromeDriverPath]"; +Write-Host "Chrome driver path: [$ChromeDriverPath]"; Remove-Item -Path "$ChromeDriverPath\*" -Force; $ChromePath = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(default)'; [version]$ChromeVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($ChromePath).ProductVersion; -Write-Debug "Chrome version: [$ChromeVersion]"; +Write-Host "Chrome version: [$ChromeVersion]"; $ChromeDriverVersionUri = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$($ChromeVersion.Major).$($ChromeVersion.Minor).$($ChromeVersion.Build)"; -Write-Debug "Chrome driver version Uri [$ChromeDriverVersionUri]"; -Write-Debug "Getting the Chrome driver version..."; +Write-Host "Chrome driver version Uri [$ChromeDriverVersionUri]"; +Write-Host "Getting the Chrome driver version..."; $ChromeDriverVersion = Invoke-WebRequest -Uri $ChromeDriverVersionUri; -Write-Debug "Current Chrome driver version: [$ChromeDriverVersion]"; +Write-Host "Current Chrome driver version: [$ChromeDriverVersion]"; $ChromeDriverZipDownloadUri = "https://chromedriver.storage.googleapis.com/$($ChromeDriverVersion.ToString())/chromedriver_win32.zip"; -Write-Debug "Chrome driver zip file download Uri: [$ChromeDriverZipDownloadUri]"; +Write-Host "Chrome driver zip file download Uri: [$ChromeDriverZipDownloadUri]"; $DestFile= "$ChromeDriverPath\chromedriver_win32.zip"; $ChromeDriverVersion.Content | Out-File -FilePath "$ChromeDriverPath\versioninfo.txt" -Force; -Write-Debug "Chrome driver download...."; +Write-Host "Chrome driver download...."; Invoke-WebRequest -Uri $ChromeDriverZipDownloadUri -OutFile $DestFile; -Write-Debug "Chrome driver install...."; +Write-Host "Chrome driver install...."; Expand-Archive -Path "$ChromeDriverPath\chromedriver_win32.zip" -DestinationPath $ChromeDriverPath -Force; Remove-Item -Path "$ChromeDriverPath\chromedriver_win32.zip" -Force; -Write-Debug "Setting the environment variables"; +Write-Host "Setting the environment variables"; setx IEWebDriver "C:\SeleniumWebDrivers\IEDriver" /M; setx GeckoWebDriver "C:\SeleniumWebDrivers\GeckoDriver" /M; From b5bfd51bdf852a1d3765f117887a7984d683a261 Mon Sep 17 00:00:00 2001 From: Ivan Nosar Date: Fri, 20 Dec 2019 15:33:40 +0300 Subject: [PATCH 09/16] Rename `Windows2016-Azure` to `vs2017-Server2016-Azure` --- .../win/{Windows2016-Azure.json => vs2017-Server2016-Azure.json} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename images/win/{Windows2016-Azure.json => vs2017-Server2016-Azure.json} (100%) diff --git a/images/win/Windows2016-Azure.json b/images/win/vs2017-Server2016-Azure.json similarity index 100% rename from images/win/Windows2016-Azure.json rename to images/win/vs2017-Server2016-Azure.json From 7ad7e32149cedfcba23346157e52db73ef1d5883 Mon Sep 17 00:00:00 2001 From: Andy Mishechkin Date: Mon, 23 Dec 2019 17:41:24 +0400 Subject: [PATCH 10/16] 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"; From 80932d59cf1526a31fcf965832f5f92fc7e3851d Mon Sep 17 00:00:00 2001 From: Andy Mishechkin Date: Mon, 23 Dec 2019 18:46:01 +0400 Subject: [PATCH 11/16] Small modification - space after If --- images/win/scripts/ImageHelpers/InstallHelpers.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/images/win/scripts/ImageHelpers/InstallHelpers.ps1 b/images/win/scripts/ImageHelpers/InstallHelpers.ps1 index 788e60043..56fac941e 100644 --- a/images/win/scripts/ImageHelpers/InstallHelpers.ps1 +++ b/images/win/scripts/ImageHelpers/InstallHelpers.ps1 @@ -106,7 +106,7 @@ Switch for stopping the script and exit from PowerShell if one service is absent $Service = Get-Service $ServiceName -ErrorAction SilentlyContinue if (-not $Service) { Write-Warning "[!] Service [$ServiceName] is not found"; - if($StopOnError) { + if ($StopOnError) { exit 1; } } From 1afc2bffb48c3ae3e3508b0d9186d0c983dc57f9 Mon Sep 17 00:00:00 2001 From: Andy Mishechkin Date: Mon, 23 Dec 2019 18:53:28 +0400 Subject: [PATCH 12/16] Path to ImageHelper has been fixed --- images/win/scripts/Installers/Install-Chrome.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/images/win/scripts/Installers/Install-Chrome.ps1 b/images/win/scripts/Installers/Install-Chrome.ps1 index c730ac040..aac913e71 100644 --- a/images/win/scripts/Installers/Install-Chrome.ps1 +++ b/images/win/scripts/Installers/Install-Chrome.ps1 @@ -3,7 +3,7 @@ ## Desc: Install Google Chrome ################################################################################ -Import-Module -Name "D:\GitHub\Akvelon\Forks\virtual-environments\images\win\scripts\ImageHelpers\ImageHelpers.psm1" -Force; +Import-Module -Name ImageHelpers -Force; $ChromeInstallerFile = "chrome_installer.exe"; $ChromeInstallerUri = "https://dl.google.com/chrome/install/375.126/${ChromeInstallerFile}"; From 72c8000241d2ba8598e64c7fa96428e1bfa8f31a Mon Sep 17 00:00:00 2001 From: Andrey Mishechkin Date: Tue, 24 Dec 2019 12:50:10 +0400 Subject: [PATCH 13/16] Windows2019-Azure.json -> vs2019-Server2019-Azure.json --- .../win/{Windows2019-Azure.json => vs2019-Server2019-Azure.json} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename images/win/{Windows2019-Azure.json => vs2019-Server2019-Azure.json} (100%) diff --git a/images/win/Windows2019-Azure.json b/images/win/vs2019-Server2019-Azure.json similarity index 100% rename from images/win/Windows2019-Azure.json rename to images/win/vs2019-Server2019-Azure.json From 263fd30c2754e016d54b18a43dda0a715379884d Mon Sep 17 00:00:00 2001 From: Andrey Mishechkin Date: Wed, 25 Dec 2019 22:17:56 +0400 Subject: [PATCH 14/16] [C:\SeleniumWebDrivers\ChromeDriver] has been added to [Path] env variable --- .../{vs2017-Server2016-Azure.json => Windows2016-Azure.json} | 0 .../{vs2019-Server2019-Azure.json => Windows2019-Azure.json} | 0 images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 | 5 +++++ 3 files changed, 5 insertions(+) rename images/win/{vs2017-Server2016-Azure.json => Windows2016-Azure.json} (100%) rename images/win/{vs2019-Server2019-Azure.json => Windows2019-Azure.json} (100%) diff --git a/images/win/vs2017-Server2016-Azure.json b/images/win/Windows2016-Azure.json similarity index 100% rename from images/win/vs2017-Server2016-Azure.json rename to images/win/Windows2016-Azure.json diff --git a/images/win/vs2019-Server2019-Azure.json b/images/win/Windows2019-Azure.json similarity index 100% rename from images/win/vs2019-Server2019-Azure.json rename to images/win/Windows2019-Azure.json diff --git a/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 b/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 index 93d9d1ade..390e04111 100644 --- a/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 +++ b/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 @@ -50,5 +50,10 @@ setx IEWebDriver "C:\SeleniumWebDrivers\IEDriver" /M; setx GeckoWebDriver "C:\SeleniumWebDrivers\GeckoDriver" /M; setx ChromeWebDriver "C:\SeleniumWebDrivers\ChromeDriver" /M; +$regEnvKey = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\'; +$PathValue = Get-ItemPropertyValue -Path $reg -Name 'Path'; +$PathValue += ";C:\SeleniumWebDrivers\ChromeDriver"; +Set-ItemProperty -Path $regEnvKey -Name 'Path' -Value $PathValue; + exit 0; From ce965c849cce4192d3f3438be449f47cfb38bc81 Mon Sep 17 00:00:00 2001 From: Andrey Mishechkin Date: Wed, 25 Dec 2019 22:38:26 +0400 Subject: [PATCH 15/16] Bug in $PathValue has been fixed --- images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 b/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 index 390e04111..0c7e70671 100644 --- a/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 +++ b/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 @@ -51,8 +51,8 @@ setx GeckoWebDriver "C:\SeleniumWebDrivers\GeckoDriver" /M; setx ChromeWebDriver "C:\SeleniumWebDrivers\ChromeDriver" /M; $regEnvKey = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\'; -$PathValue = Get-ItemPropertyValue -Path $reg -Name 'Path'; -$PathValue += ";C:\SeleniumWebDrivers\ChromeDriver"; +$PathValue = Get-ItemPropertyValue -Path $regEnvKey -Name 'Path'; +$PathValue += ";C:\SeleniumWebDrivers\ChromeDriver\"; Set-ItemProperty -Path $regEnvKey -Name 'Path' -Value $PathValue; exit 0; From f72d0ae63c38dc05043b7be0e61943ccb21015be Mon Sep 17 00:00:00 2001 From: Andrey Mishechkin Date: Tue, 7 Jan 2020 16:04:14 +0400 Subject: [PATCH 16/16] Hardcoded 'C:\' has been replaced to '$($env:SystemDrive)\' --- images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 b/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 index 0c7e70671..1ea4fa84d 100644 --- a/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 +++ b/images/win/scripts/Installers/Install-SeleniumWebDrivers.ps1 @@ -2,7 +2,7 @@ ## File: Install-SeleniumWebDrivers.ps1 ## Desc: Install Selenium Web Drivers ################################################################################ -$DestinationPath = "C:\"; +$DestinationPath = "$($env:SystemDrive)\"; $DriversZipFile = "SeleniumWebDrivers.zip" Write-Host "Destination path: [$DestinationPath]"; Write-Host "Selenium drivers download and install...";