如何使用powershell访问MTP设备的内容



我是powershell脚本的新手,请耐心忍受我的愚蠢。我正试图写一个脚本来备份我的Android手机中选定的文件夹,它在Windows中显示为MTP设备。在复制连接之前,我找到了两段代码来连接并为手机创建一个对象(我认为(。

代码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

代码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()

尝试在代码1中打印$phoneRootDir以及$phone和$phone。GetFolder.Items((给了我

系统__ComObject

如何列出文件并遍历此对象?

要查看有关COM对象的更多信息,您可以打印它,例如通过Write-Host ($phone| Format-Table | Out-String)

然后,您将看到对象属性的列表,并且确实可以通过对象浏览路径。GetFolder.items((.

相关内容

  • 没有找到相关文章

最新更新