-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeckodriver.ps1
82 lines (73 loc) · 3.42 KB
/
geckodriver.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
$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
}