Search code examples
powershellvariablesadb

Passing a powershell variable value to an adb shell command


Good day to all. After google-ing and trying various solutions, I'm a bit stuck with the following easy (at least it seemed so) task: I have a powershell variable, say:

$simpleString = "Hello World and stuff"

I'm testing an Android app via ADB, where I need to pass this variable's value as a string:

.\adb.exe shell input text "$simpleString"

I get error

.\adb.exe : Error: Invalid arguments for command: text

followed by reminder on how to use "input" command by adb.

Update: I've also tried the following workaround:

$myCmd = Write-output "adb.exe shell input text `"$simpleString`""

thus building a valid command for CMD and then run it via:

cmd /c $myCmd

but I still get same issue

Any help will be much appreciated, thank you.


Solution

  • I had this problem too and after looking at this post I eventually came up with a simpler answer. The issue for me was to pass double quotes (") to the adb shell command to enclose a message string for sms. I realized after much trial and error that the adb shell required backslash characters () to escape the double quotes, but the calling powershell script required backticks (`) to escape both the backslashes and double quotes. So my script code looked like this:-

            $messy = "`\`"Dear $Name , your appointment with XXX is at: " + ($_.Start.tostring("dd/MM/yyyy HH:mm")) + "`\`""
            adb.exe shell service call isms 5 i32 1 s16 com.android.mms s16 null s16 09090909090 s16 null s16 $messy s16 null s16 null i32 0 i64 0
    

    The backticks in the above string $messy will be stripped off by powershell, leaving the " to be passed to the adb shell, which will then strip off the \ leaving the quote around the message string.