Build status

Invoke-Parallel

This function will take in a script or scriptblock, and run it against specified objects(s) in parallel. It uses runspaces, as there are many situations where jobs or PSRemoting are not appropriate.

Instructions

# Download and unblock the file(s).
# Dot source the file.
    . "\\Path\To\Invoke-Parallel.ps1"


# Get help for the function
    Get-Help Invoke-Parallel -Full


# Use Invoke-Parallel with variables in your session

    $Number = 2
    1..10 | Invoke-Parallel -ImportVariables -ScriptBlock { $Number * $_ }


# Use the $Using Syntax, currently restricted to PowerShell v3 and later

    $Path = 'C:\temp\'

    'Server1', 'Server2' | Invoke-Parallel {

        #Create a log file for this server, use the root $Path
        $ThisPath = Join-Path $Using:Path "$_.log"
        "Doing something with $_" | Out-File -FilePath $ThisPath -Force

    }


# Import modules found in the current session

    #From https://psremoteregistry.codeplex.com/releases/view/65928
    Import-Module PSRemoteRegistry

    $ServerList | Invoke-Parallel -ImportModules -ScriptBlock {

        $Key = 'Software\Microsoft\Windows\CurrentVersion\Policies\System'
        Get-RegValue -ComputerName $_ -Hive LocalMachine -Key $Key |
            Select ComputerName, Value, Data

    }


# Want to time out items that take too long?

    1..5 | Invoke-Parallel -RunspaceTimeout 2 -ScriptBlock {

        "Starting $_"
        Start-Sleep -Seconds $_
        "If you see this, we didn't timeout $_"
    }


# Is one thread freezing up when you time it out, and preventing your scripting from moving on?

    $ServerList | Invoke-Parallel -RunspaceTimeout 10 -NoCloseOnTimeout -ScriptBlock {

            Get-WmiObject -Class Win32_OperatingSystem -ComputerName $_ | select -Property PSComputerName, Caption, Version

    }

Some outdated notes and details are available on the TechNet Galleries submission.

Help!

Would love contributors, suggestions, feedback, and other help! Split this out at the suggestion of @vors to help enable collaboration.

Notes



构建状态data-canonical-src

Invoke-Parallel

此函数将接收脚本或脚本块,并且并行对指定的对象运行它。它使用运行空间,因为有许多情况下,作业或PSRemoting不适合。

Instructions

# Download and unblock the file(s).
# Dot source the file.
    . "\Path\To\Invoke-Parallel.ps1"

# Get help for the function Get-Help Invoke-Parallel -Full

# Use Invoke-Parallel with variables in your session

<span class="pl-k">$</span><span class="pl-smi">Number</span> <span class="pl-k">=</span> <span class="pl-c1"><span class="pl-c1">2</span></span>
<span class="pl-c1"><span class="pl-c1">1</span></span><span class="pl-k">..</span><span class="pl-c1"><span class="pl-c1">10</span></span> <span class="pl-k">|</span> <span class="pl-c1">Invoke-Parallel</span> <span class="pl-k">-</span>ImportVariables <span class="pl-k">-</span>ScriptBlock { <span class="pl-k">$</span><span class="pl-smi">Number</span> <span class="pl-k">*</span> <span class="pl-k">$</span><span class="pl-c1">_</span> }

# Use the $Using Syntax, currently restricted to PowerShell v3 and later

<span class="pl-k">$</span><span class="pl-smi">Path</span> <span class="pl-k">=</span> <span class="pl-s">&#39;C:\temp\&#39;</span>

