我需要一种方法来加速以下代码,以便在非常大的服务器上使用。我需要的只是全名参数的列表。我想使用如下内容:http: //newsqlblog.com/2012/05/22/concurrency-in-powershell-multi-threading-with-runspaces/
我正在阅读它,它看起来很简单,但是我无法让线程在现有池中创建新线程......以及如何传递该池。
任何帮助都会很棒,因为这样做的速度太可怕了,而且我有更多的资源,然后我正在使用。
Function Get-ChildItemToDepth {
Param(
[String]$Path = $PWD,
[int]$ToDepth = 255,
[Byte]$CurrentDepth = 0
)
if ($ToDepth -lt 0) {
return get-item $path
}
$CurrentDepth++
Get-ChildItem $Path | Where-Object { $_.PSIsContainer } | %{ $_
If ($CurrentDepth -le $ToDepth) {
Get-ChildItemToDepth -Path $_.FullName `
-ToDepth $ToDepth -CurrentDepth $CurrentDepth
}
}
}
注意:我仅限于 v2.0
摘要:我基本上需要以数字方式尽可能快地将所有文件夹的路径直到 $depth 放入一个数组中。
失败的尝试:
$func = `
{
Param(
[String]$Path = $PWD,
[int]$ToDepth = 255,
[Byte]$CurrentDepth = 0,
$p = $pool
)
if ($ToDepth -lt 0) {
return $path
}
$CurrentDepth++
$folders = Get-ChildItem $Path | Where-Object { $_.PSIsContainer } | select -expand FullName
If ($CurrentDepth -le $ToDepth) {
foreach ($path in $folders) {
$pipeline = [System.Management.Automation.PowerShell]::create()
$pipeline.RunspacePool = $pool
$pipeline.AddScript($func).AddArgument($path).AddArgument($ToDepth).AddArgument($CurrentDepth).AddArgument($pool)
$AsyncHandle = $pipeline.BeginInvoke()
$folders += $pipeline.EndInvoke($AsyncHandle)
$pipeline.Dispose()
}
}
return $folders
}
$path = "\\server\users-folder\"
$toDepth = 3
$pool = [RunspaceFactory]::CreateRunspacePool(1, 4)
$pool.ApartmentState = "STA"
$pool.Open()
$pipeline = [System.Management.Automation.PowerShell]::create()
$pipeline.RunspacePool = $pool
$pipeline.AddScript($func).AddArgument($path).AddArgument($toDepth).AddArgument($CurrentDepth).AddArgument($pool)
$AsyncHandle = $pipeline.BeginInvoke()
$RESULTS = $pipeline.EndInvoke($AsyncHandle)
$pipeline.Dispose()
$pool.Close()