【发布时间】:2020-03-26 22:38:53
【问题描述】:
我是 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() 给了我
System.__ComObject
如何获取文件列表并遍历该对象?
【问题讨论】:
-
您使用的是哪个安卓版本?您使用的是什么操作系统?什么 PowerShell 版本。为什么要经历这一切,然后将手机安装为驱动器并正常复制?网上有几篇文章分步说明了如何做到这一点。
-
Android 9,windows 10 pro 1909,powershell 版本 - 5.1.18362.628,谢谢你的信息,我会试试那个方法。
标签: powershell mtp