Git Product home page Git Product logo

invoke-parallel's Introduction

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

invoke-parallel's People

Contributors

psitem avatar ramblingcookiemonster avatar vors avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.