PowerShell 复制失败且没有警告
你好,我正在尝试将文件从 IE 缓存复制到其他地方。这适用于 w7,但不适用于 Vista Ultimate。
简而言之:
copy-item $f -Destination "$targetDir" -force
(我也尝试过 $f.fullname)
完整脚本:
$targetDir = "C:\temp"
$ieCache=(get-itemproperty "hkcu:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders").cache
$minSize = 5mb
Write-Host "minSize:" $minSize
Add-Content -Encoding Unicode -Path $targetDir"\log.txt" -Value (get-Date)
Set-Location $ieCache
#\Low\Content.IE5 for protected mode
#\content.ie5 for unprotected
$a = Get-Location
foreach ($f in
(get-childitem -Recurse -Force -Exclude *.dat, *.tmp | where {$_.length -gt $minSize})
)
{
Write-Host (get-Date) $f.Name $f.length
Add-Content -Encoding Unicode -Path $targetDir"\log.txt" -Value $f.name, $f.length
copy-item $f -Destination "$targetDir" -force
}
智慧终结。请帮忙!
Howdy, am trying to copy a file from IE cache to somewhere else. This works on w7, but not Vista Ultimate.
In short:
copy-item $f -Destination "$targetDir" -force
(I also tried $f.fullname)
The full script:
$targetDir = "C:\temp"
$ieCache=(get-itemproperty "hkcu:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders").cache
$minSize = 5mb
Write-Host "minSize:" $minSize
Add-Content -Encoding Unicode -Path $targetDir"\log.txt" -Value (get-Date)
Set-Location $ieCache
#\Low\Content.IE5 for protected mode
#\content.ie5 for unprotected
$a = Get-Location
foreach ($f in
(get-childitem -Recurse -Force -Exclude *.dat, *.tmp | where {$_.length -gt $minSize})
)
{
Write-Host (get-Date) $f.Name $f.length
Add-Content -Encoding Unicode -Path $targetDir"\log.txt" -Value $f.name, $f.length
copy-item $f -Destination "$targetDir" -force
}
End of wisdom. Please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每当您在尝试找出参数在 PowerShell 中未正确绑定的原因时遇到问题,请使用 Trace-Command,如下所示:
在这种情况下,它对我有用。也许这是 PowerShell 2.0 的一个“功能”,但您可以看到它在触发之前尝试绑定几次不同的时间:
当 FileInfo 对象通过管道传递时,它们通常通过“PropertyName”绑定到 LiteralPath 。范围。我知道,您可能想知道,呃,不认为 System.IO.FileInfo 有 LiteralPath 属性。呵呵,其实不然。那些狡猾的 PowerShell 人员将 PSPath 别名偷偷添加到 LiteralPath 参数上,并且 PowerShell 会“调整”每个 FileInfo 对象以添加许多 PS* 属性,包括 PSPath。因此,如果您想“字面上”匹配您将使用的管道行为:
请注意,在这种情况下您不必引用 $targetDir (作为参数参数)。
Anytime you're having problems trying to figure out why a parameter doesn't bind correctly in PowerShell, use Trace-Command like so:
In this case, it works for me. Perhaps this is a "feature" of PowerShell 2.0 but you can see it attempt to bind several different times before it hits on:
The way this normally works when the FileInfo objects are passed via the pipeline, they bind by "PropertyName" to the LiteralPath parameter. I know, you're probably wondering, uh didn't think System.IO.FileInfo had a LiteralPath property. Heheh, it doesn't. Those tricky PowerShell folks snuck a PSPath alias onto the LiteralPath parameter and PowerShell "adapts" each the FileInfo object to add a number of PS* properties including PSPath. So if you wanted to "literally" match the pipelining behavior you would use:
Note that you don't have to quote $targetDir in this case (as a parameter argument).
copy-item
不起作用的原因是您将System.IO.FileInfo
作为参数-path
传递。有两种可能的正确方法:
copy-item -literal $f.Fullname -destination ...
$f | copy-item -destination ...
请注意,我使用参数
-literalPath
因为临时文件夹中的文件通常具有[
和]
在名称中充当通配符。如果您想知道为什么情况 #2 有效,请查看“help Copy-Item -Parameter Path”,您将看到接受管道输入? true(按值、按属性名称)。有关其含义的详细信息,请参阅 Keith 的电子书 Effective Windows PowerShell。
为什么你的版本不行?因为参数
-path
(位于位置 1)接受[string[]]
类型的输入,而不是FileInfo
类型。因此,PowerShell 尝试将其转换为[string]
(然后转换为数组),但它可能只使用Name
属性。您可以这样尝试:[string[]] (gci | select -first 1)
The reason why
copy-item
doesn't work is that you passSystem.IO.FileInfo
as parameter-path
.There are two possibilities how to do it correctly:
copy-item -literal $f.Fullname -destination ...
$f | copy-item -destination ...
Note that I use parameter
-literalPath
because files in the temp folder have usually[
and]
inside the name which acts as wildcards characters.If you wonder why case #2 works, have a look at 'help Copy-Item -Parameter Path`, you will see Accept pipeline input? true (ByValue, ByPropertyName). For more info what it means look at Keith's ebook Effective Windows PowerShell.
Why you version doesn't work? Because paramerer
-path
(which is at position 1) takes input of type[string[]]
and notFileInfo
. So PowerShell tried to convert it to[string]
(and then to the array) but it probably used onlyName
property. You can try it like this:[string[]] (gci | select -first 1)