Tuesday, June 23, 2015

Powershell Script - Invoke-Multithread - Make any script multithreaded

Here's a very useful wrapper which helps you turn your single threaded sequential functions into multithreaded beasts which complete in a fraction of the time when addressing many servers (or whatever network devices).

The function works by taking your code as a [scriptblock] object and a list of computers (servers, IPs, whatever) and breaking up the list based on a throttle limit (default is 25) and starting powershell jobs.  The script waits for each job to finish and captures the return from the job and parses it into an object array and associates each computer's output with the name.

This function is great for generating reports, pushing configurations, making changes, etc, where native multithreaded cmdlets won't work and sequential code must be used.  Most admins new to powershell don't know how to leverage built in commands that automatically thread and this wrapper function does a nice job at speeding up slow scripts.


Here's an example where I'm just checking to see if the server is a windows server, but this could be any function which takes a string array "ComputerName" as a parameter.

And the output:

ComputerName  Output
------------  ------
SERVER1       Is a Windows Server
SERVER2       Is a Windows Server
SERVER3       Is a Windows Server
SERVER4       Is a Windows Server
SERVER5       Is a Windows Server
SERVER6       Is a Windows Server
SERVER7       Is a Windows Server
SERVER8       Is a Windows Server
SERVER9       Is a Windows Server
SERVER10      Is a Windows Server
SERVER11      Is a Windows Server
SERVER12      Is a Windows Server
SERVER13      Is a Windows Server
SERVER14      Is a Windows Server
SERVER15      Is a Windows Server
SERVER16      Is a Windows Server
SERVER17      Is a Windows Server
SERVER18      Is a Windows Server
SERVER19      Is a Windows Server
SERVER20      Is a Windows Server
SERVER21      Is a Windows Server
SERVER22      Is a Windows Server
SERVER23      Is a Windows Server
SERVER24      Is a Windows Server
SERVER25      Is a Windows Server
SERVER26      Is a Windows Server
SERVER27      Is a Windows Server
SERVER28      Is a Windows Server
SERVER29      Is a Windows Server
SERVER30      Is a Windows Server
SERVER31      Is a Windows Server
SERVER32      Is a Windows Server
SERVER33      Is a Windows Server
SERVER34      Is a Windows Server
SERVER35      Is a Windows Server
SERVER36      Is a Windows Server
SERVER37      Is a Windows Server
SERVER38      Is a Windows Server
SERVER39      Is a Windows Server
SERVER40      Is a Windows Server
SERVER41      Is a Windows Server
SERVER42      Is a Windows Server
SERVER43      Is a Windows Server
SERVER44      Is a Windows Server
SERVER45      Is a Windows Server
SERVER46      Is a Windows Server
SERVER47      Is a Windows Server
SERVER48      Is a Windows Server
SERVER49      Is a Windows Server
SERVER50      Is a Windows Server
NAS1          Is NOT a Windows Server

No comments: