From c5fa538b42ed554f9404063783dfb02a516545c0 Mon Sep 17 00:00:00 2001 From: josephsmendoza Date: Fri, 12 Jul 2019 23:48:57 -0700 Subject: [PATCH] added search for adb function --- README.md | 2 +- SideQuest/install.ps1 | 54 -------------------------- install.ps1 | 90 ++++++++++++++++++++++++++++--------------- 3 files changed, 60 insertions(+), 86 deletions(-) delete mode 100644 SideQuest/install.ps1 diff --git a/README.md b/README.md index a31b66e..5acdac7 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/SideQuest/install.ps1 b/SideQuest/install.ps1 deleted file mode 100644 index 0d6e121..0000000 --- a/SideQuest/install.ps1 +++ /dev/null @@ -1,54 +0,0 @@ -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" -$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 -} -} -if($installPath.Length.Equals(0)){ -$installPath="$env:APPDATA\SideQuest\platform-tools" -} -} - -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") -} - -if(!$adb -and !$fastboot){ -$oldpath = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path -$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 -} -Copy-Item -Path "$env:temp\platform-tools\*" -Destination "$installPath\" -Force - -$PSScriptRootLegacy=split-path -parent $MyInvocation.MyCommand.Definition -Start-Process $PSScriptRootLegacy/android_winusb.inf -Verb Install - -$shell.Popup("Done!") \ No newline at end of file diff --git a/install.ps1 b/install.ps1 index 4cbae36..1652d57 100644 --- a/install.ps1 +++ b/install.ps1 @@ -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!") \ No newline at end of file