Skip to content

Commit

Permalink
Chrome Edge Detection
Browse files Browse the repository at this point in the history
  • Loading branch information
jayrodksmith authored Mar 8, 2024
1 parent 3ebbc0e commit 295460a
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 23 deletions.
1 change: 1 addition & 0 deletions .github/workflows/combine-scripts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches:
- main
- dev
- dev-edge
paths:
- 'Public/**'
- 'Private/**'
Expand Down
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

## [Unreleased]

# [0.9.9] - 08-03-2024

### Added

- Beta Edge surpport

### Changed

- WebDriver now checks if exists
- WebDriver now has check for Chrome/Edge

### Deprecated

### Removed

### Fixed

# [0.9.3] - 22-02-2024

### Added
Expand Down
2 changes: 1 addition & 1 deletion EasyWarrantyCheck.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'EasyWarrantyCheck.psm1'

# Version number of this module.
ModuleVersion = '0.9.3'
ModuleVersion = '0.9.9'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
142 changes: 139 additions & 3 deletions Private/Helpers/Get-WebDriver.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,148 @@ function Get-WebDriver {
param(
[Parameter(Mandatory = $false)]
[ValidateSet('Chrome', 'Edge')]
[String]$WebDriver = "Chrome"
[String]$WebDriver = "Chrome",

$registryRoot = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths", # root location in registry to check version of currently installed apps
$edgeRegistryPath = "$registryRoot\msedge.exe", # direct registry location for MS Edge (to check version)
$chromeRegistryPath = "$registryRoot\chrome.exe", # direct registry location for Chrome (to check version)
$webDriversPath = "C:\temp\EasyWarrantyCheck\WebDrivers", # local path for all web drivers (assuming that both are in the same location)
$edgeDriverPath = "$($webDriversPath)\msedgedriver.exe", # direct MS Edge driver path
$chromeDriverPath = "$($webDriversPath)\chromedriver.exe", # direct Chrome driver path
$chromeDriverWebsite = "https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json", # Chrome dooesn't allow to query the version from downloads page; instead available pages can be found here
$edgeDriverWebsite = "https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/" # URL to find and download relevant MS Edge Driver version
)

function Get-LocalDriverVersion{
param(
$pathToDriver # direct path to the driver
)
$processInfo = New-Object System.Diagnostics.ProcessStartInfo # need to pass the switch & catch the output, hence ProcessStartInfo is used

$processInfo.FileName = $pathToDriver
$processInfo.RedirectStandardOutput = $true # need to catch the output - the version
$processInfo.Arguments = "-v"
$processInfo.UseShellExecute = $false # hide execution

$process = New-Object System.Diagnostics.Process

$process.StartInfo = $processInfo
try {
$process.Start() | Out-Null
$process.WaitForExit() # run synchronously, we need to wait for result
$processStOutput = $process.StandardOutput.ReadToEnd()
}catch{
$version = "1.0.0.0"
}

if ($version -eq "1.0.0.0") {
return $version
}
elseif ($pathToDriver.Contains("msedgedriver")) {
return ($processStOutput -split " ")[3] # MS Edge returns version on 4th place in the output (be careful, in old versions it was on 1st as well)...
}
else {
return ($processStOutput -split " ")[1] # ... while Chrome on 2nd place
}
}

function Confirm-NeedForUpdate{
param(
$v1, # version 1 to compare
$v2 # version 2 to compare
)
return $v1.Substring(0, $v1.LastIndexOf(".")) -ne $v2.Substring(0, $v2.LastIndexOf(".")) # return true if update is needed, otherwise false. Ignore last minor version - it's not so important and can be skipped
}
# Create WebDrivers Location if not exist
try {
if (-not (Test-Path -Path $webDriversPath -PathType Container)) {
# Directory doesn't exist, create it
New-Item -Path $webDriversPath -ItemType Directory -Force | Out-Null
Write-Verbose "Directory created successfully."
} else {
Write-Verbose "Directory already exists."
}
} catch {
Write-Host "An error occurred: $_"
}

if($WebDriver -eq "Chrome"){
Get-WebDriverChrome
# Check which browser versions are installed (from registry)
try {
$chromeVersion = (Get-Item (Get-ItemProperty $chromeRegistryPath).'(Default)').VersionInfo.ProductVersion
} catch {

}
# check which driver versions are installed
$chromeDriverVersion = Get-LocalDriverVersion -pathToDriver $chromeDriverPath
if (Confirm-NeedForUpdate $chromeVersion $chromeDriverVersion){
$jsonString = Invoke-RestMethod -Uri $chromeDriverWebsite
# Find the URL for chromedriver for win64 platform in the stable channel
$webdriverurl = $jsonString.channels.Stable.downloads.chromedriver | Where-Object { $_.platform -eq "win64" } | Select-Object -ExpandProperty url
$chromeDriverAvailableVersions = $webdriverurl
$versionLink = $chromeDriverAvailableVersions | where {$_ -like "*$chromeVersion/*"}
if (!$versionLink){
$browserMajorVersion = $chromeVersion.Substring(0, $chromeVersion.IndexOf("."))
$versionLink = $chromeDriverAvailableVersions | where {$_ -like "*$browserMajorVersion.*"}
}
# in case of multiple links, take the first only
if ($versionLink.Count -gt 1){
$versionLink = $versionLink[0]
}
$downloadLink = $versionLink
try {
Invoke-WebRequest $downloadLink -OutFile "chromeNewDriver.zip"
}catch{

}
# Expand archive and replace the old file
Expand-Archive "chromeNewDriver.zip" -DestinationPath "chromeNewDriver\" -Force
Move-Item "chromeNewDriver/chromedriver-win64/chromedriver.exe" -Destination "$($webDriversPath)\chromedriver.exe" -Force

# clean-up
Remove-Item "chromeNewDriver.zip" -Force
Remove-Item "chromeNewDriver" -Recurse -Force
}
}
if($WebDriver -eq "Edge"){
Get-WebDriverEdge
# Check which browser versions are installed (from registry)
try {
$edgeVersion = (Get-Item (Get-ItemProperty $edgeRegistryPath).'(Default)').VersionInfo.ProductVersion
} catch {

}
# check which driver versions are installed
$edgeDriverVersion = Get-LocalDriverVersion -pathToDriver $edgeDriverPath
if($edgeDriverVersion -eq $null){
# Set version to nothing
$edgeDriverVersion = "1.0.0.0"
}
if (Confirm-NeedForUpdate $edgeVersion $edgeDriverVersion){
# find exact matching version
$edgeDriverAvailableVersions = (Invoke-RestMethod $edgeDriverWebsite) -split " " | where {$_ -like "*href=*win64*"} | % {$_.replace("href=","").replace('"','')}
$downloadLink = $edgeDriverAvailableVersions | where {$_ -like "*/$edgeVersion/*"}

# if cannot find (e.g. it's too new to have a web driver), look for relevant major version
if (!$downloadLink){
$browserMajorVersion = $edgeVersion.Substring(0, $edgeVersion.IndexOf("."))
$downloadLink = $edgeDriverAvailableVersions | where {$_ -like "*/$browserMajorVersion*"}
}

# in case of multiple links, take the first only
if ($downloadLink.Count -gt 1) {
$downloadLink = $downloadLink[0]
}

# download the file
Invoke-WebRequest $downloadLink -OutFile "edgeNewDriver.zip"

# epand archive and replace the old file
Expand-Archive "edgeNewDriver.zip" -DestinationPath "edgeNewDriver\" -Force
Move-Item "edgeNewDriver/msedgedriver.exe" -Destination "$($webDriversPath)\msedgedriver.exe" -Force

# clean-up
Remove-Item "edgeNewDriver.zip" -Force
Remove-Item "edgeNewDriver" -Recurse -Force
}
}
}
7 changes: 4 additions & 3 deletions Private/Helpers/Start-SeleniumModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ function Start-SeleniumModule {
[String]$WebDriver = "Chrome",

[Parameter(Mandatory = $false)]
[bool]$Headless = $true
[bool]$Headless = $true,

[Parameter(Mandatory = $false)]
[String]$WebDriverPath = "C:\temp\EasyWarrantyCheck\WebDrivers"
)
if($WebDriver -eq "Edge"){
$WebDriverPath = "C:\temp\edgedriver-win64"
$EdgeService = [OpenQA.Selenium.Edge.EdgeDriverService]::CreateDefaultService($WebDriverPath, 'msedgedriver.exe')
$EdgeService.HideCommandPromptWindow = $true
$EdgeService.UseVerboseLogging = $true
Expand All @@ -38,7 +40,6 @@ function Start-SeleniumModule {

}
if($WebDriver -eq "Chrome"){
$WebDriverPath = "C:\temp\chromedriver-win64"
$ChromeService = [OpenQA.Selenium.Chrome.ChromeDriverService]::CreateDefaultService($WebDriverPath, 'chromedriver.exe')
$ChromeService.HideCommandPromptWindow = $true
$chromeOptions = [OpenQA.Selenium.Chrome.ChromeOptions]::new()
Expand Down
10 changes: 9 additions & 1 deletion Public/Get-Warranty.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ function Get-Warranty {
[Parameter(Mandatory = $false, ParameterSetName = 'Default')]
[ValidateSet('NinjaRMM', 'None')]
[String]$RMM = 'NinjaRMM',


# Web Driver mode, Edge or Chrome ( Edge Beta Support )
[Parameter(Mandatory = $false, ParameterSetName = 'Default')]
[ValidateSet('Chrome', 'Edge')]
[String]$Seleniumdrivermode = 'Chrome',

# ServerMode, exclusive to CentralNinja but included in Default for consistency
[Parameter(Mandatory = $false, ParameterSetName = 'CentralNinja')]
[Switch]$ServerMode,
Expand Down Expand Up @@ -84,6 +89,9 @@ function Get-Warranty {
if ($ForceUpdate -eq $true) {
Set-Variable ForceUpdate -Value $ForceUpdate -Scope Global -option ReadOnly -Force
}
if ($Seleniumdrivermode) {
Set-Variable Seleniumdrivermode -Value $Seleniumdrivermode -Scope Global -option ReadOnly -Force
}
if ($PSCmdlet.ParameterSetName -eq 'Default') {
$machineinfo = Get-MachineInfo
if ($serial -eq 'Automatic') {
Expand Down
31 changes: 25 additions & 6 deletions Public/Get-WarrantyAsus.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,35 @@ function Get-WarrantyAsus {
)
Get-WebDriver -WebDriver $Seleniumdrivermode
Get-SeleniumModule
$googlechrome = Test-SoftwareInstalled -SoftwareName "Google Chrome"
if ($Seleniumdrivermode -eq "Chrome" ){
$browserinstalled = Test-SoftwareInstalled -SoftwareName "Google Chrome"
}
if ($Seleniumdrivermode -eq "Edge" ){
$browserinstalled = Test-SoftwareInstalled -SoftwareName "Microsoft Edge"
}
if ($browserinstalled.Installed -eq $false){
Write-Host "###########################"
Write-Host "WARNING"
Write-Host "$($browserinstalled.software) not detected"
Write-Host "This manufacturer currently requires $($browserinstalled.software) installed to check expiry"
Write-Host "###########################"
$WarObj = [PSCustomObject]@{
'Serial' = $Serial
'Warranty Product name' = $null
'StartDate' = $null
'EndDate' = $null
'Warranty Status' = 'Could not get warranty information'
'Client' = $null
'Product Image' = $null
'Warranty URL' = $null
}
Remove-Module Selenium
return $warObj
}
# Start a new browser session with headless mode
try{
$driver = Start-SeleniumModule -WebDriver $Seleniumdrivermode -Headless $true
}catch{
Write-Host "###########################"
Write-Host "WARNING"
Write-Host "Google Chrome not detected"
Write-Host "This manufacturer currently requires Google Chrome installed to check expiry"
Write-Host "###########################"
$WarObj = [PSCustomObject]@{
'Serial' = $Serial
'Warranty Product name' = $null
Expand Down
31 changes: 25 additions & 6 deletions Public/Get-WarrantyDell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,35 @@ function Get-WarrantyDell {
)
Get-WebDriver -WebDriver $Seleniumdrivermode
Get-SeleniumModule
$googlechrome = Test-SoftwareInstalled -SoftwareName "Google Chrome"
if ($Seleniumdrivermode -eq "Chrome" ){
$browserinstalled = Test-SoftwareInstalled -SoftwareName "Google Chrome"
}
if ($Seleniumdrivermode -eq "Edge" ){
$browserinstalled = Test-SoftwareInstalled -SoftwareName "Microsoft Edge"
}
if ($browserinstalled.Installed -eq $false){
Write-Host "###########################"
Write-Host "WARNING"
Write-Host "$($browserinstalled.software) not detected"
Write-Host "This manufacturer currently requires $($browserinstalled.software) installed to check expiry"
Write-Host "###########################"
$WarObj = [PSCustomObject]@{
'Serial' = $Serial
'Warranty Product name' = $null
'StartDate' = $null
'EndDate' = $null
'Warranty Status' = 'Could not get warranty information'
'Client' = $null
'Product Image' = $null
'Warranty URL' = $null
}
Remove-Module Selenium
return $warObj
}
# Start a new browser session with headless mode
try{
$driver = Start-SeleniumModule -WebDriver $Seleniumdrivermode -Headless $true
}catch{
Write-Host "###########################"
Write-Host "WARNING"
Write-Host "Google Chrome not detected"
Write-Host "This manufacturer currently requires Google Chrome installed to check expiry"
Write-Host "###########################"
$WarObj = [PSCustomObject]@{
'Serial' = $Serial
'Warranty Product name' = $null
Expand Down
11 changes: 8 additions & 3 deletions Public/Get-WarrantyHP.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ function Get-WarrantyHP {
[Parameter(Mandatory = $false)]
[String]$SystemSKU
)
Get-WebDriver -WebDriver $Seleniumdrivermode
Get-SeleniumModule
$googlechrome = Test-SoftwareInstalled -SoftwareName "Google Chrome"
Get-WebDriver -WebDriver $Seleniumdrivermode
Get-SeleniumModule
if ($Seleniumdrivermode -eq "Chrome" ){
$browserinstalled = Test-SoftwareInstalled -SoftwareName "Google Chrome"
}
if ($Seleniumdrivermode -eq "Edge" ){
$browserinstalled = Test-SoftwareInstalled -SoftwareName "Microsoft Edge"
}
# Start a new browser session with headless mode
try {
$driver = Start-SeleniumModule -WebDriver $Seleniumdrivermode -Headless $true
Expand Down

0 comments on commit 295460a

Please sign in to comment.