你好,我正在尝试将文件从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
}
智慧的终结。请帮忙!
答案 0 :(得分:6)
如果您在尝试找出PowerShell中未正确绑定参数的原因时遇到问题,请使用Trace-Command,如下所示:
Trace-Command -Name ParameterBinding -expr { copy-item $f foo.cat.bak } -PSHost
在这种情况下,它适合我。也许这是PowerShell 2.0的“功能”,但你可以看到它在点击之前尝试绑定几个不同的时间:
COERCE arg to [System.String]
Trying to convert argument value from System.Management.Automation.PSObject to System.String
CONVERT arg type to param type using LanguagePrimitives.ConvertTo
CONVERT SUCCESSFUL using LanguagePrimitives.ConvertTo: [C:\Users\Keith\foo.cat]
当FileInfo对象通过管道传递时,这通常有效,它们通过“PropertyName”绑定到LiteralPath参数。我知道,你可能想知道,呃不认为System.IO.FileInfo有一个LiteralPath属性。呵呵,它没有。那些棘手的PowerShell人员将一个PSPath别名隐藏在LiteralPath参数上,PowerShell“调整”每个FileInfo对象以添加许多PS *属性,包括PSPath。因此,如果您希望“逐字地”匹配您将使用的流水线操作:
Copy-Item -LiteralPath $f.PSPath $targetDir -force
请注意,在这种情况下,您不必引用$ targetDir(作为参数参数)。
答案 1 :(得分:3)
copy-item
不起作用的原因是您将System.IO.FileInfo
作为参数-path
传递。
如何正确地完成它有两种可能性:
copy-item -literal $f.Fullname -destination ...
$f | copy-item -destination ...
请注意,我使用参数-literalPath
,因为临时文件夹中的文件通常在名称中[
和]
充当通配符字符。
如果你想知道为什么#2的情况有效,看看'帮助Copy-Item -Parameter Path`,你会看到接受管道输入? true(ByValue,ByPropertyName)。有关更多信息,请参阅Keith的电子书Effective Windows PowerShell。
为什么你的版本不起作用?因为参数-path
(位于第1位)的输入类型为[string[]]
而非FileInfo
。因此PowerShell尝试将其转换为[string]
(然后转换为数组),但它可能只使用Name
属性。你可以这样试试:
[string[]] (gci | select -first 1)