Copy PowerShell without warning

Howdy, I'm trying to copy a file from the IE cache to another location. This works on w7, but not on Vista Ultimate.

In short:

copy-item $ f -Destination "$ targetDir" -force

(I also tried $ f.fullname)

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 } 

The end of wisdom. Please, help!

+4
source share
2 answers

Anytime you have trouble trying to figure out why a parameter doesn't bind correctly in PowerShell, use Trace-Command as follows:

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

In this case, it works for me. This may be a β€œfeature” of PowerShell 2.0, but you can see how it tries to relate several different times before it comes across:

 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] 

As usual, when the FileInfo objects are piped, they bind the PropertyName to the LiteralPath parameter. I know that you are probably wondering, did not think that System.IO.FileInfo has the LiteralPath property. Heheh, this is not so. These PowerShell tricky people scroll the PSPath alias over the LiteralPath parameter, and PowerShell β€œadapts” each FileInfo object to add several PS * properties, including PSPath. Therefore, if you want to "literally" match the pipelining behavior that you would use:

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

Note that in this case you do not need to specify $ targetDir (as a parameter argument).

+6
source

The reason copy-item not working is because you are passing System.IO.FileInfo as the -path parameter. There are two ways to do it right:

  • copy-item -literal $f.Fullname -destination ...
  • $f | copy-item -destination ...

Note that I use the -literalPath option because the files in the temp folder are usually [ and ] inside the name, which act as wildcard characters.

If you're wondering why Case # 2 works, take a look at "help Copy-Item-Parameter Path", will you see the message Accept concoming? true (ByValue, ByPropertyName). For more information on what this means, check out the Keith ebook Effective Windows PowerShell .

Why is the version not working? Since the -path parameter (which is at position 1) accepts input of type [string[]] , not FileInfo . Therefore, PowerShell tried to convert it to [string] (and then to an array), but it probably only used the Name property. You can try the following:

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

+3
source

Source: https://habr.com/ru/post/1304642/


All Articles