Last active
January 31, 2023 02:37
-
-
Save SonOfDiablo/81f3d610295c69c777b512e4da90393d to your computer and use it in GitHub Desktop.
A script to download and unzip geckodriver, and then add it to the user path.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ProgressPreference='SilentlyContinue' | |
Write-Host "Finding OS Architecture: " -ForegroundColor DarkCyan -NoNewline | |
# Find Os Architecture | |
if((Get-ComputerInfo).OsArchitecture -eq "64-bit"){ | |
$arch = "*win64*" | |
Write-Host "64-bit" -ForegroundColor Green | |
}else{ | |
$arch = "*win32*" | |
Write-Host "32-bit" -ForegroundColor Green | |
} | |
Write-Host "Making Temp Directory: " -ForegroundColor DarkCyan -NoNewline | |
# Generate random 10 char string | |
$tmp = -join (((48..57) + (65..90) + (97..122)) * 120 | Get-Random -Count 10 | ForEach-Object {[char]$_}) | |
# Make temp directory | |
$tmpDir = "$env:TEMP\tmp.$tmp" | |
mkdir $tmpDir *>$null | |
Write-Host "$tmpDir" -ForegroundColor Green | |
Write-Host "Downloading zip: " -ForegroundColor DarkCyan -NoNewline | |
# Download zip | |
$tmpLocation = "$tmpDir\geckodriver.zip" | |
$downloadUrl = (((Invoke-WebRequest -Uri "https://api.github.com/repos/mozilla/geckodriver/releases/latest" -UseBasicParsing).content | ConvertFrom-Json).assets | Where-Object name -Like $arch).browser_download_url | |
Invoke-WebRequest -Uri $downloadUrl -OutFile $tmpLocation -UseBasicParsing | |
Write-Host "Done!" -ForegroundColor Green | |
Write-Host "Extracting exe: " -ForegroundColor DarkCyan -NoNewline | |
# Unzip | |
Expand-Archive -LiteralPath $tmpLocation -DestinationPath $tmpDir | |
Write-Host "Done!" -ForegroundColor Green | |
Write-Host "Checking if gecodriver already exists: " -ForegroundColor DarkCyan -NoNewline | |
$installDir = "$env:APPDATA\geckodriver" | |
$shouldCopy = $true | |
if(Test-Path "$installDir\geckodriver.exe"){ | |
Write-Host "gecodriver already exists" -ForegroundColor DarkYellow | |
Write-Host "Checking file hashes: " -ForegroundColor DarkCyan -NoNewline | |
if((Get-FileHash "$installDir\geckodriver.exe").hash -eq (Get-FileHash "$tmpDir\geckodriver.exe").hash){ | |
Write-Host "Hashes match!" -ForegroundColor DarkYellow | |
$shouldCopy = $false | |
}else{ | |
Write-Host "Hashes doesn't match" -ForegroundColor Green | |
Write-Host "Removing old exe: " -ForegroundColor DarkCyan -NoNewline | |
Remove-Item -LiteralPath "$installDir\geckodriver.exe" -Force | |
Write-Host "Done!" -ForegroundColor Green | |
} | |
}else{ | |
Write-Host "gecodriver doesn't exist" -ForegroundColor Green | |
Write-Host "Creating install directory in AppData: " -ForegroundColor DarkCyan -NoNewline | |
if(!(Test-Path $installDir)){ | |
mkdir $installDir *>$null | |
} | |
Write-Host "Done!" -ForegroundColor Green | |
} | |
if($shouldCopy){ | |
# Copy exe from temp directory to install directory | |
Write-Host "Copying exe: " -ForegroundColor DarkCyan -NoNewline | |
Copy-Item -LiteralPath "$tmpDir\geckodriver.exe" -Destination "$installDir\geckodriver.exe" | |
Write-Host "Done!" -ForegroundColor Green | |
} | |
Write-Host "Cleaning Up: " -ForegroundColor DarkCyan -NoNewline | |
# Clean Up | |
Remove-Item -LiteralPath $tmpDir -Force -Recurse | |
Write-Host "Done!" -ForegroundColor Green | |
Write-Host "Adding gecodriver to Path: " -ForegroundColor DarkCyan -NoNewline | |
# Adding gecodriver to Path | |
$userPath = [System.Environment]::GetEnvironmentVariable("Path", "User") | |
if($userPath -like "*;$installDir*"){ | |
Write-Host "gecodriver already in Path" -ForegroundColor DarkYellow | |
}else{ | |
[System.Environment]::SetEnvironmentVariable("Path", "$userPath;$installDir", "User") | |
Write-Host "Done!" -ForegroundColor Green | |
Write-Host "Updating session Path: " -ForegroundColor DarkCyan -NoNewline | |
# Update session Path | |
$env:Path = "$env:Path;$installDir" | |
Write-Host "Done!" -ForegroundColor Green | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ProgressPreference='SilentlyContinue' | |
if((Get-ComputerInfo).OsArchitecture -eq "64-bit"){ | |
$arch = "*win64*" | |
}else{ | |
$arch = "*win32*" | |
} | |
$tmp = -join (((48..57) + (65..90) + (97..122)) * 120 | Get-Random -Count 10 | ForEach-Object {[char]$_}) | |
$tmpDir = "$env:TEMP\tmp.$tmp" | |
mkdir $tmpDir *>$null | |
$tmpLocation = "$tmpDir\geckodriver.zip" | |
$downloadUrl = (((Invoke-WebRequest -Uri "https://api.github.com/repos/mozilla/geckodriver/releases/latest" -UseBasicParsing).content | ConvertFrom-Json).assets | Where-Object name -Like $arch).browser_download_url | |
Invoke-WebRequest -Uri $downloadUrl -OutFile $tmpLocation -UseBasicParsing | |
Expand-Archive -LiteralPath $tmpLocation -DestinationPath $tmpDir | |
$installDir = "$env:APPDATA\geckodriver" | |
$shouldCopy = $true | |
if(Test-Path "$installDir\geckodriver.exe"){ | |
if((Get-FileHash "$installDir\geckodriver.exe").hash -eq (Get-FileHash "$tmpDir\geckodriver.exe").hash){ | |
$shouldCopy = $false | |
}else{ | |
Remove-Item -LiteralPath "$installDir\geckodriver.exe" -Force | |
} | |
}else{ | |
if(!(Test-Path $installDir)){ | |
mkdir $installDir *>$null | |
} | |
} | |
if($shouldCopy){ | |
Copy-Item -LiteralPath "$tmpDir\geckodriver.exe" -Destination "$installDir\geckodriver.exe" | |
} | |
Remove-Item -LiteralPath $tmpDir -Force -Recurse | |
$userPath = [System.Environment]::GetEnvironmentVariable("Path", "User") | |
if($userPath -like "*;$installDir*"){ | |
}else{ | |
[System.Environment]::SetEnvironmentVariable("Path", "$userPath;$installDir", "User") | |
$env:Path = "$env:Path;$installDir" | |
} |
@Ohihahaha the two scripts does the same thing, Clean just doesn't have comments and output to console (It's less verbose).
It does look like the scripts ran correctly however :)
Windows 7 SP1 x64
PS 5.1
PS C:\Users\PC> Set-ExecutionPolicy RemoteSigned
Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic at
http://go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): y
PS C:\Users\PC> C:\81f3d610295c69c777b512e4da90393d-3bf37d72a678c5d3934355937e84e9928de4ad09\81f3d610295c69c777b512e4da9
0393d-3bf37d72a678c5d3934355937e84e9928de4ad09\download_geckodriver.ps1
Finding OS Architecture: Get-ComputerInfo : Unable to find an entry point named 'GetFirmwareType' in DLL 'kernel32.dll'.
At C:\81f3d610295c69c777b512e4da90393d-3bf37d72a678c5d3934355937e84e9928de4ad09\81f3d610295c69c777b512e4da90393d-3bf37d
72a678c5d3934355937e84e9928de4ad09\download_geckodriver.ps1:5 char:5
+ if((Get-ComputerInfo).OsArchitecture -eq "64-bit"){
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-ComputerInfo], EntryPointNotFoundException
+ FullyQualifiedErrorId : System.EntryPointNotFoundException,Microsoft.PowerShell.Commands.GetComputerInfoCommand
Making Temp Directory: C:\Users\PC\AppData\Local\Temp\tmp.QZUXAK4dHR
Downloading zip: Invoke-WebRequest : The request was aborted: Could not create SSL/TLS secure channel.
At C:\81f3d610295c69c777b512e4da90393d-3bf37d72a678c5d3934355937e84e9928de4ad09\81f3d610295c69c777b512e4da90393d-3bf37d
72a678c5d3934355937e84e9928de4ad09\download_geckodriver.ps1:24 char:19
+ ... oadUrl = (((Invoke-WebRequest -Uri "https://api.github.com/repos/mozi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Invoke-WebRequest : Cannot validate argument on parameter 'Uri'. The argument is null or empty. Provide an argument
that is not null or empty, and then try the command again.
At C:\81f3d610295c69c777b512e4da90393d-3bf37d72a678c5d3934355937e84e9928de4ad09\81f3d610295c69c777b512e4da90393d-3bf37d
72a678c5d3934355937e84e9928de4ad09\download_geckodriver.ps1:25 char:24
+ Invoke-WebRequest -Uri $downloadUrl -OutFile $tmpLocation -UseBasicPa ...
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Invoke-WebRequest], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Done!
Extracting exe: Expand-Archive : The path 'C:\Users\PC\AppData\Local\Temp\tmp.QZUXAK4dHR\geckodriver.zip' either does no
t exist or is
not a valid file system path.
At C:\81f3d610295c69c777b512e4da90393d-3bf37d72a678c5d3934355937e84e9928de4ad09\81f3d610295c69c777b512e4da90393d-3bf37d
72a678c5d3934355937e84e9928de4ad09\download_geckodriver.ps1:30 char:1
+ Expand-Archive -LiteralPath $tmpLocation -DestinationPath $tmpDir
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (C:\Users\PC\App...geckodriver.zip:String) [Expand-Archive], InvalidOpe
rationException
+ FullyQualifiedErrorId : ArchiveCmdletPathNotFound,Expand-Archive
Done!
Checking if gecodriver already exists: gecodriver doesn't exist
Creating install directory in AppData: Done!
Copying exe: Copy-Item : Cannot find path 'C:\Users\PC\AppData\Local\Temp\tmp.QZUXAK4dHR\geckodriver.exe' because it doe
s not exist.
At C:\81f3d610295c69c777b512e4da90393d-3bf37d72a678c5d3934355937e84e9928de4ad09\81f3d610295c69c777b512e4da90393d-3bf37d
72a678c5d3934355937e84e9928de4ad09\download_geckodriver.ps1:60 char:5
+ Copy-Item -LiteralPath "$tmpDir\geckodriver.exe" -Destination "$i ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\PC\App...geckodriver.exe:String) [Copy-Item], ItemNotFoundExce
ption
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Done!
Cleaning Up: Done!
Adding gecodriver to Path: gecodriver already in Path
@i486 I'm sorry this is script was written and tested on Windows 10.
I'm not sure if it works on Windows 7 and I don't have what it takes to test it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
first download_geckodriver_clean.ps1
then download_geckodriver.ps1