boi*_*ink 4 powershell copy-item
你好,我试图将文件从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
}
Run Code Online (Sandbox Code Playgroud)
智慧的终结.请帮忙!
无论何时您在尝试找出PowerShell中参数未正确绑定的原因时遇到问题,请使用Trace-Command,如下所示:
Trace-Command -Name ParameterBinding -expr { copy-item $f foo.cat.bak } -PSHost
Run Code Online (Sandbox Code Playgroud)
在这种情况下,它适合我.也许这是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]
Run Code Online (Sandbox Code Playgroud)
当FileInfo对象通过管道传递时,它通常的工作方式是,它们通过"PropertyName"绑定到LiteralPath参数.我知道,你可能想知道,呃不认为System.IO.FileInfo有一个LiteralPath属性.呵呵,它没有.那些棘手的PowerShell人员将一个PSPath别名隐藏在LiteralPath参数上,PowerShell"调整"每个FileInfo对象以添加许多PS*属性,包括PSPath.因此,如果您希望"逐字"匹配您将使用的流水线操作:
Copy-Item -LiteralPath $f.PSPath $targetDir -force
Run Code Online (Sandbox Code Playgroud)
请注意,在这种情况下,您不必引用$ targetDir(作为参数参数).
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。
为什么你的版本不行?因为 paramerer -path(位于位置 1)接受 type[string[]]而不是的输入FileInfo。因此,PowerShell 尝试将其转换为[string](然后转换为数组),但它可能只使用Name属性。你可以这样尝试:
[string[]] (gci | select -first 1)
| 归档时间: |
|
| 查看次数: |
2621 次 |
| 最近记录: |