Skip to content

Instantly share code, notes, and snippets.

@KillyMXI
Last active January 27, 2017 17:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KillyMXI/4059aefe7122c11021152d3603669e24 to your computer and use it in GitHub Desktop.
Save KillyMXI/4059aefe7122c11021152d3603669e24 to your computer and use it in GitHub Desktop.

What's this

Various ways to run code in parallel are showcased here. I've made this demo while looking for a proper way to run file minification for my blog.

More info at http://mxii.eu.org/2016/09/16/parallel-processing-in-powershell/

How to make it run

  1. Put these binaries somewhere in execution path:

  2. Install this module:

    https://github.com/proxb/PoshRSJob

  3. This code is already included here for convenience:

    https://github.com/RamblingCookieMonster/Invoke-Parallel

    (If you have some license complaints about this demo then I'll remove it.)

  4. Make a folder '.\public', put jpegs, pngs, htmls and so on in there.

  5. Run whatever part you need as is or with psake.

# Tasks for psake. Run with invoke-psake.
Task default -Depends TestPath, Minify
Task Minify -Depends MinifyWithRunspace
Task Build -Depends Clean {
& hugo
}
Task Clean {
'Removing everything from .\public\ folder'
Remove-Item .\public\* -recurse
}
Task TestPath {
$path = '.\public'
if(test-path $path)
{
"$path folder found."
}
else
{
"Creating folder $path."
"Put some content in there."
New-Item -ItemType Directory -Force -Path $path
}
}
Task MinifySequential {
. .\minify-sequential.ps1
}
Task MinifyWithInvokeParallel {
. .\minify-invokeparallel.ps1
}
Task MinifyWithJob {
. .\minify-job.ps1
}
Task MinifyWithPoshRSJob {
. .\minify-poshrsjob.ps1
}
Task MinifyWithRunspace {
. .\minify-runspace.ps1
}
Task MinifyWithWorkflow {
. .\minify-workflow.ps1
}
Task MinifyWithInvokeParallelSort {
. .\minify-invokeparallel.ps1 -Sort
}
Task MinifyWithJobSort {
. .\minify-job.ps1 -Sort
}
Task MinifyWithPoshRSJobSort {
. .\minify-poshrsjob.ps1 -Sort
}
Task MinifyWithRunspaceSort {
. .\minify-runspace.ps1 -Sort
}
Task MinifyWithWorkflowSort {
. .\minify-workflow.ps1 -Sort
}
# Ellipsis implementation by KillyMXI
# https://github.com/KillyMXI/PsEllipsisModule/
function Proportion([double]$Left, [double]$Right)
{
if(($Left -eq 0) -and ($Right -eq 0))
{
return 0.5; # left and right sides are equally empty
}
else
{
return 1.0 * $Left / ($Left + $Right);
}
}
function CountMetric([string[]]$Array)
{
return $Array.Count;
}
function SummaryLengthMetric([string[]]$Array, [int]$Correction=0)
{
return ($Array | %{ ([string]$_).Length + $Correction } | Measure-Object -Sum).Sum;
}
function Format-Ellipsis
{
[cmdletbinding()]
param(
[parameter(Mandatory=$true, ValueFromPipeline=$true)][string]$InputString,
[parameter(Mandatory=$true)][ValidateRange(3,[int]::MaxValue)][Alias('w')][int]$Width,
[ValidateSet('count','length','len',ignorecase=$true)][Alias('by')][string]$Weighting = 'length',
[ValidateRange(0.0,1.0)][Alias('p')][double]$Proportion = 0.5,
[ValidateSet('none','auto','left','right',ignorecase=$true)][Alias('cut')][string]$CutSegments = 'left',
[ValidateLength(1,[int]::MaxValue)][Alias('s')][string]$Separator = [IO.Path]::DirectorySeparatorChar,
[ValidateLength(1,[int]::MaxValue)][Alias('e')][string]$EllipsisString = '...',
[switch]$Strict
)
if($InputString.Length -le $Width)
{
return $InputString;
}
if($Strict -and (($CutSegments -eq 'left') -or ($CutSegments -eq 'right')))
{
# this options make no sense in combination with strict mode
$CutSegments = 'auto'
}
$segments = $InputString.Split(@($Separator), [StringSplitOptions]::None);
$leftSegments = @();
$rightSegments = @();
$deadEnd = $false;
$lastSegmentLeft = $false;
while($true)
{
$currStr = ($leftSegments + $EllipsisString + $rightSegments) -join $Separator;
$currLen = $currStr.Length;
$insertSpace = $Width - $currLen - $Separator.Length;
$cutSpace = $Width - $currLen;
if($deadEnd)
{
Write-Debug ("`n" + "-"*$Width);
if(($CutSegments -eq 'none') -or ($cutSpace -eq 0))
{
return $currStr;
}
if(($CutSegments -eq 'left') -or (($CutSegments -eq 'auto') -and $lastSegmentLeft))
{
$lastSeg = $segments[0];
$EllipsisString = $lastSeg.Substring(0,$cutSpace) + $EllipsisString;
}
else
{
$lastSeg = $segments[$segments.Count - 1];
$skip = $lastSeg.Length - $cutSpace;
$EllipsisString = $EllipsisString + $lastSeg.Substring($skip, $cutSpace);
}
return ($leftSegments + $EllipsisString + $rightSegments) -join $Separator;
}
$leftMetric = if($Weighting -eq 'count'){ CountMetric $leftSegments }
else{ SummaryLengthMetric $leftSegments $Separator.Length }
$rightMetric = if($Weighting -eq 'count'){ CountMetric $rightSegments }
else{ SummaryLengthMetric $rightSegments $Separator.Length }
$currentProportion = Proportion $leftMetric $rightMetric;
Write-Debug ("{0,3} | {1,3} | {2,-7:0.#####} | {3,3} | {4,3} | {5}" -f `
$leftMetric, $rightMetric, $currentProportion, $insertSpace, $cutSpace, $currStr);
if(($currentProportion -le $Proportion) -and ($Proportion -gt 0.0)) # right is longer; add to left
{
$seg = $segments[0];
$lastSegmentLeft = $true;
if($seg.Length -le $insertSpace)
{
$segments = $segments[1..($segments.Count-1)];
$leftSegments += @($seg);
}
else # too long; try opposite (in strict mode just call it a day)
{
$seg = $segments[-1];
if(!$Strict -and ($seg.Length -le $insertSpace))
{
$segments = $segments[0..($segments.Count-1-1)];
$rightSegments = @($seg) + $rightSegments;
}
else # too long again; dead end
{
$deadEnd = $true;
}
}
}
else # left is longer; add to right
{
$seg = $segments[-1];
$lastSegmentLeft = $false;
if($seg.Length -le $insertSpace)
{
$segments = $segments[0..($segments.Count-1-1)];
$rightSegments = @($seg) + $rightSegments;
}
else # too long; try opposite (in strict mode just call it a day)
{
$seg = $segments[0];
if(!$Strict -and ($seg.Length -le $insertSpace))
{
$segments = $segments[1..($segments.Count-1)];
$leftSegments += @($seg);
}
else # too long again; dead end
{
$deadEnd = $true;
}
}
}
}
}
function Multiply-Timespan([TimeSpan]$timespan, [double]$multiplier) {
[long]$ticks = $timespan.Ticks * $multiplier
return [TimeSpan]::FromTicks($ticks)
}
function Show-Progress{
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)][PSObject[]]$InputObject,
[Parameter(Mandatory=$true)] $TotalItems,
[string]$Activity = "Processing items"
)
begin {
[int]$Count = 0
$startTime = Get-Date
}
process {
$_
$Count++
$partComplete = 1.0 * $Count / $TotalItems
$now = Get-Date
$passed = $now - $startTime
$remaining = Multiply-Timespan ($now - $startTime) ((1 - $partComplete) / $partComplete)
$status = "{0} / {1} ( {2:P1} )" -f ($Count, $TotalItems, $partComplete, $remaining)
$moreInfo = "passed: {0:hh\:mm\:ss} remaining: {1:hh\:mm\:ss}" -f $passed,$remaining
Write-Progress -Id 1 `
-Activity $Activity `
-PercentComplete (100 * $partComplete) `
-Status ($status) `
-CurrentOperation ($moreInfo)
}
end {
Write-Progress -Id 1 -Activity $Activity -Completed
}
}
function Draw-Line($char = "="){
$char * ($(get-host).UI.RawUI.BufferSize.Width - 1)
}
function Invoke-Parallel {
<#
.SYNOPSIS
Function to control parallel processing using runspaces
.DESCRIPTION
Function to control parallel processing using runspaces
Note that each runspace will not have access to variables and commands loaded in your session or in other runspaces by default.
This behaviour can be changed with parameters.
.PARAMETER ScriptFile
File to run against all input objects. Must include parameter to take in the input object, or use $args. Optionally, include parameter to take in parameter. Example: C:\script.ps1
.PARAMETER ScriptBlock
Scriptblock to run against all computers.
You may use $Using:<Variable> language in PowerShell 3 and later.
The parameter block is added for you, allowing behaviour similar to foreach-object:
Refer to the input object as $_.
Refer to the parameter parameter as $parameter
.PARAMETER InputObject
Run script against these specified objects.
.PARAMETER Parameter
This object is passed to every script block. You can use it to pass information to the script block; for example, the path to a logging folder
Reference this object as $parameter if using the scriptblock parameterset.
.PARAMETER ImportVariables
If specified, get user session variables and add them to the initial session state
.PARAMETER ImportModules
If specified, get loaded modules and pssnapins, add them to the initial session state
.PARAMETER Throttle
Maximum number of threads to run at a single time.
.PARAMETER SleepTimer
Milliseconds to sleep after checking for completed runspaces and in a few other spots. I would not recommend dropping below 200 or increasing above 500
.PARAMETER RunspaceTimeout
Maximum time in seconds a single thread can run. If execution of your code takes longer than this, it is disposed. Default: 0 (seconds)
WARNING: Using this parameter requires that maxQueue be set to throttle (it will be by default) for accurate timing. Details here:
http://gallery.technet.microsoft.com/Run-Parallel-Parallel-377fd430
.PARAMETER NoCloseOnTimeout
Do not dispose of timed out tasks or attempt to close the runspace if threads have timed out. This will prevent the script from hanging in certain situations where threads become non-responsive, at the expense of leaking memory within the PowerShell host.
.PARAMETER MaxQueue
Maximum number of powershell instances to add to runspace pool. If this is higher than $throttle, $timeout will be inaccurate
If this is equal or less than throttle, there will be a performance impact
The default value is $throttle times 3, if $runspaceTimeout is not specified
The default value is $throttle, if $runspaceTimeout is specified
.PARAMETER LogFile
Path to a file where we can log results, including run time for each thread, whether it completes, completes with errors, or times out.
.PARAMETER Quiet
Disable progress bar.
.EXAMPLE
Each example uses Test-ForPacs.ps1 which includes the following code:
param($computer)
if(test-connection $computer -count 1 -quiet -BufferSize 16){
$object = [pscustomobject] @{
Computer=$computer;
Available=1;
Kodak=$(
if((test-path "\\$computer\c$\users\public\desktop\Kodak Direct View Pacs.url") -or (test-path "\\$computer\c$\documents and settings\all users
\desktop\Kodak Direct View Pacs.url") ){"1"}else{"0"}
)
}
}
else{
$object = [pscustomobject] @{
Computer=$computer;
Available=0;
Kodak="NA"
}
}
$object
.EXAMPLE
Invoke-Parallel -scriptfile C:\public\Test-ForPacs.ps1 -inputobject $(get-content C:\pcs.txt) -runspaceTimeout 10 -throttle 10
Pulls list of PCs from C:\pcs.txt,
Runs Test-ForPacs against each
If any query takes longer than 10 seconds, it is disposed
Only run 10 threads at a time
.EXAMPLE
Invoke-Parallel -scriptfile C:\public\Test-ForPacs.ps1 -inputobject c-is-ts-91, c-is-ts-95
Runs against c-is-ts-91, c-is-ts-95 (-computername)
Runs Test-ForPacs against each
.EXAMPLE
$stuff = [pscustomobject] @{
ContentFile = "windows\system32\drivers\etc\hosts"
Logfile = "C:\temp\log.txt"
}
$computers | Invoke-Parallel -parameter $stuff {
$contentFile = join-path "\\$_\c$" $parameter.contentfile
Get-Content $contentFile |
set-content $parameter.logfile
}
This example uses the parameter argument. This parameter is a single object. To pass multiple items into the script block, we create a custom object (using a PowerShell v3 language) with properties we want to pass in.
Inside the script block, $parameter is used to reference this parameter object. This example sets a content file, gets content from that file, and sets it to a predefined log file.
.EXAMPLE
$test = 5
1..2 | Invoke-Parallel -ImportVariables {$_ * $test}
Add variables from the current session to the session state. Without -ImportVariables $Test would not be accessible
.EXAMPLE
$test = 5
1..2 | Invoke-Parallel {$_ * $Using:test}
Reference a variable from the current session with the $Using:<Variable> syntax. Requires PowerShell 3 or later. Note that -ImportVariables parameter is no longer necessary.
.FUNCTIONALITY
PowerShell Language
.NOTES
Credit to Boe Prox for the base runspace code and $Using implementation
http://learn-powershell.net/2012/05/10/speedy-network-information-query-using-powershell/
http://gallery.technet.microsoft.com/scriptcenter/Speedy-Network-Information-5b1406fb#content
https://github.com/proxb/PoshRSJob/
Credit to T Bryce Yehl for the Quiet and NoCloseOnTimeout implementations
Credit to Sergei Vorobev for the many ideas and contributions that have improved functionality, reliability, and ease of use
.LINK
https://github.com/RamblingCookieMonster/Invoke-Parallel
#>
[cmdletbinding(DefaultParameterSetName='ScriptBlock')]
Param (
[Parameter(Mandatory=$false,position=0,ParameterSetName='ScriptBlock')]
[System.Management.Automation.ScriptBlock]$ScriptBlock,
[Parameter(Mandatory=$false,ParameterSetName='ScriptFile')]
[ValidateScript({test-path $_ -pathtype leaf})]
$ScriptFile,
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[Alias('CN','__Server','IPAddress','Server','ComputerName')]
[PSObject]$InputObject,
[PSObject]$Parameter,
[switch]$ImportVariables,
[switch]$ImportModules,
[int]$Throttle = 20,
[int]$SleepTimer = 200,
[int]$RunspaceTimeout = 0,
[switch]$NoCloseOnTimeout = $false,
[int]$MaxQueue,
[validatescript({Test-Path (Split-Path $_ -parent)})]
[string]$LogFile = "C:\temp\log.log",
[switch] $Quiet = $false
)
Begin {
#No max queue specified? Estimate one.
#We use the script scope to resolve an odd PowerShell 2 issue where MaxQueue isn't seen later in the function
if( -not $PSBoundParameters.ContainsKey('MaxQueue') )
{
if($RunspaceTimeout -ne 0){ $script:MaxQueue = $Throttle }
else{ $script:MaxQueue = $Throttle * 3 }
}
else
{
$script:MaxQueue = $MaxQueue
}
Write-Verbose "Throttle: '$throttle' SleepTimer '$sleepTimer' runSpaceTimeout '$runspaceTimeout' maxQueue '$maxQueue' logFile '$logFile'"
#If they want to import variables or modules, create a clean runspace, get loaded items, use those to exclude items
if ($ImportVariables -or $ImportModules)
{
$StandardUserEnv = [powershell]::Create().addscript({
#Get modules and snapins in this clean runspace
$Modules = Get-Module | Select -ExpandProperty Name
$Snapins = Get-PSSnapin | Select -ExpandProperty Name
#Get variables in this clean runspace
#Called last to get vars like $? into session
$Variables = Get-Variable | Select -ExpandProperty Name
#Return a hashtable where we can access each.
@{
Variables = $Variables
Modules = $Modules
Snapins = $Snapins
}
}).invoke()[0]
if ($ImportVariables) {
#Exclude common parameters, bound parameters, and automatic variables
Function _temp {[cmdletbinding()] param() }
$VariablesToExclude = @( (Get-Command _temp | Select -ExpandProperty parameters).Keys + $PSBoundParameters.Keys + $StandardUserEnv.Variables )
Write-Verbose "Excluding variables $( ($VariablesToExclude | sort ) -join ", ")"
# we don't use 'Get-Variable -Exclude', because it uses regexps.
# One of the veriables that we pass is '$?'.
# There could be other variables with such problems.
# Scope 2 required if we move to a real module
$UserVariables = @( Get-Variable | Where { -not ($VariablesToExclude -contains $_.Name) } )
Write-Verbose "Found variables to import: $( ($UserVariables | Select -expandproperty Name | Sort ) -join ", " | Out-String).`n"
}
if ($ImportModules)
{
$UserModules = @( Get-Module | Where {$StandardUserEnv.Modules -notcontains $_.Name -and (Test-Path $_.Path -ErrorAction SilentlyContinue)} | Select -ExpandProperty Path )
$UserSnapins = @( Get-PSSnapin | Select -ExpandProperty Name | Where {$StandardUserEnv.Snapins -notcontains $_ } )
}
}
#region functions
Function Get-RunspaceData {
[cmdletbinding()]
param( [switch]$Wait )
#loop through runspaces
#if $wait is specified, keep looping until all complete
Do {
#set more to false for tracking completion
$more = $false
#Progress bar if we have inputobject count (bound parameter)
if (-not $Quiet) {
Write-Progress -Activity "Running Query" -Status "Starting threads"`
-CurrentOperation "$startedCount threads defined - $totalCount input objects - $script:completedCount input objects processed"`
-PercentComplete $( Try { $script:completedCount / $totalCount * 100 } Catch {0} )
}
#run through each runspace.
Foreach($runspace in $runspaces) {
#get the duration - inaccurate
$currentdate = Get-Date
$runtime = $currentdate - $runspace.startTime
$runMin = [math]::Round( $runtime.totalminutes ,2 )
#set up log object
$log = "" | select Date, Action, Runtime, Status, Details
$log.Action = "Removing:'$($runspace.object)'"
$log.Date = $currentdate
$log.Runtime = "$runMin minutes"
#If runspace completed, end invoke, dispose, recycle, counter++
If ($runspace.Runspace.isCompleted) {
$script:completedCount++
#check if there were errors
if($runspace.powershell.Streams.Error.Count -gt 0) {
#set the logging info and move the file to completed
$log.status = "CompletedWithErrors"
Write-Verbose ($log | ConvertTo-Csv -Delimiter ";" -NoTypeInformation)[1]
foreach($ErrorRecord in $runspace.powershell.Streams.Error) {
Write-Error -ErrorRecord $ErrorRecord
}
}
else {
#add logging details and cleanup
$log.status = "Completed"
Write-Verbose ($log | ConvertTo-Csv -Delimiter ";" -NoTypeInformation)[1]
}
#everything is logged, clean up the runspace
$runspace.powershell.EndInvoke($runspace.Runspace)
$runspace.powershell.dispose()
$runspace.Runspace = $null
$runspace.powershell = $null
}
#If runtime exceeds max, dispose the runspace
ElseIf ( $runspaceTimeout -ne 0 -and $runtime.totalseconds -gt $runspaceTimeout) {
$script:completedCount++
$timedOutTasks = $true
#add logging details and cleanup
$log.status = "TimedOut"
Write-Verbose ($log | ConvertTo-Csv -Delimiter ";" -NoTypeInformation)[1]
Write-Error "Runspace timed out at $($runtime.totalseconds) seconds for the object:`n$($runspace.object | out-string)"
#Depending on how it hangs, we could still get stuck here as dispose calls a synchronous method on the powershell instance
if (!$noCloseOnTimeout) { $runspace.powershell.dispose() }
$runspace.Runspace = $null
$runspace.powershell = $null
$completedCount++
}
#If runspace isn't null set more to true
ElseIf ($runspace.Runspace -ne $null ) {
$log = $null
$more = $true
}
#log the results if a log file was indicated
if($logFile -and $log){
($log | ConvertTo-Csv -Delimiter ";" -NoTypeInformation)[1] | out-file $LogFile -append
}
}
#Clean out unused runspace jobs
$temphash = $runspaces.clone()
$temphash | Where { $_.runspace -eq $Null } | ForEach {
$Runspaces.remove($_)
}
#sleep for a bit if we will loop again
if($PSBoundParameters['Wait']){ Start-Sleep -milliseconds $SleepTimer }
#Loop again only if -wait parameter and there are more runspaces to process
} while ($more -and $PSBoundParameters['Wait'])
#End of runspace function
}
#endregion functions
#region Init
if($PSCmdlet.ParameterSetName -eq 'ScriptFile')
{
$ScriptBlock = [scriptblock]::Create( $(Get-Content $ScriptFile | out-string) )
}
elseif($PSCmdlet.ParameterSetName -eq 'ScriptBlock')
{
#Start building parameter names for the param block
[string[]]$ParamsToAdd = '$_'
if( $PSBoundParameters.ContainsKey('Parameter') )
{
$ParamsToAdd += '$Parameter'
}
$UsingVariableData = $Null
# This code enables $Using support through the AST.
# This is entirely from Boe Prox, and his https://github.com/proxb/PoshRSJob module; all credit to Boe!
if($PSVersionTable.PSVersion.Major -gt 2)
{
#Extract using references
$UsingVariables = $ScriptBlock.ast.FindAll({$args[0] -is [System.Management.Automation.Language.UsingExpressionAst]},$True)
If ($UsingVariables)
{
$List = New-Object 'System.Collections.Generic.List`1[System.Management.Automation.Language.VariableExpressionAst]'
ForEach ($Ast in $UsingVariables)
{
[void]$list.Add($Ast.SubExpression)
}
$UsingVar = $UsingVariables | Group SubExpression | ForEach {$_.Group | Select -First 1}
#Extract the name, value, and create replacements for each
$UsingVariableData = ForEach ($Var in $UsingVar) {
Try
{
$Value = Get-Variable -Name $Var.SubExpression.VariablePath.UserPath -ErrorAction Stop
[pscustomobject]@{
Name = $Var.SubExpression.Extent.Text
Value = $Value.Value
NewName = ('$__using_{0}' -f $Var.SubExpression.VariablePath.UserPath)
NewVarName = ('__using_{0}' -f $Var.SubExpression.VariablePath.UserPath)
}
}
Catch
{
Write-Error "$($Var.SubExpression.Extent.Text) is not a valid Using: variable!"
}
}
$ParamsToAdd += $UsingVariableData | Select -ExpandProperty NewName -Unique
$NewParams = $UsingVariableData.NewName -join ', '
$Tuple = [Tuple]::Create($list, $NewParams)
$bindingFlags = [Reflection.BindingFlags]"Default,NonPublic,Instance"
$GetWithInputHandlingForInvokeCommandImpl = ($ScriptBlock.ast.gettype().GetMethod('GetWithInputHandlingForInvokeCommandImpl',$bindingFlags))
$StringScriptBlock = $GetWithInputHandlingForInvokeCommandImpl.Invoke($ScriptBlock.ast,@($Tuple))
$ScriptBlock = [scriptblock]::Create($StringScriptBlock)
Write-Verbose $StringScriptBlock
}
}
$ScriptBlock = $ExecutionContext.InvokeCommand.NewScriptBlock("param($($ParamsToAdd -Join ", "))`r`n" + $Scriptblock.ToString())
}
else
{
Throw "Must provide ScriptBlock or ScriptFile"; Break
}
Write-Debug "`$ScriptBlock: $($ScriptBlock | Out-String)"
Write-Verbose "Creating runspace pool and session states"
#If specified, add variables and modules/snapins to session state
$sessionstate = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
if ($ImportVariables)
{
if($UserVariables.count -gt 0)
{
foreach($Variable in $UserVariables)
{
$sessionstate.Variables.Add( (New-Object -TypeName System.Management.Automation.Runspaces.SessionStateVariableEntry -ArgumentList $Variable.Name, $Variable.Value, $null) )
}
}
}
if ($ImportModules)
{
if($UserModules.count -gt 0)
{
foreach($ModulePath in $UserModules)
{
$sessionstate.ImportPSModule($ModulePath)
}
}
if($UserSnapins.count -gt 0)
{
foreach($PSSnapin in $UserSnapins)
{
[void]$sessionstate.ImportPSSnapIn($PSSnapin, [ref]$null)
}
}
}
#Create runspace pool
$runspacepool = [runspacefactory]::CreateRunspacePool(1, $Throttle, $sessionstate, $Host)
$runspacepool.Open()
Write-Verbose "Creating empty collection to hold runspace jobs"
$Script:runspaces = New-Object System.Collections.ArrayList
#If inputObject is bound get a total count and set bound to true
$bound = $PSBoundParameters.keys -contains "InputObject"
if(-not $bound)
{
[System.Collections.ArrayList]$allObjects = @()
}
#Set up log file if specified
if( $LogFile ){
New-Item -ItemType file -path $logFile -force | Out-Null
("" | Select Date, Action, Runtime, Status, Details | ConvertTo-Csv -NoTypeInformation -Delimiter ";")[0] | Out-File $LogFile
}
#write initial log entry
$log = "" | Select Date, Action, Runtime, Status, Details
$log.Date = Get-Date
$log.Action = "Batch processing started"
$log.Runtime = $null
$log.Status = "Started"
$log.Details = $null
if($logFile) {
($log | convertto-csv -Delimiter ";" -NoTypeInformation)[1] | Out-File $LogFile -Append
}
$timedOutTasks = $false
#endregion INIT
}
Process {
#add piped objects to all objects or set all objects to bound input object parameter
if($bound)
{
$allObjects = $InputObject
}
Else
{
[void]$allObjects.add( $InputObject )
}
}
End {
#Use Try/Finally to catch Ctrl+C and clean up.
Try
{
#counts for progress
$totalCount = $allObjects.count
$script:completedCount = 0
$startedCount = 0
foreach($object in $allObjects){
#region add scripts to runspace pool
#Create the powershell instance, set verbose if needed, supply the scriptblock and parameters
$powershell = [powershell]::Create()
if ($VerbosePreference -eq 'Continue')
{
[void]$PowerShell.AddScript({$VerbosePreference = 'Continue'})
}
[void]$PowerShell.AddScript($ScriptBlock).AddArgument($object)
if ($parameter)
{
[void]$PowerShell.AddArgument($parameter)
}
# $Using support from Boe Prox
if ($UsingVariableData)
{
Foreach($UsingVariable in $UsingVariableData) {
Write-Verbose "Adding $($UsingVariable.Name) with value: $($UsingVariable.Value)"
[void]$PowerShell.AddArgument($UsingVariable.Value)
}
}
#Add the runspace into the powershell instance
$powershell.RunspacePool = $runspacepool
#Create a temporary collection for each runspace
$temp = "" | Select-Object PowerShell, StartTime, object, Runspace
$temp.PowerShell = $powershell
$temp.StartTime = Get-Date
$temp.object = $object
#Save the handle output when calling BeginInvoke() that will be used later to end the runspace
$temp.Runspace = $powershell.BeginInvoke()
$startedCount++
#Add the temp tracking info to $runspaces collection
Write-Verbose ( "Adding {0} to collection at {1}" -f $temp.object, $temp.starttime.tostring() )
$runspaces.Add($temp) | Out-Null
#loop through existing runspaces one time
Get-RunspaceData
#If we have more running than max queue (used to control timeout accuracy)
#Script scope resolves odd PowerShell 2 issue
$firstRun = $true
while ($runspaces.count -ge $Script:MaxQueue) {
#give verbose output
if($firstRun){
Write-Verbose "$($runspaces.count) items running - exceeded $Script:MaxQueue limit."
}
$firstRun = $false
#run get-runspace data and sleep for a short while
Get-RunspaceData
Start-Sleep -Milliseconds $sleepTimer
}
#endregion add scripts to runspace pool
}
Write-Verbose ( "Finish processing the remaining runspace jobs: {0}" -f ( @($runspaces | Where {$_.Runspace -ne $Null}).Count) )
Get-RunspaceData -wait
if (-not $quiet) {
Write-Progress -Activity "Running Query" -Status "Starting threads" -Completed
}
}
Finally
{
#Close the runspace pool, unless we specified no close on timeout and something timed out
if ( ($timedOutTasks -eq $false) -or ( ($timedOutTasks -eq $true) -and ($noCloseOnTimeout -eq $false) ) ) {
Write-Verbose "Closing the runspace pool"
$runspacepool.close()
}
#collect garbage
[gc]::Collect()
}
}
}
Param (
[switch]$Sort
)
. ".\Invoke-Parallel.ps1"
. ".\ellipsis.ps1"
function Draw-Line($char = "="){
$char * ($(get-host).UI.RawUI.BufferSize.Width - 1)
}
$jpegExtensions = ".jpg", ".jpeg"
$pngExtensions = ,".png"
$textExtensions = ".css", ".htm", ".html", ".js", ".json", ".svg", ".xml"
$allExtensions = $jpegExtensions + $pngExtensions + $textExtensions
$optimizeFile = {
$InputFile = $_
$path = $InputFile.FullName
$ext = $InputFile.Extension
$fromSize = $InputFile.length
if( $ext -in $jpegExtensions )
{
& jpegtran.exe -copy none -optimize -progressive -outfile "$path" "$path"
}
if( $ext -in $pngExtensions )
{
& optipng.exe -quiet -o3 -clobber "$path"
}
if( $ext -in $textExtensions )
{
& minify.exe --html-keep-whitespace -o "$path" "$path"
}
$toSize = (Get-Item $path).length
return [PSCustomObject]@{
Name=$InputFile.Name;
From=$fromSize;
To=$toSize;
Path=($InputFile.FullName | Resolve-Path -Relative);
Folder=($InputFile.Directory | Resolve-Path -Relative);
Ext=$ext
}
}
$numberOfCores = (Get-WmiObject -class Win32_processor).NumberOfLogicalProcessors
$colW = 12
$firstColW = $(get-host).UI.RawUI.BufferSize.Width - 1 - ($colW+1)*4
$allFiles = Get-ChildItem -Path .\public -Recurse -File | Where Extension -in $allExtensions
if($Sort) {
$allFiles = $allFiles | Sort-Object -Property length -Descending
}
$allFiles |
Invoke-Parallel -ImportVariables `
-Throttle $numberOfCores `
-ScriptBlock $optimizeFile |
tee -Variable results |
Format-Table -Property @{ Label="Path"; Width=$firstColW; `
Expression={Format-Ellipsis $_.Path $firstColW} },
@{ Label="From"; Width=$colW; Expression={$_.From} },
@{ Label="To"; Width=$colW; Expression={$_.To} },
@{ Label="Saved"; Alignment="Right"; Width=$colW; `
Expression={"{0}" -f ($_.From - $_.To)} },
@{ Label="Percentage"; Alignment="Right"; Width=$colW; `
Expression={"{0:P1}" -f ($_.To/$_.From)} }
Draw-Line
""
" Totals"
$results |
Group-Object { $_.Ext } |
Select-Object -Property Count,
@{ Name = "Extension"; Expression = {$_.Name} },
@{ Name = "From"; `
Expression = {($_.Group | Measure-Object -Property From -Sum).Sum} },
@{ Name = "To"; `
Expression = {($_.Group | Measure-Object -Property To -Sum).Sum} } |
Format-Table -Property @{ Label="Extension"; Width=$firstColW-$colW-1;
Expression={$_.Extension} },
@{ Label="Count"; Width=$colW; Expression={$_.Count} },
@{ Label="From"; Width=$colW; Expression={$_.From} },
@{ Label="To"; Width=$colW; Expression={$_.To} },
@{ Label="Saved"; Alignment="Right"; Width=$colW; `
Expression={"{0}" -f ($_.From - $_.To)} },
@{ Label="Percentage"; Alignment="Right"; Width=$colW; `
Expression={"{0:P1}" -f ($_.To/$_.From)} }
Param (
[switch]$Sort
)
. ".\helpers.ps1"
. ".\ellipsis.ps1"
function Process-InJobs {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, ValueFromPipeline=$true)] [PSObject[]]$InputObject,
[parameter(Mandatory=$true)] [ScriptBlock]$ScriptBlock,
[int]$MaxThreads = 8,
$SleepTimer = 10,
$AdditionalArguments
)
Get-Job | Remove-Job -Force # killing existing jobs
ForEach ($item in $Input) {
# Check to see if there are too many open threads
# If there are too many threads then wait here until some close
While ($(Get-Job -state running).count -ge $MaxThreads) {
Start-Sleep -Milliseconds $SleepTimer
}
ForEach($Job in Get-Job -State Completed){
Receive-Job $Job
Remove-Job $Job
}
Start-Job -ScriptBlock $ScriptBlock -ArgumentList $item,$AdditionalArguments -Name $item.FullName | Out-Null
}
While ($(Get-Job -State Running).count -gt 0) {
Start-Sleep -Milliseconds $SleepTimer
}
ForEach($Job in Get-Job){
Receive-Job $Job
Remove-Job $Job
}
}
$jpegExtensions = ".jpg", ".jpeg"
$pngExtensions = ,".png"
$textExtensions = ".css", ".htm", ".html", ".js", ".json", ".svg", ".xml"
$allExtensions = $jpegExtensions + $pngExtensions + $textExtensions
$ExtObject = [PSCustomObject]@{
JpegExtensions=$jpegExtensions;
PngExtensions=$pngExtensions;
TextExtensions=$textExtensions
}
function Optimize-File($InputFile, $ExtObject) {
$path = $InputFile.FullName
$ext = $InputFile.Extension
$fromSize = $InputFile.length
if( $ext -in $ExtObject.JpegExtensions )
{
& jpegtran.exe -copy none -optimize -progressive -outfile "$path" "$path"
}
if( $ext -in $ExtObject.PngExtensions )
{
& optipng.exe -quiet -o3 -clobber "$path"
}
if( $ext -in $ExtObject.TextExtensions )
{
& minify.exe --html-keep-whitespace -o "$path" "$path"
}
$toSize = (Get-Item $path).length
return [PSCustomObject]@{
Name=$InputFile.Name;
From=$fromSize;
To=$toSize;
Path=($InputFile.FullName | Resolve-Path -Relative);
Folder=($InputFile.Directory | Resolve-Path -Relative);
Ext=$ext
}
}
$numberOfCores = (Get-WmiObject -class Win32_processor).NumberOfLogicalProcessors
$colW = 12
$firstColW = $(get-host).UI.RawUI.BufferSize.Width - 1 - ($colW+1)*4
$allFiles = Get-ChildItem -Path .\public -Recurse -File | Where Extension -in $allExtensions
$numberOfFiles = $allFiles.Count
if($Sort) {
$allFiles = $allFiles | Sort-Object -Property length -Descending
}
$allFiles |
Process-InJobs -MaxThreads $numberOfCores `
-ScriptBlock ${function:Optimize-File} `
-AdditionalArguments $ExtObject |
Show-Progress -Activity "Minifying files" -TotalItems $numberOfFiles |
tee -Variable results |
Format-Table -Property @{ Label="Path"; Width=$firstColW; `
Expression={Format-Ellipsis $_.Path $firstColW} },
@{ Label="From"; Width=$colW; Expression={$_.From} },
@{ Label="To"; Width=$colW; Expression={$_.To} },
@{ Label="Saved"; Alignment="Right"; Width=$colW; `
Expression={"{0}" -f ($_.From - $_.To)} },
@{ Label="Percentage"; Alignment="Right"; Width=$colW; `
Expression={"{0:P1}" -f ($_.To/$_.From)} }
Draw-Line
""
" Totals"
$results |
Group-Object { $_.Ext } |
Select-Object -Property Count,
@{ Name = "Extension"; Expression = {$_.Name} },
@{ Name = "From"; `
Expression = {($_.Group | Measure-Object -Property From -Sum).Sum} },
@{ Name = "To"; `
Expression = {($_.Group | Measure-Object -Property To -Sum).Sum} } |
Format-Table -Property @{ Label="Extension"; Width=$firstColW-$colW-1;
Expression={$_.Extension} },
@{ Label="Count"; Width=$colW; Expression={$_.Count} },
@{ Label="From"; Width=$colW; Expression={$_.From} },
@{ Label="To"; Width=$colW; Expression={$_.To} },
@{ Label="Saved"; Alignment="Right"; Width=$colW; `
Expression={"{0}" -f ($_.From - $_.To)} },
@{ Label="Percentage"; Alignment="Right"; Width=$colW; `
Expression={"{0:P1}" -f ($_.To/$_.From)} }
Param (
[switch]$Sort
)
. ".\helpers.ps1"
. ".\ellipsis.ps1"
$jpegExtensions = ".jpg", ".jpeg"
$pngExtensions = ,".png"
$textExtensions = ".css", ".htm", ".html", ".js", ".json", ".svg", ".xml"
$allExtensions = $jpegExtensions + $pngExtensions + $textExtensions
$optimizeFile = {
$InputFile = $_
$path = $InputFile.FullName
$ext = $InputFile.Extension
$fromSize = $InputFile.length
if( $ext -in $Using:jpegExtensions )
{
& jpegtran.exe -copy none -optimize -progressive -outfile "$path" "$path"
}
if( $ext -in $Using:pngExtensions )
{
& optipng.exe -quiet -o3 -clobber "$path"
}
if( $ext -in $Using:textExtensions )
{
& minify.exe --html-keep-whitespace -o "$path" "$path"
}
$toSize = (Get-Item $path).length
[PSCustomObject]@{
Name=$InputFile.Name;
From=$fromSize;
To=$toSize;
Path=($InputFile.FullName | Resolve-Path -Relative);
Folder=($InputFile.Directory | Resolve-Path -Relative);
Ext=$ext
}
}
$numberOfCores = (Get-WmiObject -class Win32_processor).NumberOfLogicalProcessors
$Activity = "Minifying files"
$colW = 12
$firstColW = $(get-host).UI.RawUI.BufferSize.Width - 1 - ($colW+1)*4
$allFiles = Get-ChildItem -Path .\public -Recurse -File | Where Extension -in $allExtensions
$numberOfFiles = $allFiles.Count
if($Sort) {
$allFiles = $allFiles | Sort-Object -Property length -Descending
}
$allFiles |
Start-RSJob -ScriptBlock $optimizeFile -Throttle $numberOfCores |
Wait-RSJob |
Receive-RSJob |
Show-Progress -Activity "Minifying files" -TotalItems $numberOfFiles |
tee -Variable results |
Format-Table -Property @{ Label="Path"; Width=$firstColW; `
Expression={Format-Ellipsis $_.Path $firstColW} },
@{ Label="From"; Width=$colW; Expression={$_.From} },
@{ Label="To"; Width=$colW; Expression={$_.To} },
@{ Label="Saved"; Alignment="Right"; Width=$colW; `
Expression={"{0}" -f ($_.From - $_.To)} },
@{ Label="Percentage"; Alignment="Right"; Width=$colW; `
Expression={"{0:P1}" -f ($_.To/$_.From)} }
Draw-Line
""
" Totals"
$results |
Group-Object { $_.Ext } |
Select-Object -Property Count,
@{ Name = "Extension"; Expression = {$_.Name} },
@{ Name = "From"; `
Expression = {($_.Group | Measure-Object -Property From -Sum).Sum} },
@{ Name = "To"; `
Expression = {($_.Group | Measure-Object -Property To -Sum).Sum} } |
Format-Table -Property @{ Label="Extension"; Width=$firstColW-$colW-1;
Expression={$_.Extension} },
@{ Label="Count"; Width=$colW; Expression={$_.Count} },
@{ Label="From"; Width=$colW; Expression={$_.From} },
@{ Label="To"; Width=$colW; Expression={$_.To} },
@{ Label="Saved"; Alignment="Right"; Width=$colW; `
Expression={"{0}" -f ($_.From - $_.To)} },
@{ Label="Percentage"; Alignment="Right"; Width=$colW; `
Expression={"{0:P1}" -f ($_.To/$_.From)} }
Param (
[switch]$Sort
)
. ".\helpers.ps1"
. ".\ellipsis.ps1"
function Process-InRunspaces {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, ValueFromPipeline=$true)] [PSObject[]]$InputObject,
[parameter(Mandatory=$true)] [ScriptBlock]$ScriptBlock,
[int]$MaxThreads = 8,
$SleepTimer = 10,
$AdditionalArguments
)
$location = Get-Location
$rsPool = [RunspaceFactory]::CreateRunspacePool(1,$MaxThreads)
$rsPool.Open()
$RunspaceCollection = @()
ForEach ($item in $Input) {
$Powershell = [PowerShell]::Create()
$Powershell.AddCommand("Set-Location").AddArgument($location) | Out-Null
$Powershell.AddScript($ScriptBlock) | Out-Null
$Powershell.AddArgument($item) | Out-Null
ForEach ($arg in $AdditionalArguments) {
$Powershell.AddArgument($arg) | Out-Null
}
$Powershell.RunspacePool = $rsPool
$Runspace = $PowerShell.BeginInvoke()
[Collections.Arraylist]$RunspaceCollection += [PSCustomObject]@{
Runspace = $Runspace;
PowerShell = $PowerShell
}
}
While ($RunspaceCollection) {
Foreach ($Runspace in $RunspaceCollection.ToArray()) {
If ($Runspace.Runspace.IsCompleted) {
$result = $Runspace.PowerShell.EndInvoke($Runspace.Runspace)
$result # just to make it clear that we output result here
$Runspace.PowerShell.Dispose()
$RunspaceCollection.Remove($Runspace)
}
}
Start-Sleep -Milliseconds $SleepTimer
}
}
$jpegExtensions = ".jpg", ".jpeg"
$pngExtensions = ,".png"
$textExtensions = ".css", ".htm", ".html", ".js", ".json", ".svg", ".xml"
$allExtensions = $jpegExtensions + $pngExtensions + $textExtensions
$ExtObject = [PSCustomObject]@{
JpegExtensions=$jpegExtensions;
PngExtensions=$pngExtensions;
TextExtensions=$textExtensions
}
function Optimize-File($InputFile, $ExtObject) {
$path = $InputFile.FullName
$ext = $InputFile.Extension
$fromSize = $InputFile.length
if( $ext -in $ExtObject.JpegExtensions )
{
& jpegtran.exe -copy none -optimize -progressive -outfile "$path" "$path"
}
if( $ext -in $ExtObject.PngExtensions )
{
& optipng.exe -quiet -o3 -clobber "$path"
}
if( $ext -in $ExtObject.TextExtensions )
{
& minify.exe --html-keep-whitespace -o "$path" "$path"
}
$toSize = (Get-Item $path).length
return [PSCustomObject]@{
Name=$InputFile.Name;
From=$fromSize;
To=$toSize;
Path=($InputFile.FullName | Resolve-Path -Relative);
Folder=($InputFile.Directory | Resolve-Path -Relative);
Ext=$ext
}
}
$numberOfCores = (Get-WmiObject -class Win32_processor).NumberOfLogicalProcessors
$colW = 12
$firstColW = $(get-host).UI.RawUI.BufferSize.Width - 1 - ($colW+1)*4
$allFiles = Get-ChildItem -Path .\public -Recurse -File | Where Extension -in $allExtensions
$numberOfFiles = $allFiles.Count
if($Sort) {
$allFiles = $allFiles | Sort-Object -Property length -Descending
}
$allFiles |
Process-InRunspaces -MaxThreads $numberOfCores `
-ScriptBlock ${function:Optimize-File} `
-AdditionalArguments $ExtObject |
Show-Progress -Activity "Minifying files" -TotalItems $numberOfFiles |
tee -Variable results |
Format-Table -Property @{ Label="Path"; Width=$firstColW; `
Expression={Format-Ellipsis $_.Path $firstColW} },
@{ Label="From"; Width=$colW; Expression={$_.From} },
@{ Label="To"; Width=$colW; Expression={$_.To} },
@{ Label="Saved"; Alignment="Right"; Width=$colW; `
Expression={"{0}" -f ($_.From - $_.To)} },
@{ Label="Percentage"; Alignment="Right"; Width=$colW; `
Expression={"{0:P1}" -f ($_.To/$_.From)} }
Draw-Line
""
" Totals"
$results |
Group-Object { $_.Ext } |
Select-Object -Property Count,
@{ Name = "Extension"; Expression = {$_.Name} },
@{ Name = "From"; `
Expression = {($_.Group | Measure-Object -Property From -Sum).Sum} },
@{ Name = "To"; `
Expression = {($_.Group | Measure-Object -Property To -Sum).Sum} } |
Format-Table -Property @{ Label="Extension"; Width=$firstColW-$colW-1;
Expression={$_.Extension} },
@{ Label="Count"; Width=$colW; Expression={$_.Count} },
@{ Label="From"; Width=$colW; Expression={$_.From} },
@{ Label="To"; Width=$colW; Expression={$_.To} },
@{ Label="Saved"; Alignment="Right"; Width=$colW; `
Expression={"{0}" -f ($_.From - $_.To)} },
@{ Label="Percentage"; Alignment="Right"; Width=$colW; `
Expression={"{0:P1}" -f ($_.To/$_.From)} }
. ".\helpers.ps1"
$jpegExtensions = ".jpg", ".jpeg"
$pngExtensions = ,".png"
$textExtensions = ".css", ".htm", ".html", ".js", ".json", ".svg", ".xml"
$allExtensions = $jpegExtensions + $pngExtensions + $textExtensions
function Optimize-File([System.IO.FileInfo]$InputFile) {
$path = $InputFile.FullName
$ext = $InputFile.Extension
$fromSize = $InputFile.length
if( $ext -in $jpegExtensions )
{
& jpegtran.exe -copy none -optimize -progressive -outfile "$path" "$path"
}
if( $ext -in $pngExtensions )
{
& optipng.exe -quiet -o3 -clobber "$path"
}
if( $ext -in $textExtensions )
{
& minify.exe --html-keep-whitespace -o "$path" "$path"
}
$toSize = (Get-Item $path).length
return [PSCustomObject]@{
Name=$InputFile.Name;
From=$fromSize;
To=$toSize;
Folder=($InputFile.Directory | Resolve-Path -Relative);
Ext=$ext
}
}
$colW = 12
$firstColW = $(get-host).UI.RawUI.BufferSize.Width - 1 - ($colW+1)*4
$allFiles = Get-ChildItem -Path .\public -Recurse -File | Where Extension -in $allExtensions
$numberOfFiles = $allFiles.Count
$allFiles |
%{ Optimize-File $_ } |
Show-Progress -Activity "Minifying files" -TotalItems $numberOfFiles |
tee -Variable results |
Format-Table -GroupBy Folder `
-Property @{ Label="Name"; Width=$firstColW; Expression={$_.Name} },
@{ Label="From"; Width=$colW; Expression={$_.From} },
@{ Label="To"; Width=$colW; Expression={$_.To} },
@{ Label="Saved"; Alignment="Right"; Width=$colW; `
Expression={"{0}" -f ($_.From - $_.To)} },
@{ Label="Percentage"; Alignment="Right"; Width=$colW; `
Expression={"{0:P1}" -f ($_.To/$_.From)} }
Draw-Line
""
" Totals"
$results |
Group-Object { $_.Ext } |
Select-Object -Property Count,
@{ Name = "Extension"; Expression = {$_.Name} },
@{ Name = "From"; `
Expression = {($_.Group | Measure-Object -Property From -Sum).Sum} },
@{ Name = "To"; `
Expression = {($_.Group | Measure-Object -Property To -Sum).Sum} } |
Format-Table -Property @{ Label="Extension"; Width=$firstColW-$colW-1;
Expression={$_.Extension} },
@{ Label="Count"; Width=$colW; Expression={$_.Count} },
@{ Label="From"; Width=$colW; Expression={$_.From} },
@{ Label="To"; Width=$colW; Expression={$_.To} },
@{ Label="Saved"; Alignment="Right"; Width=$colW; `
Expression={"{0}" -f ($_.From - $_.To)} },
@{ Label="Percentage"; Alignment="Right"; Width=$colW; `
Expression={"{0:P1}" -f ($_.To/$_.From)} }
Param (
[switch]$Sort
)
. ".\helpers.ps1"
. ".\ellipsis.ps1"
workflow Process-InWorkflow {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, ValueFromPipeline=$true)] [PSObject[]]$InputObject,
[parameter(Mandatory=$true)] [ScriptBlock]$ScriptBlock,
[int]$ThrottleLimit = 8,
$AdditionalArguments
)
$location = Get-Location
$ProgressPreference="SilentlyContinue"
ForEach -Parallel -ThrottleLimit $ThrottleLimit ($item in $Input)
{
InlineScript {
# InlineScript runs in different environment, but I need at least working directory to be the same.
Set-Location $Using:location
# In some cases arguments passed as strings (Remoting, Jobs, InlineScripts).
# There is no way to deal with it other than to recreate ScriptBlock from string.
$sb2 = [ScriptBlock]::Create($Using:ScriptBlock);
Invoke-Command -ScriptBlock $sb2 -ArgumentList $Using:item,$Using:AdditionalArguments
}
}
$progressPreference = 'Continue'
}
$jpegExtensions = ".jpg", ".jpeg"
$pngExtensions = ,".png"
$textExtensions = ".css", ".htm", ".html", ".js", ".json", ".svg", ".xml"
$allExtensions = $jpegExtensions + $pngExtensions + $textExtensions
$ExtObject = [PSCustomObject]@{
JpegExtensions=$jpegExtensions;
PngExtensions=$pngExtensions;
TextExtensions=$textExtensions
}
function Optimize-File($InputFile, $ExtObject) {
$path = $InputFile.FullName
$ext = $InputFile.Extension
$fromSize = $InputFile.length
if( $ext -in $ExtObject.JpegExtensions )
{
& jpegtran.exe -copy none -optimize -progressive -outfile "$path" "$path"
}
if( $ext -in $ExtObject.PngExtensions )
{
& optipng.exe -quiet -o3 -clobber "$path"
}
if( $ext -in $ExtObject.TextExtensions )
{
& minify.exe --html-keep-whitespace -o "$path" "$path"
}
$toSize = (Get-Item $path).length
return [PSCustomObject]@{
Name=$InputFile.Name;
From=$fromSize;
To=$toSize;
Path=($InputFile.FullName | Resolve-Path -Relative);
Folder=($InputFile.Directory | Resolve-Path -Relative);
Ext=$ext
}
}
$numberOfCores = (Get-WmiObject -class Win32_processor).NumberOfLogicalProcessors
$colW = 12
$firstColW = $(get-host).UI.RawUI.BufferSize.Width - 1 - ($colW+1)*4
$allFiles = Get-ChildItem -Path .\public -Recurse -File | Where Extension -in $allExtensions
$numberOfFiles = $allFiles.Count
if($Sort) {
$allFiles = $allFiles | Sort-Object -Property length -Descending
}
$allFiles |
Process-InWorkflow -ThrottleLimit $numberOfCores `
-AdditionalArguments $ExtObject `
-ScriptBlock ${function:Optimize-File} |
Show-Progress -Activity "Minifying files" -TotalItems $numberOfFiles |
tee -Variable results |
Format-Table -Property @{ Label="Path"; Width=$firstColW; `
Expression={Format-Ellipsis $_.Path $firstColW} },
@{ Label="From"; Width=$colW; Expression={$_.From} },
@{ Label="To"; Width=$colW; Expression={$_.To} },
@{ Label="Saved"; Alignment="Right"; Width=$colW; `
Expression={"{0}" -f ($_.From - $_.To)} },
@{ Label="Percentage"; Alignment="Right"; Width=$colW; `
Expression={"{0:P1}" -f ($_.To/$_.From)} }
Draw-Line
""
" Totals"
$results |
Group-Object { $_.Ext } |
Select-Object -Property Count,
@{ Name = "Extension"; Expression = {$_.Name} },
@{ Name = "From"; `
Expression = {($_.Group | Measure-Object -Property From -Sum).Sum} },
@{ Name = "To"; `
Expression = {($_.Group | Measure-Object -Property To -Sum).Sum} } |
Format-Table -Property @{ Label="Extension"; Width=$firstColW-$colW-1;
Expression={$_.Extension} },
@{ Label="Count"; Width=$colW; Expression={$_.Count} },
@{ Label="From"; Width=$colW; Expression={$_.From} },
@{ Label="To"; Width=$colW; Expression={$_.To} },
@{ Label="Saved"; Alignment="Right"; Width=$colW; `
Expression={"{0}" -f ($_.From - $_.To)} },
@{ Label="Percentage"; Alignment="Right"; Width=$colW; `
Expression={"{0:P1}" -f ($_.To/$_.From)} }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment