PowerShell performance issue
I am new to powershell. I use the following powershell script to copy file from a network share, but the time cost is ridiculously long compared to a traditional windows batch file. What could b开发者_JAVA技巧e the cause?
$dlls=get-childitem -path "\\myShare\myBinFolder" -include *.dll -recurse
copy-item $dlls -destination c:\bins
Thanks
Update - 1 - 1:38 PM 1/13/2011
Why Get-ChildItem is So Slow?
http://blogs.msdn.com/b/powershell/archive/2009/11/04/why-is-get-childitem-so-slow.aspx
Do not use the Include
parameter. Use the Filter
parameter instead. Include will require every file to be returned from the share and filtered locally. Using Filter
should allow the filtering to happen on the remote end.
$dlls = Get-ChildItem -Path "\\myShare\myBinFolder" -Filter *.dll -recurse
or using positional feature of these parameters:
$dlls = Get-ChildItem \\myShare\myBinFolder *.dll -r
In fact, the only time I would ever use Include
over Filter
is if I needed to specify multiple filter terms (Include takes a string array) e.g.:
Get-ChildItem . -inc *.h,*.cpp,*.rc -r
One way to optimize this is to avoid assigning it to a variable. Try this
Get-ChildItem *.dll -Path \\Myshare\Folder -recurse | % { Copy-item $_.FullName -destination C:\bins }
You can use Measure-Command to measure how much time these two methods are taking. You can do that by:
(Measure-Command { Get-ChildItem *.dll -Path \\Myshare\Folder -recurse | % { Copy-item $_.FullName -destination C:\bins } }).Milliseconds
and
(Measure-Command {$dlls = Get-ChildItem *.dll -Path \\Myshare\Folder -recurse; copy-item $dlls -Destination C:\bins}).Milliseconds
All you really need from the remote system is a list of the full paths to the .dll files in that share. Get-childitem is overkill for that, and has known issues working with large directory structures remotely.
See if this isn't a lot quicker:
cmd /c dir \\Myshare\Folder\*.dll /b /s |% {Copy-Item $_ -destination C:\bins}
Note: the double backslash in the UNC is showing up as a single in the post.
How do I fix that?
精彩评论