Skip to content

Commit

Permalink
added search for adb function
Browse files Browse the repository at this point in the history
  • Loading branch information
sugoidogo committed Jul 13, 2019
1 parent 32d0f1c commit c5fa538
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 86 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This installer requires an internet connection.

The main component is [the PowerShell script](https://github.com/josephsmendoza/ADB-Installer/blob/master/install.ps1) which downloads [the latest android platform tools for windows](https://dl.google.com/android/repository/platform-tools-latest-windows.zip) and installs them to either `C:\android-platform-tools`, an auto-detected previous install location, or a path you can specify via `-installPath`.

For conveinence, this is wrapped in an `.exe` file which runs the script with `ExecutionPolicy` set to `Bypass`. This file is fully automated.
For convenience, this is wrapped in an `.exe` file which runs the script with `ExecutionPolicy` set to `Bypass`. This file is fully automated.

Icon is from [Google via icon-icons.com](https://icon-icons.com/icon/adb/90476)

Expand Down
54 changes: 0 additions & 54 deletions SideQuest/install.ps1

This file was deleted.

90 changes: 59 additions & 31 deletions install.ps1
Original file line number Diff line number Diff line change
@@ -1,51 +1,79 @@
Param( [Parameter(Position=0)] [String[]] $installPath )
Set-PSDebug -Trace 2
$ErrorActionPreference = 'Inquire'
$localZip="$env:temp\platform-tools-latest-windows.zip"
$remoteZip="https://dl.google.com/android/repository/platform-tools-latest-windows.zip"
$adbUrl = "https://dl.google.com/android/repository/platform-tools-latest-windows.zip"
$adbZip = "$env:temp\platform-tools-latest-windows.zip"
$adbProcess = Get-Process -Name adb -ErrorAction SilentlyContinue
$shell = New-Object -ComObject Wscript.Shell
$application= New-Object -ComObject shell.application
$webClient = New-Object Net.WebClient
$adb=Get-Command "adb" -ErrorAction SilentlyContinue
$fastboot=Get-Command "fastboot" -ErrorAction SilentlyContinue

if($installPath.Length.Equals(0)){
if(!$adb -and $fastboot){
$installPath=fastboot --version | Select-String -Pattern "(?<=installed as )(.+)(?=\\.*\.exe)" | % { $_.Matches } | % { $_.Value }
}
if($adb){
$installPath=adb --version | Select-String -Pattern "(?<=installed as )(.+)(?=\\.*\.exe)" | % { $_.Matches } | % { $_.Value }
Invoke-Command -ScriptBlock {
$ErrorActionPreference = 'Ignore'
adb kill-server
}
$fastboot = Get-Command "fastboot" -ErrorAction SilentlyContinue

$adbDownloadJob = Start-Job -ScriptBlock {
$ErrorActionPreference = 'Inquire'
Write-Verbose "Start-BitsTransfer -Source $using:adbURL -Destination $using:adbZip" -Verbose
Start-BitsTransfer -Source $using:adbURL -Destination $using:adbZip
}
if($installPath.Length.Equals(0)){
$installPath="C:\android-platform-tools"

$adbSearchJob = Start-Job -ScriptBlock {
$ErrorActionPreference = 'Inquire'
Get-ChildItem -Path $env:SystemDrive\ -Include "adb.exe","fastboot.exe" -Exclude $env:SystemRoot\*,(Split-Path -Path $HOME)\* -ErrorAction SilentlyContinue -Recurse | ForEach-Object {Split-Path -Path $_.FullName} | Select-Object -Unique
}

$adbUserSearchJob = Start-Job -ScriptBlock {
$ErrorActionPreference = 'Inquire'
Get-ChildItem -Path $HOME\ -Include "adb.exe","fastboot.exe" -Exclude $env:temp\* -ErrorAction SilentlyContinue -Recurse -Force | ForEach-Object {Split-Path -Path $_.FullName} | Select-Object -Unique
}

if ( (cmd /c sc query Windefend) -like "*RUNNING*" ){
Add-MpPreference -ExclusionPath $installPath
Add-MpPreference -ExclusionProcess "adb.exe"
if($adbProcess){
$adb=$adbProcess.Path
$adbProcess.Kill()
} else {
$shell.Popup("Windows Defender is not running!
If you have antivirus, exclude $installPath")
$adb=Get-Command "adb" -ErrorAction SilentlyContinue
}

if(!$installPath){
if(!$adb -and $fastboot){
$installPath=&$fastboot --version | Select-String -Pattern "(?<=installed as )(.+)(?=\\.*\.exe)" | % { $_.Matches } | % { $_.Value }
}
if($adb){
$installPath=&$adb --version | Select-String -Pattern "(?<=installed as )(.+)(?=\\.*\.exe)" | % { $_.Matches } | % { $_.Value }
}
if(!$installPath){
$installPath="C:\android-platform-tools"
if($shell.Popup("automatic adb locating failed, Would you like to search for a previously downloaded adb? This process can take a long time, depending on how fast your storage device is",
0,'ADB Installer',36) -eq 6){
$result=@($installPath)
$result+=Receive-Job -Job $adbUserSearchJob -Wait -AutoRemoveJob
$result+=Receive-Job -Job $adbSearchJob -Wait -AutoRemoveJob
$installPath=($result | Out-GridView -OutputMode Single)
} else {
Remove-Job $adbSearchJob -Force
Remove-Job $adbUserSearchJob -Force
}
}
}

if(!$adb -and !$fastboot){
$oldpath = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path
if( !$oldpath.Contains($installPath) ){
$newpath = "$oldpath;$installPath"
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newPath
}

$webClient.DownloadFile($remoteZip,$localZip)
foreach ( $item in $application.NameSpace($localZip).items() ) {
$application.NameSpace("$env:temp").CopyHere($item,16)
}
if(-Not (Test-Path $installPath)){
mkdir $installPath
if ( (cmd /c sc query Windefend) -like "*RUNNING*" ){
Add-MpPreference -ExclusionPath $installPath
Add-MpPreference -ExclusionProcess "adb.exe"
} else {
$shell.Popup("Windows Defender is not running! If you have antivirus, exclude $installPath")
}

Receive-Job -Job $adbDownloadJob -Wait -AutoRemoveJob
Start-Job -ScriptBlock {
$ErrorActionPreference = 'Inquire'
Write-Verbose "Expand-Archive -Path $using:adbZip -DestinationPath $env:temp -Force" -Verbose
Expand-Archive -Path $using:adbZip -DestinationPath $env:temp -Force
Remove-Item $using:adbZip -Verbose
} | Receive-Job -Wait -AutoRemoveJob
if(!(Test-Path $installPath)){mkdir $installPath}
Copy-Item -Path "$env:temp\platform-tools\*" -Destination "$installPath\" -Force
Remove-Item -Path "$env:temp\platform-tools" -Recurse -Force

$shell.Popup("Done!")

0 comments on commit c5fa538

Please sign in to comment.