1

I am new to powershell scripting and kindly bear with my stupidity if any present. I am trying to write a script to backup selected folders from my Android phone, which shows up as a MTP device in Windows.Before copying for connecting I had found two pieces of code to connect and create a object (i think) for the phone.

Code 1

function Get-PhoneMainDir($phoneName)
{
  $o = New-Object -com Shell.Application
  $rootComputerDirectory = $o.NameSpace(0x11)
  $phoneDirectory = $rootComputerDirectory.Items() | Where-Object {$_.Name -eq $phoneName} | select -First 1

  if($phoneDirectory -eq $null)
  {
    throw "Not found '$phoneName' folder in This computer. Connect your phone."
  }

  return $phoneDirectory;
}
$phoneName = "ONEPLUS A3003"
$phoneRootDir = Get-PhoneMainDir $phoneName
Write-Host $phoneRootDir

Code 2

function Get-Phone
{
    param($phoneName)
    $shell = Get-ShellProxy
    # 17 (0x11) = ssfDRIVES from the ShellSpecialFolderConstants (https://msdn.microsoft.com/en-us/library/windows/desktop/bb774096(v=vs.85).aspx)
    # => "My Computer" — the virtual folder that contains everything on the local computer: storage devices, printers, and Control Panel.
    # This folder can also contain mapped network drives.
    $shellItem = $shell.NameSpace(17).self
    $phone = $shellItem.GetFolder.items() | where { $_.name -eq $phoneName }
    return $phone
}
function Get-ShellProxy
{
    if( -not $global:ShellProxy)
    {
        $global:ShellProxy = new-object -com Shell.Application
    }
    $global:ShellProxy
}
$phoneName ="ONEPLUS A3003"
$phone = Get-Phone -phoneName $phoneName
Write-Host $phone
Write-Host $phone.GetFolder.Items()

trying to print $phoneRootDir in code 1 as well as $phone and $phone.GetFolder.Items() gives me

System.__ComObject

how do I get list the files and traverse through with this object?

  • 1
    What Android version are you using? What OS are you using? What PowerShell version. Why go through all this, and just mount your phone as a drive and copy as normal? There are several articles on the web with step by step instructions on how to do that. – postanote Mar 26 '20 at 23:38
  • Android 9, windows 10 pro 1909, powershell version - 5.1.18362.628 , Thanks for the info , i'll try that method. – Kaushik Kyle Mar 27 '20 at 19:27
  • Anyone found a solution to why the code doesn't list all files unless you open the folder in Windows Explorer first? I've got the backup thing working, but without this issue solved, it's of no use. https://github.com/anandbibek/powershell-mtp-file-transfer – anandbibek Dec 11 '22 at 14:46

1 Answers1

0

To view more info about the COM Object, you can print it e.g. via Write-Host ($phone| Format-Table | Out-String)

You will then see a list of the properties of the object and can indeed navigate through the paths via object.GetFolder.items().

Matthias Muth
  • 71
  • 1
  • 1
  • 4