Post-review code modification

This commit is contained in:
Andy Mishechkin
2019-12-20 14:36:00 +04:00
parent 166982b4c4
commit 347d8dd3b6
2 changed files with 44 additions and 63 deletions

View File

@@ -8,20 +8,18 @@ function Stop-SvcWithErrHandling
[Parameter(Mandatory, ValueFromPipeLine = $true)] [string] $ServiceName [Parameter(Mandatory, ValueFromPipeLine = $true)] [string] $ServiceName
) )
Begin {
Write-Debug "Function [Stop-SvcWithErrHnadlig] is started";
}
Process { Process {
$Service = Get-Service $ServiceName -ErrorAction SilentlyContinue $Service = Get-Service $ServiceName -ErrorAction SilentlyContinue
if(-not $Service) { if (-not $Service) {
Write-Warning "[!] Service [$ServiceName] is not found"; Write-Error "[!] Service [$ServiceName] is not found";
exit 1;
} }
else { else {
Write-Debug "Try to stop service [$ServiceName]"; Write-Host "Try to stop service [$ServiceName]";
try { try {
Stop-Service -Name $ServiceName -Force; Stop-Service -Name $ServiceName -Force;
$Service.WaitForStatus("Stopped", "00:01:00"); $Service.WaitForStatus("Stopped", "00:01:00");
Write-Debug "Service [$ServiceName] has been stoppet successfuly"; Write-Host "Service [$ServiceName] has been stopped successfuly";
} }
catch { catch {
Write-Error "[!] Failed to stop service [$ServiceName] with error:" 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 function Set-SvcWithErrHandling
@@ -40,12 +35,10 @@ function Set-SvcWithErrHandling
[Parameter(Mandatory, ValueFromPipeLine = $true)] [string] $ServiceName, [Parameter(Mandatory, ValueFromPipeLine = $true)] [string] $ServiceName,
[Parameter(Mandatory)] [hashtable] $Arguments [Parameter(Mandatory)] [hashtable] $Arguments
) )
Begin {
Write-Debug "Function [Set-SvcWithErrHnadlig] is started";
}
Process { Process {
$Service = Get-Service $ServiceName -ErrorAction SilentlyContinue $Service = Get-Service $ServiceName -ErrorAction SilentlyContinue
if(-not $Service) { if (-not $Service) {
Write-Warning "[!] Service [$ServiceName] is not found"; Write-Warning "[!] Service [$ServiceName] is not found";
} }
try { try {
@@ -56,62 +49,42 @@ function Set-SvcWithErrHandling
$_ | Out-String | Write-Error; $_ | 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; Import-Module -Name ImageHelpers -Force;
$ChromeInstallerFile = "chrome_installer.exe"; $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") 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"; New-NetFirewallRule -DisplayName "BlockGoogleUpdate" -Direction Outbound -Action Block -Program "C:\Program Files (x86)\Google\Update\GoogleUpdate.exe";
('gupdate','gupdatem') | Stop-SvcWithErrHandling; $GoogleSvcs = ('gupdate','gupdatem');
('gupdate','gupdatem') | Set-SvcWithErrHandling -Arguments @{StartupType = "Disabled"}; $GoogleSvcs | Stop-SvcWithErrHandling;
$GoogleSvcs | Set-SvcWithErrHandling -Arguments @{StartupType = "Disabled"};
$regGoogleUpdatePath = "HKLM:\SOFTWARE\Policies\Google\Update"; $regGoogleUpdatePath = "HKLM:\SOFTWARE\Policies\Google\Update";
$regGoogleUpdateChrome = "HKLM:\SOFTWARE\Policies\Google\Chrome"; $regGoogleUpdateChrome = "HKLM:\SOFTWARE\Policies\Google\Chrome";
($regGoogleUpdatePath, $regGoogleUpdateChrome) | ForEach-Object { ($regGoogleUpdatePath, $regGoogleUpdateChrome) | ForEach-Object {
Write-Debug "Creation of [$_] registry key"; New-Item -Path $_ -Force;
try {
New-Item -Path $_ -Force;
}
catch {
Write-Warning "[!] Failed to create [$_] registry key";
}
} }
$regGoogleUpdateParameters = @( $regGoogleParameters = @(
@{ Name = "AutoUpdateCheckPeriodMinutes"; Value = 00000000}, @{ Name = "AutoUpdateCheckPeriodMinutes"; Value = 00000000},
@{ Name = "UpdateDefault"; Value = 00000000 }, @{ Name = "UpdateDefault"; Value = 00000000 },
@{ Name = "DisableAutoUpdateChecksCheckboxValuet"; Value = 00000000 }, @{ Name = "DisableAutoUpdateChecksCheckboxValue"; Value = 00000001 },
@{ Name = "Update{8A69D345-D564-463C-AFF1-A69D9E530F96}"; Value = 00000000 } @{ Name = "Update{8A69D345-D564-463C-AFF1-A69D9E530F96}"; Value = 00000000 },
@{ Path = $regGoogleUpdateChrome; Name = "DefaultBrowserSettingEnabled"; Value = 00000000 }
) )
$regGoogleUpdateParameters | ForEach-Object { $regGoogleParameters | ForEach-Object {
$Arguments = $_; $Arguments = $_;
$Arguments.Add("Path", $regGoogleUpdatePath); if (-not ($Arguments.Path)) {
$Arguments.Add("Path", $regGoogleUpdatePath);
}
$Arguments.Add("Force", $true); $Arguments.Add("Force", $true);
New-ItemWithErrHandling -Arguments $Arguments New-ItemProperty @Arguments;
} }
$Arguments = @{ Path = $regGoogleUpdateChrome; Name = "DefaultBrowserSettingEnabled"; Value = 00000000; Force = $true };
New-ItemWithErrHandling -Arguments $Arguments;

View File

@@ -3,40 +3,48 @@
## Desc: Install Selenium Web Drivers ## Desc: Install Selenium Web Drivers
################################################################################ ################################################################################
$DestinationPath = "C:\"; $DestinationPath = "C:\";
Write-Debug "Destination path: [$DestinationPath]"; $DriversZipFile = "SeleniumWebDrivers.zip"
Write-Debug "Selenium drivers download and install..."; Write-Host "Destination path: [$DestinationPath]";
Invoke-WebRequest -UseBasicParsing -Uri "https://seleniumwebdrivers.blob.core.windows.net/seleniumwebdrivers/SeleniumWebDrivers.zip" -OutFile SeleniumWebDrivers.zip; Write-Host "Selenium drivers download and install...";
Expand-Archive -Path SeleniumWebDrivers.zip -DestinationPath $DestinationPath -Force; try {
Remove-Item SeleniumWebDrivers.zip; 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"; $ChromeDriverPath = "$DestinationPath\SeleniumWebDrivers\ChromeDriver";
Write-Debug "Chrome driver path: [$ChromeDriverPath]"; Write-Host "Chrome driver path: [$ChromeDriverPath]";
Remove-Item -Path "$ChromeDriverPath\*" -Force; Remove-Item -Path "$ChromeDriverPath\*" -Force;
$ChromePath = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(default)'; $ChromePath = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(default)';
[version]$ChromeVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($ChromePath).ProductVersion; [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)"; $ChromeDriverVersionUri = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$($ChromeVersion.Major).$($ChromeVersion.Minor).$($ChromeVersion.Build)";
Write-Debug "Chrome driver version Uri [$ChromeDriverVersionUri]"; Write-Host "Chrome driver version Uri [$ChromeDriverVersionUri]";
Write-Debug "Getting the Chrome driver version..."; Write-Host "Getting the Chrome driver version...";
$ChromeDriverVersion = Invoke-WebRequest -Uri $ChromeDriverVersionUri; $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"; $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"; $DestFile= "$ChromeDriverPath\chromedriver_win32.zip";
$ChromeDriverVersion.Content | Out-File -FilePath "$ChromeDriverPath\versioninfo.txt" -Force; $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; 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; Expand-Archive -Path "$ChromeDriverPath\chromedriver_win32.zip" -DestinationPath $ChromeDriverPath -Force;
Remove-Item -Path "$ChromeDriverPath\chromedriver_win32.zip" -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 IEWebDriver "C:\SeleniumWebDrivers\IEDriver" /M;
setx GeckoWebDriver "C:\SeleniumWebDrivers\GeckoDriver" /M; setx GeckoWebDriver "C:\SeleniumWebDrivers\GeckoDriver" /M;