<span class="pl-s">&#39;Server1&#39;</span><span class="pl-k">,</span> <span class="pl-s">&#39;Server2&#39;</span> <span class="pl-k">|</span> <span class="pl-c1">Invoke-Parallel</span> {

    <span class="pl-c">#Create a log file for this server, use the root $Path</span>
    <span class="pl-k">$</span><span class="pl-smi">ThisPath</span> <span class="pl-k">=</span> <span class="pl-c1">Join-Path</span> <span class="pl-k">$</span><span class="pl-k">Using</span>:<span class="pl-smi">Path</span> <span class="pl-s">&#34;<span class="pl-k">$</span><span class="pl-c1">_</span>.log&#34;</span>
    <span class="pl-s">&#34;Doing something with <span class="pl-k">$</span><span class="pl-c1">_</span>&#34;</span> <span class="pl-k">|</span> <span class="pl-c1">Out-File</span> <span class="pl-k">-</span>FilePath <span class="pl-k">$</span><span class="pl-smi">ThisPath</span> <span class="pl-k">-</span>Force

}

# Import modules found in the current session

<span class="pl-c">#From https://psremoteregistry.codeplex.com/releases/view/65928</span>
<span class="pl-c1">Import-Module</span> PSRemoteRegistry

<span class="pl-k">$</span><span class="pl-smi">ServerList</span> <span class="pl-k">|</span> <span class="pl-c1">Invoke-Parallel</span> <span class="pl-k">-</span>ImportModules <span class="pl-k">-</span>ScriptBlock {

    <span class="pl-k">$</span><span class="pl-smi">Key</span> <span class="pl-k">=</span> <span class="pl-s">&#39;Software\Microsoft\Windows\CurrentVersion\Policies\System&#39;</span>
    <span class="pl-c1">Get-RegValue</span> <span class="pl-k">-</span>ComputerName <span class="pl-k">$</span><span class="pl-c1">_</span> <span class="pl-k">-</span>Hive LocalMachine <span class="pl-k">-</span>Key <span class="pl-k">$</span><span class="pl-smi">Key</span> <span class="pl-k">|</span>
        Select ComputerName<span class="pl-k">,</span> Value<span class="pl-k">,</span> <span class="pl-k">Data</span>

}

# Want to time out items that take too long?

<span class="pl-c1"><span class="pl-c1">1</span></span><span class="pl-k">..</span><span class="pl-c1"><span class="pl-c1">5</span></span> <span class="pl-k">|</span> <span class="pl-c1">Invoke-Parallel</span> <span class="pl-k">-</span>RunspaceTimeout <span class="pl-c1"><span class="pl-c1">2</span></span> <span class="pl-k">-</span>ScriptBlock {

    <span class="pl-s">&#34;Starting <span class="pl-k">$</span><span class="pl-c1">_</span>&#34;</span>
    <span class="pl-c1">Start-Sleep</span> <span class="pl-k">-</span>Seconds <span class="pl-k">$</span><span class="pl-c1">_</span>
    <span class="pl-s">&#34;If you see this, we didn&#39;t timeout <span class="pl-k">$</span><span class="pl-c1">_</span>&#34;</span>
}

# Is one thread freezing up when you time it out, and preventing your scripting from moving on?

<span class="pl-k">$</span><span class="pl-smi">ServerList</span> <span class="pl-k">|</span> <span class="pl-c1">Invoke-Parallel</span> <span class="pl-k">-</span>RunspaceTimeout <span class="pl-c1"><span class="pl-c1">10</span></span> <span class="pl-k">-</span>NoCloseOnTimeout <span class="pl-k">-</span>ScriptBlock {

        <span class="pl-c1">Get-WmiObject</span> <span class="pl-k">-</span><span class="pl-k">Class</span> <span class="pl-en">Win32_OperatingSystem</span> <span class="pl-k">-</span>ComputerName <span class="pl-k">$</span><span class="pl-c1">_</span> <span class="pl-k">|</span> select <span class="pl-k">-</span>Property PSComputerName<span class="pl-k">,</span> Caption<span class="pl-k">,</span> Version

}</pre></div>

可以在 TechNet画廊提交的上提供一些过时的笔记和详细信息。

Help!

希望提供稿件,建议,意见和其他帮助!根据@vors的建议分解这一点,以帮助协助。

Notes




相关问题推荐