问题描述
DIR
或 GCI
在 Powershell 中很慢,但在 CMD 中很快.有什么办法可以加快速度吗?
DIR
or GCI
is slow in Powershell, but fast in CMD. Is there any way to speed this up?
在 CMD.exe 中,在亚秒延迟后,这会以 CMD 窗口可以跟上的速度响应
In CMD.exe, after a sub-second delay, this responds as fast as the CMD window can keep up
dir \\remote-server.domain.com\share\folder\file*.*
在 Powershell (v2) 中,经过 40 秒以上的延迟后,响应速度明显变慢(可能每秒 3-4 行)
In Powershell (v2), after a 40+ second delay, this responds with a noticable slowness (maybe 3-4 lines per second)
gci \\remote-server.domain.com\share\folder\file*.*
我正在尝试扫描远程服务器上的日志,所以也许有更快的方法.
I'm trying to scan logs on a remote server, so maybe there's a faster approach.
get-childitem \\$s\logs -include $filemask -recurse | select-string -pattern $regex
推荐答案
这里 很好地解释了 Lee Holmes 对 Get-ChildItem 为什么慢的原因.如果您注意到页面底部Anon 11 Mar 2010 11:11 AM"的评论,他的解决方案可能适合您.
Here is a good explanation on why Get-ChildItem is slow by Lee Holmes. If you take note of the comment from "Anon 11 Mar 2010 11:11 AM" at the bottom of the page his solution might work for you.
Anon 的代码:
# SCOPE: SEARCH A DIRECTORY FOR FILES (W/WILDCARDS IF NECESSARY)
# Usage:
# $directory = "\\SERVER\SHARE"
# $searchterms = "filname[*].ext"
# PS> $Results = Search $directory $searchterms
[reflection.assembly]::loadwithpartialname("Microsoft.VisualBasic") | Out-Null
Function Search {
# Parameters $Path and $SearchString
param ([Parameter(Mandatory=$true, ValueFromPipeline = $true)][string]$Path,
[Parameter(Mandatory=$true)][string]$SearchString
)
try {
#.NET FindInFiles Method to Look for file
# BENEFITS : Possibly running as background job (haven't looked into it yet)
[Microsoft.VisualBasic.FileIO.FileSystem]::GetFiles(
$Path,
[Microsoft.VisualBasic.FileIO.SearchOption]::SearchAllSubDirectories,
$SearchString
)
} catch { $_ }
}
这篇关于如何通过 UNC 加速 Powershell Get-Childitem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!