PowerShell 复制失败且没有警告

发布于 2024-08-26 02:42:40 字数 940 浏览 7 评论 0原文

你好,我正在尝试将文件从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〗斷ホ乔殘χμё〖 2024-09-02 02:42:40

每当您在尝试找出参数在 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 (作为参数参数)。

Anytime you're having problems trying to figure out why a parameter doesn't bind correctly in PowerShell, use Trace-Command like so:

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

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:

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]

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:

Copy-Item -LiteralPath $f.PSPath $targetDir -force

Note that you don't have to quote $targetDir in this case (as a parameter argument).

海未深 2024-09-02 02:42:40

copy-item 不起作用的原因是您将 System.IO.FileInfo 作为参数 -path 传递。
有两种可能的正确方法:

  1. copy-item -literal $f.Fullname -destination ...
  2. $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 pass System.IO.FileInfo as parameter -path.
There are two possibilities how to do it correctly:

  1. copy-item -literal $f.Fullname -destination ...
  2. $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 not FileInfo. So PowerShell tried to convert it to [string] (and then to the array) but it probably used only Name property. You can try it like this:

[string[]] (gci | select -first 1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文