-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNew-RemoteAndroidSession.ps1
324 lines (254 loc) · 11.2 KB
/
New-RemoteAndroidSession.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<#
.SYNOPSIS
This function starts a **fully controllable** **graphical** remote session with an android device
on the same network.
If your Android device *is* rooted, install the app 'ADB Wireless (root)' and launch it. Then, this
function will successfully connect
If your Android device *is not* rooted, install the app 'ADB Wireless (no root)', launch it, and
follow the instructions in the app. Then, this function will work.
This function currently only works on Windows 10.
.DESCRIPTION
See .SYNOPSIS
.NOTES
If you receive an error like...
ERROR: "adb push" returned with value 1
Press any key to continue...
...then temporarily connect your android device to your Windows machine via USB, select the appropriate
prompts on your Android device to have it trust your Windows machine, and then from Windows, run...
C:\Users\zeroadmin\Downloads\scrcpy-win64-v1.11> .\adb tcpip 5555
...then feel free to disconnect the USB cable and use this function as normal.
.PARAMETER IPOfAndroidDevice
This parameter is MANDATORY.
This parameter takes a string that represents the IP Address of the Android Device.
.PARAMETER ScrcpyPort
This parameter is OPTIONAL, but it has a default value of '5555'
This parameter takes a string that represents the Port Number that adbd is allowing TCPIP connections on.
.PARAMETER PathToScrcpyBinaries
This parameter is OPTIONAL.
This parameter takes a string that represents the full path to the directory that contains scrcpy.exe
and adb.exe.
This parameter should *not* be used with the -DownloadLatestScrcpy switch.
.PARAMETER DownloadLatestScrcpy
This parameter is OPTIONAL.
This parameter is a switch. If used, this function will download the latest win64 scrcpy binaries to
$HOME\Downloads\$ZipFileNameWithoutExtension and unzip them there.
This parameter should *not* be used with the PathToScrcpyBinaries parameter.
.EXAMPLE
# If you already have adb.exe and scrcpy.exe in a directory on your machine...
PS C:\Users\zeroadmin> New-RemoteAndroidSession -IPOfAndroidDevice "192.168.2.3" -PathToScrcpyBinaries "$HOME\Downloads\scrcpy-win64-v1.11"
.EXAMPLE
# If you already have adb.exe and scrcpy.exe in a directory on your machine and that directory is already
# part of your $env:PATH
PS C:\Users\zeroadmin> New-RemoteAndroidSession -IPOfAndroidDevice "192.168.2.3"
.EXAMPLE
# If you **do not** have adb.exe and scrcpy.exe in a directory on your machine...
PS C:\Users\zeroadmin> New-RemoteAndroidSession -IPOfAndroidDevice "192.168.2.3" -DownloadLatestScrcpy
#>
function New-RemoteAndroidSession {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$True)]
[string]$IPOfAndroidDevice,
[Parameter(Mandatory=$False)]
[string]$ScrcpyPort = "5555",
[Parameter(Mandatory=$False)]
[string]$PathToScrcpyBinaries,
[Parameter(Mandatory=$False)]
[switch]$DownloadLatestScrcpy
)
#region >> Helper Functions
function GetElevation {
if ($PSVersionTable.PSEdition -eq "Desktop" -or $PSVersionTable.Platform -eq "Win32NT" -or $PSVersionTable.PSVersion.Major -le 5) {
[System.Security.Principal.WindowsPrincipal]$currentPrincipal = New-Object System.Security.Principal.WindowsPrincipal(
[System.Security.Principal.WindowsIdentity]::GetCurrent()
)
[System.Security.Principal.WindowsBuiltInRole]$administratorsRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator
if($currentPrincipal.IsInRole($administratorsRole)) {
return $true
}
else {
return $false
}
}
if ($PSVersionTable.Platform -eq "Unix") {
if ($(whoami) -eq "root") {
return $true
}
else {
return $false
}
}
}
#endregion >> Helper Functions
#region >> Prep
if ($PSVersionTable.Platform -ne 'Win32NT' -and $PSVersionTable.PSEdition -ne 'Desktop') {
if ($PathToScrcpyBinaries) {
Write-Error "The -PathToScrcpyBinaries parameter should only be used on Windows systems. Halting!"
$global:FunctionResult = "1"
return
}
try {
$AptResult = Get-Command apt -ErrorAction Stop
}
catch {
Write-Error "Unable to find package manager 'apt'! Halting!"
$global:FunctionResult = "1"
return
}
try {
$SnapResult = Get-Command snap -ErrorAction Stop
}
catch {
Write-Error "Unable to find package manager 'snap'! Halting!"
$global:FunctionResult = "1"
return
}
# If we need to install/update adb or scrcpy, we need to be running this function as root
$NeedPackages = $False
try {
$ADBPackageCheck = Get-Command adb -ErrorAction Stop
}
catch {
$NeedPackages = $True
}
try {
$ScrcpyPackageCheck = Get-Command scrcpy -ErrorAction Stop
}
catch {
$NeedPackages = $True
}
if ($DownloadLatestScrcpy -or $NeedPackages) {
if (!$(GetElevation)) {
Write-Error "You must run PowerShell from an elevated prompt (i.e. 'sudo pwsh') in order to install/update adb and/or scrcpy! Halting!"
$global:FunctionResult = "1"
return
}
}
}
try {
$null = [ipaddress]$IPOfAndroidDevice
}
catch {
Write-Error $_
$global:FunctionResult = "1"
return
}
if ($PathToScrcpyBinaries -and $DownloadLatestScrcpy) {
Write-Error "Please use *either* the -PathToScrcpyBinaries parameter *or* the -DownloadLatestScrcpy parameter, not both. Halting!"
$global:FunctionResult = "1"
return
}
$IPAndPortString = $IPOfAndroidDevice + ':' + $ScrcpyPort
#endregion >> Prep
#region >> Main
if ($PSVersionTable.Platform -eq 'Win32NT' -or $PSVersionTable.PSEdition -eq 'Desktop') {
if ($DownloadLatestScrcpy) {
$releaseInfo = Invoke-RestMethod 'https://api.github.com/repos/Genymobile/scrcpy/releases/latest'
$target = $releaseInfo.assets | Where-Object {$_.Name.Contains('win64')}
$destination = Join-Path "$HOME\Downloads" -ChildPath $target.Name
$ExpansionDirectory = "$HOME\Downloads" + '\' + $($target.Name -replace '.zip')
if (Test-Path $destination) {
#$null = Remove-Item $destination -Recurse -Force -ErrorAction Stop
Write-Error "The download destination path '$destination' already exists! Please delete it and try again. Halting!"
$global:FunctionResult = "1"
return
}
try {
$IWRResult = Invoke-WebRequest -Uri $target.browser_download_url -OutFile $destination -ErrorAction Stop
}
catch {
Write-Error $_
$global:FunctionResult = "1"
return
}
try {
$ExpandArchiveResult = Expand-Archive -Path $destination -DestinationPath $ExpansionDirectory -ErrorAction Stop
}
catch {
Write-Error $_
$global:FunctionResult = "1"
return
}
$PathToScrcpyBinaries = $ExpansionDirectory
$ScrcpyPath = "$PathToScrcpyBinaries\scrcpy.exe"
$ADBPath = "$PathToScrcpyBinaries\adb.exe"
}
else {
if ($PathToScrcpyBinaries) {
if (! $(Test-Path $PathToScrcpyBinaries)) {
Write-Error "The path '$PathToScrcpyBinaries' was not found! Halting!"
$global:FunctionResult = "1"
return
}
if (! $(Test-Path "$PathToScrcpyBinaries\adb.exe")) {
Write-Error "The path '$PathToScrcpyBinaries\adb.exe' was not found! Halting!"
$global:FunctionResult = "1"
return
}
if (! $(Test-Path "$PathToScrcpyBinaries\scrcpy.exe")) {
Write-Error "The path '$PathToScrcpyBinaries\scrcpy.exe' was not found! Halting!"
$global:FunctionResult = "1"
return
}
#Set-Location $PathToScrcpyBinaries
$ADBPath = "$PathToScrcpyBinaries\adb.exe"
$ScrcpyPath = "$PathToScrcpyBinaries\scrcpy.exe"
}
else {
try {
$ADBCheck = Get-Command adb -ErrorAction Stop
}
catch {
Write-Error $_
$global:FunctionResult = "1"
return
}
try {
$ScrcpyCheck = Get-Command scrcpy -ErrorAction Stop
}
catch {
Write-Error $_
$global:FunctionResult = "1"
return
}
$ADBPath = $ADBCheck.Source
$ScrcpyPath = $ScrcpyCheck.Source
}
}
# At this point, we should have $ADBPath and $ScrcpyPath
if (!$ScrcpyPath) {
Write-Error "Failed to identify `$ScrcpyPath! Halting!"
$global:FunctionResult = "1"
return
}
if (!$ADBPath) {
Write-Error "Failed to identify `$ADBPath! Halting!"
$global:FunctionResult = "1"
return
}
# Disconnect any existing adb connections
# NOTE: $ADBDisconnectResult should contain the string 'disconnected everything'
$ADBDisconnectResult = & $ADBPath disconnect
# Create an adb connection to the Android Device
# NOTE: $ADBConnectResult should contain the string 'connected to $IPAndPortString'
$ADBConnectResult = & $ADBPath connect $IPAndPortString
# Fire Up scrcpy
$ScrcpyResult = & $ScrcpyPath --bit-rate 2M --max-size 800
}
if ($PSVersionTable.Platform -ne 'Win32NT' -and $PSVersionTable.PSEdition -ne 'Desktop') {
if ($DownloadLatestScrcpy -or $NeedPackages) {
$null = snap install scrcpy
$null = apt install adb
}
# Disconnect any existing adb connections
# NOTE: $ADBDisconnectResult should contain the string 'disconnected everything'
$ADBDisconnectResult = adb disconnect
# Create an adb connection to the Android Device
# NOTE: $ADBConnectResult should contain the string 'connected to $IPAndPortString'
$ADBConnectResult = adb connect $IPAndPortString
# Fire Up scrcpy
$ScrcpyResult = scrcpy --bit-rate 2M --max-size 800
}
$ScrcpyResult
#endregion >> Main
}