# set the path of web browser executable $browser = Get-Item "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" $browserVersion = [version]($browser).VersionInfo.ProductVersion # define the web driver download url $updateURI = "https://msedgedriver.azureedge.net/$browserVersion/edgedriver_win64.zip" # define the current web driver location $outPath = "D:\Selenium\drivers\edge_driver" $driverFile = "msedgedriver.exe" $webDriver = "$outPath\$driverFile" # define the log settings $logName = "Application" $logSource = "WebDriver Update" $EvtIDOK = 5050 $EvtIDError = 5051 # create log source if it doesn't exists if ([System.Diagnostics.EventLog]::SourceExists($logSource) -eq $False) { New-EventLog -LogName $logName -Source $logSource } # determining the curent web driver version $webDriverVersion = [version](Get-Item $webDriver -ea 0).VersionInfo.ProductVersion $webDriverVersionStr = (Get-Item $webDriver -ea 0).VersionInfo.ProductVersion # comparing the version of web browser and web driver if ($browserVersion -eq $webDriverVersion){ Write-EventLog -LogName $logName -Source $logSource -EntryType Information -EventId $EvtIDOK -Message "Web browser and web drivers are already running on same version ($webDriverVersionStr)..." }else { # check and kill the web driver process if running $webDriverPs = Get-Process "msedgedriver" -ea 0 if ($webDriverPs) { # try closing gracefully first $webDriverPs.CloseMainWindow() # kill after five seconds Sleep 5 if (!$webDriverPs.HasExited) { $webDriverPs | Stop-Process -Force } } # downloading the updated driver zip file if versions do not macth Write-EventLog -LogName $logName -Source $logSource -EntryType Information -EventId $EvtIDOK -Message "Web browser and web driver versions are not same. Downloading the updated drivers..." Invoke-WebRequest $updateURI -OutFile "$outPath\webDriver.zip" $webDriverZip = "$outPath\webDriver.zip" if($webDriverZip){ # extracting the web driver executable from zip archive Write-EventLog -LogName $logName -Source $logSource -EntryType Information -EventId $EvtIDOK -Message "Extracting the updated web driver executables..." Expand-Archive -Path $webDriverZip -DestinationPath $outPath -Force } # determining the updated web drivers version $driverFile1 = "msedgedriver.exe" $webDriver1 = "$outPath\$driverFile1" $webDriverVersion1 = [version](Get-Item $webDriver1 -ea 0).VersionInfo.ProductVersion $webDriverVersion1Str = (Get-Item $webDriver1 -ea 0).VersionInfo.ProductVersion if ($webDriverVersion1 -eq $browserVersion) { Write-EventLog -LogName $logName -Source $logSource -EntryType Information -EventId $EvtIDOK -Message "Web drivers were sucessfully updated to version ($webDriverVersion1Str)..." } else { Write-EventLog -LogName $logName -Source $logSource -EntryType Error -EventId $EvtIDError -Message "Something went wrong. Please try again later!" } # removing the downloaded web drivers zip file Write-EventLog -LogName $logName -Source $logSource -EntryType Information -EventId $EvtIDOK -Message "Removing the web drivers zip file..." Remove-Item $webDriverZip -Force }