从网上找到的脚本:
#获取 Shell.Application 代理
function Get-ShellProxy{
if( -not $global:ShellProxy) {
$global:ShellProxy = new-object -com Shell.Application
}
$global:ShellProxy
}
#查看 Shell 项
function Get-ShellItem{
param($Path=17)
$shell=Get-ShellProxy
#默认的 NameSpace()方法只支持目录,不支持文件
#为了增强兼容性和保持一致性,如果传入一个文件路径,可以尝试通过文件路径获取目录,然后再从目录的子项中获取Shell项
trap [System.Management.Automation.MethodInvocationException]{
$Path = $Path.ToString()
$dir = $Path.Substring( 0 ,$Path.LastIndexOf(‘\’) )
return $shell.NameSpace($dir).items() |
where { (-not $_.IsFolder ) -and $_.path -eq $Path} |
select -First 1
continue
}
$shell.NameSpace($Path).self
}
#查看 Shell 子项,支持递归和过滤
function Get-ChildShellItem{
param(
$Path=17,
[switch]$Recurse,
$Filter=$null)
#内部过滤器
Filter myFilter{
if($Filter){
$_ | where { $_.Name -match $Filter }
}
else{
$_
}
}
$shellItem = Get-ShellItem $Path
$shellItem | myFilter
if( $shellItem.IsFolder ){ #如果是目录
if($Recurse) { #如果指定递归
$shellItem | myFilter
$stack=New-Object System.Collections.Stack
$stack.Push($shellItem) #当前目录压入堆栈
while($stack.Count -gt 0){
$top = $stack.Pop() #访问栈顶元素
$top | myFilter
#访问栈顶元素的子元素
$top.GetFolder.items() | foreach {
if( $_.IsFolder ){
$stack.Push($_)
}else{
$_ | myFilter
}
}
}
}else{
$shellItem.GetFolder.items() | myFilter }
}
}
#复制Shell项
function Copy-ShellItem{
param($Path,$Destination)
“Path=$Path”
“Destination=$Destination”
$shell=Get-ShellProxy
$shell.NameSpace($Destination).Copyhere($Path,16)
}
#删除Shell项
function Remove-ShellItem{
param($Path)
$ShellItem = Get-ShellItem $Path
$ShellItem.InvokeVerb(‘delete’)
}
#移动Shell项
function Move-ShellItem{
param($Path,$Destination)
$shell=Get-ShellProxy
$shell.NameSpace($Destination).MoveHere($Path,16)
}
将上述脚本拷贝到PowerShell中,然后执行执行脚本:
$DIRECTORY = Get-ChildShellItem | where { $_.name -eq ‘Honor 8 Lite’ }
Echo $DIRECTORY #能够正确显示
Echo $DIRECTORY.Path #能够正确显示
$FILE = Get-ChildShellItem -Path “$($DIRECTORY.Path)\SD 卡\” -Filter ‘(AppSignature.txt)$’
Echo $FILE.Path #能够正确显示
Copy-ShellItem -Path “$($FILE.Path)” -Destination “D:\DICM” #不报错,也拷贝不到文件夹D:\DICM中
请问是什么原因?怎么解决?谢谢!
这一段的输出是什么?
$FILE = Get-ChildShellItem -Path “$($DIRECTORY.Path)\SD 卡\” -Filter ‘(AppSignature.txt)$’
$FILE
Echo $FILE.Path #能够正确显示
下面是执行结果:
PS C:\Users\Administrator.USER-20160324JH> $DIRECTORY = Get-ChildShellItem | w
re { $_.name -eq ‘Honor 8 Lite’ }
PS C:\Users\Administrator.USER-20160324JH> Echo $DIRECTORY
Application : System.__ComObject
Parent : System.__ComObject
Name : Honor 8 Lite
Path : ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\\?\usb#vid_12d1&subcl
ss_ff&prot_00#6&1ae2ff4c&1&0000#{6ac27878-a6fa-4155-ba85-f98f49
d4f33}
GetLink :
GetFolder : System.__ComObject
IsLink : False
IsFolder : True
IsFileSystem : False
IsBrowsable : False
ModifyDate : 1899-12-30 0:00:00
Size : 0
Type : 便携式媒体播放器
PS C:\Users\Administrator.USER-20160324JH> Echo $DIRECTORY.Path
::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\\?\usb#vid_12d1&subclass_ff&prot_00#
&1ae2ff4c&1&0000#{6ac27878-a6fa-4155-ba85-f98f491d4f33}
PS C:\Users\Administrator.USER-20160324JH> $FILE = Get-ChildShellItem -Path “$
DIRECTORY.Path)\SD 卡\” -Filter ‘(AppSignature.txt)$’
PS C:\Users\Administrator.USER-20160324JH> Echo $FILE.Path
::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\\?\usb#vid_12d1&subclass_ff&prot_00#
&1ae2ff4c&1&0000#{6ac27878-a6fa-4155-ba85-f98f491d4f33}\SID-{5B8A0001,,7939817
72}\{000103F8-0001-5B8A-0000-000000000000}
PS C:\Users\Administrator.USER-20160324JH>
我是按照您写的《Powershell 编写和运行脚本》来写的,检测设备、显示路径都可以,就是文件拷贝没有结果。执行拷贝的显示如下:
PS C:\Users\Administrator.USER-20160324JH> Copy-ShellItem -Path “$($FILE.Path)”
-Destination “D:\DICM”
PS C:\Users\Administrator.USER-20160324JH>