gpt4 book ai didi

PowerShell 复制失败且没有警告

转载 作者:行者123 更新时间:2023-12-02 22:12:56 25 4
gpt4 key购买 nike

你好,我正在尝试将文件从 IE 缓存复制到其他地方。这适用于 w7,但不适用于 Vista Ultimate。

简而言之:

复制项目 $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
}

智慧尽头。请帮忙!

最佳答案

每当您在尝试找出参数在 PowerShell 中无法正确绑定(bind)时遇到问题时,请像这样使用 Trace-Command:

Trace-Command -Name ParameterBinding -expr { copy-item $f foo.cat.bak } -PSHost

在这种情况下,它对我有用。也许这是 PowerShell 2.0 的一个“功能”,但您可以看到它在触发之前尝试绑定(bind)几个不同的时间:
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”绑定(bind)到 LiteralPath 参数。我知道,您可能想知道,呃,您认为 System.IO.FileInfo 没有 LiteralPath 属性。呵呵,不会的。那些狡猾的 PowerShell 人员将 PSPath 别名偷偷带入 LiteralPath 参数,并且 PowerShell “适应”每个 FileInfo 对象以添加许多 PS* 属性,包括 PSPath。因此,如果您想“从字面上”匹配您将使用的流水线行为:
Copy-Item -LiteralPath $f.PSPath $targetDir -force

请注意,在这种情况下,您不必引用 $targetDir(作为参数参数)。

关于PowerShell 复制失败且没有警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2482771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com