0

我需要一种方法来加速以下代码,以便在非常大的服务器上使用。我需要的只是全名参数的列表。我想使用如下内容: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()
4

2 回答 2

1

尝试在工作流中调用它 - PowerShell 3.0 开箱即用的强大本机 PowerShell 多线程解决方案

Workflow Invoke-Function
{
Function Get-ChildItemToDepth {   
    Param(     
        [String]$Path = $PWD,    
        [int]$ToDepth = 255,     
        [Byte]$CurrentDepth = 0
    ) 
    if ($ToDepth -lt 0) {
        return $path
    }
    $CurrentDepth++
    Get-ChildItem $Path | Where-Object { $_.PSIsContainer } | %{ $_ 
        If ($CurrentDepth -le $ToDepth) {
            Get-ChildItemToDepth -Path $_.FullName `
            -ToDepth $ToDepth -CurrentDepth $CurrentDepth
        } 
    }
}  
Get-ChildItemToDepth 
}

这是一个非常混乱的实现,我只是将您的函数和调用包装在工作流中,以便您可以将其复制粘贴到会话中以便于测试。请让我知道这对你有用。如果您想查看速度差异,我会将函数的结果传递给 Measure

Get-ChildItemToDepth | Measure
Invoke-Function | Measure

用法(将工作流粘贴到会话中之后)

Invoke-Function
于 2013-02-25T19:29:16.743 回答
0

转到http://psasync.codeplex.com并下载 psasync 模块(此处的文档http://newsqlblog.com/2012/12/17/psasync-module-multithreaded-powershell/)。然后 ...

Import-Module psasync

$AsyncPipelines = @()

# Allow 4 concurrent processes ... adjust up / down as hardware allows
$pool = Get-RunspacePool 4

# You can play with this to get better performance by going deeper into the directory structure to populate more dirs in the array
# gross generalization -> more dirs + more runspaces in $pool = faster performance

$rootDirs = Get-ChildItem c:\ | Where-Object{$_.PSIsContainer}

# Create a script block to run code per directory stored in $rootDirs
$ScriptBlock =  { ` 
    Param(     
        [String]$Path = $path
    ) 
    Get-ChildItem $path -recurse | Where-Object{$_.PSIsContainer}

}   

# Load up processes on the runspace pool
foreach($dir in $rootDirs) {
    $AsyncPipelines += Invoke-Async -RunspacePool $pool -ScriptBlock $ScriptBlock -Parameters $dir.FullName
}

# "Listen" to the pipelines and grab the results
$dirs = Receive-AsyncResults -Pipelines $AsyncPipelines

# PROFIT
$dirs | Foreach-Object{$_.FullName}

感谢您阅读 newsqlblog.com ;)

于 2013-02-27T00:35:55.260 回答