Search code examples
powershelladb

adb pull command in PowerShell works but throws an exception


When I use adb pull command to copy file from a device to pc in PowerShell, I get this:

PS>.\adb.exe pull '/sdcard/temp/screenshot.png'
.\adb.exe : 6040 KB/s (34027 bytes in 0.005s)
At line:1 char:1
+ .\adb.exe pull '/sdcard/temp/screenshot.png'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (6040 KB/s (34027 bytes in 0.005s):String) [ 
   ], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

The file is actually copied though.

The problem is that I want to use it in a loop, so when it throws an exception, script stops playing.

The same command in cmd works perfect.


Solution

  • It is common for exe's to send information down the error stream. I'm sure that is what you are seeing here as well. The "error" (read that like I used finger air quotes) is most likely coming from this line

    .\adb.exe : 6040 KB/s (34027 bytes in 0.005s)
    

    If you are familiar with psexec the initial banner of information is on the error stream.

    PSExec Error screen

    Since that data is on the error stream PowerShell reports it as such. As you have done in comments redirecting the error stream to null is an acceptable was to deal with that. 2>$null

    You could also experiment with Start-Process if you don't need the output returned.

    Start-Process adb.exe -ArgumentList "pull '/sdcard/temp/screenshot.png'"
    

    You just have to be careful if there actually is an error in either case.