├── .gitattributes ├── .gitignore ├── LICENSE ├── PSParallel.sln ├── README.md ├── images ├── Invoke-Parallel.png └── PSParallel_icon.png ├── module ├── PSParallel.psd1 └── en-US │ ├── PSParallel.dll-Help.xml │ └── about_PSParallel.Help.txt ├── scripts ├── Install.ps1 ├── Publish-ToGallery.ps1 └── dbg.ps1 └── src ├── PSParallel ├── GlobalSuppressions.cs ├── InvokeParallelCommand.cs ├── PSParallel.csproj ├── PowerShellPoolMember.cs ├── PowerShellPoolStreams.cs ├── PowershellPool.cs ├── ProgressManager.cs └── Properties │ └── AssemblyInfo.cs └── PSParallelTests ├── InvokeParallelTests.cs ├── PSParallelTests.csproj └── Properties └── AssemblyInfo.cs /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | build/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studo 2015 cache/options directory 26 | .vs/ 27 | 28 | # MSTest test Results 29 | [Tt]est[Rr]esult*/ 30 | [Bb]uild[Ll]og.* 31 | 32 | # NUNIT 33 | *.VisualState.xml 34 | TestResult.xml 35 | 36 | # Build Results of an ATL Project 37 | [Dd]ebugPS/ 38 | [Rr]eleasePS/ 39 | dlldata.c 40 | 41 | *_i.c 42 | *_p.c 43 | *_i.h 44 | *.ilk 45 | *.meta 46 | *.obj 47 | *.pch 48 | *.pdb 49 | *.pgc 50 | *.pgd 51 | *.rsp 52 | *.sbr 53 | *.tlb 54 | *.tli 55 | *.tlh 56 | *.tmp 57 | *.tmp_proj 58 | *.log 59 | *.vspscc 60 | *.vssscc 61 | .builds 62 | *.pidb 63 | *.svclog 64 | *.scc 65 | 66 | # Chutzpah Test files 67 | _Chutzpah* 68 | 69 | # Visual C++ cache files 70 | ipch/ 71 | *.aps 72 | *.ncb 73 | *.opensdf 74 | *.sdf 75 | *.cachefile 76 | 77 | # Visual Studio profiler 78 | *.psess 79 | *.vsp 80 | *.vspx 81 | 82 | # TFS 2012 Local Workspace 83 | $tf/ 84 | 85 | # Guidance Automation Toolkit 86 | *.gpState 87 | 88 | # ReSharper is a .NET coding add-in 89 | _ReSharper*/ 90 | *.[Rr]e[Ss]harper 91 | *.DotSettings.user 92 | 93 | # JustCode is a .NET coding addin-in 94 | .JustCode 95 | 96 | # TeamCity is a build add-in 97 | _TeamCity* 98 | 99 | # DotCover is a Code Coverage Tool 100 | *.dotCover 101 | 102 | # NCrunch 103 | _NCrunch_* 104 | .*crunch*.local.xml 105 | 106 | # MightyMoose 107 | *.mm.* 108 | AutoTest.Net/ 109 | 110 | # Web workbench (sass) 111 | .sass-cache/ 112 | 113 | # Installshield output folder 114 | [Ee]xpress/ 115 | 116 | # DocProject is a documentation generator add-in 117 | DocProject/buildhelp/ 118 | DocProject/Help/*.HxT 119 | DocProject/Help/*.HxC 120 | DocProject/Help/*.hhc 121 | DocProject/Help/*.hhk 122 | DocProject/Help/*.hhp 123 | DocProject/Help/Html2 124 | DocProject/Help/html 125 | 126 | # Click-Once directory 127 | publish/ 128 | 129 | # Publish Web Output 130 | *.[Pp]ublish.xml 131 | *.azurePubxml 132 | # TODO: Comment the next line if you want to checkin your web deploy settings 133 | # but database connection strings (with potential passwords) will be unencrypted 134 | *.pubxml 135 | *.publishproj 136 | 137 | # NuGet Packages 138 | *.nupkg 139 | # The packages folder can be ignored because of Package Restore 140 | **/packages/* 141 | # except build/, which is used as an MSBuild target. 142 | !**/packages/build/ 143 | # Uncomment if necessary however generally it will be regenerated when needed 144 | #!**/packages/repositories.config 145 | 146 | # Windows Azure Build Output 147 | csx/ 148 | *.build.csdef 149 | 150 | # Windows Store app package directory 151 | AppPackages/ 152 | 153 | # Others 154 | *.[Cc]ache 155 | ClientBin/ 156 | [Ss]tyle[Cc]op.* 157 | ~$* 158 | *~ 159 | *.dbmdl 160 | *.dbproj.schemaview 161 | *.pfx 162 | *.publishsettings 163 | node_modules/ 164 | bower_components/ 165 | 166 | # RIA/Silverlight projects 167 | Generated_Code/ 168 | 169 | # Backup & report files from converting an old project file 170 | # to a newer Visual Studio version. Backup files are not needed, 171 | # because we have git ;-) 172 | _UpgradeReport_Files/ 173 | Backup*/ 174 | UpgradeLog*.XML 175 | UpgradeLog*.htm 176 | 177 | # SQL Server files 178 | *.mdf 179 | *.ldf 180 | 181 | # Business Intelligence projects 182 | *.rdl.data 183 | *.bim.layout 184 | *.bim_*.settings 185 | 186 | # Microsoft Fakes 187 | FakesAssemblies/ 188 | 189 | # Node.js Tools for Visual Studio 190 | .ntvs_analysis.dat 191 | 192 | # Visual Studio 6 build log 193 | *.plg 194 | 195 | # Visual Studio 6 workspace options file 196 | *.opt 197 | *.db 198 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015, Staffan Gustafsson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /PSParallel.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.23107.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PSParallelTests", "src\PSParallelTests\PSParallelTests.csproj", "{8E181CAF-1652-46A4-82B0-D6750EE25E53}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{EAD67DAE-040F-4EFD-920F-FCC2AA2B72F7}" 9 | ProjectSection(SolutionItems) = preProject 10 | README.md = README.md 11 | EndProjectSection 12 | EndProject 13 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Scripts", "Scripts", "{74BE50DD-3285-457C-80D9-5F90897E15F3}" 14 | ProjectSection(SolutionItems) = preProject 15 | scripts\Install.ps1 = scripts\Install.ps1 16 | scripts\Publish-ToGallery.ps1 = scripts\Publish-ToGallery.ps1 17 | EndProjectSection 18 | EndProject 19 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PSParallel", "src\PSParallel\PSParallel.csproj", "{D660B110-98CC-456A-B0A2-ED1C3BE9A25A}" 20 | EndProject 21 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "module", "module", "{9C879DF2-D1E2-4143-A95A-2374F8650F48}" 22 | ProjectSection(SolutionItems) = preProject 23 | module\en-US\about_PSParallel.Help.txt = module\en-US\about_PSParallel.Help.txt 24 | module\en-US\PSParallel.dll-Help.xml = module\en-US\PSParallel.dll-Help.xml 25 | module\PSParallel.psd1 = module\PSParallel.psd1 26 | EndProjectSection 27 | EndProject 28 | Global 29 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 30 | Debug|Any CPU = Debug|Any CPU 31 | Release|Any CPU = Release|Any CPU 32 | EndGlobalSection 33 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 34 | {8E181CAF-1652-46A4-82B0-D6750EE25E53}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 35 | {8E181CAF-1652-46A4-82B0-D6750EE25E53}.Debug|Any CPU.Build.0 = Debug|Any CPU 36 | {8E181CAF-1652-46A4-82B0-D6750EE25E53}.Release|Any CPU.ActiveCfg = Release|Any CPU 37 | {8E181CAF-1652-46A4-82B0-D6750EE25E53}.Release|Any CPU.Build.0 = Release|Any CPU 38 | {D660B110-98CC-456A-B0A2-ED1C3BE9A25A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 39 | {D660B110-98CC-456A-B0A2-ED1C3BE9A25A}.Debug|Any CPU.Build.0 = Debug|Any CPU 40 | {D660B110-98CC-456A-B0A2-ED1C3BE9A25A}.Release|Any CPU.ActiveCfg = Release|Any CPU 41 | {D660B110-98CC-456A-B0A2-ED1C3BE9A25A}.Release|Any CPU.Build.0 = Release|Any CPU 42 | EndGlobalSection 43 | GlobalSection(SolutionProperties) = preSolution 44 | HideSolutionNode = FALSE 45 | EndGlobalSection 46 | GlobalSection(NestedProjects) = preSolution 47 | {74BE50DD-3285-457C-80D9-5F90897E15F3} = {EAD67DAE-040F-4EFD-920F-FCC2AA2B72F7} 48 | {9C879DF2-D1E2-4143-A95A-2374F8650F48} = {EAD67DAE-040F-4EFD-920F-FCC2AA2B72F7} 49 | EndGlobalSection 50 | EndGlobal 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PSParallel 2 | 3 | Invoke scriptblocks in parallel runspaces 4 | 5 | ## Installation 6 | 7 | ```PowerShell 8 | Install-Module PSParallel 9 | ``` 10 | 11 | ```PowerShell 12 | # ping all machines in a subnet 13 | (1..255).Foreach{"192.168.0.$_"} | Invoke-Parallel { [PSCustomObject] @{IP=$_;Result=ping.exe -4 -a -w 20 $_}} 14 | ``` 15 | 16 | Variables and functions are captured from the parent session. 17 | 18 | ## Throttling 19 | 20 | To control the degree of parallelism, i.e. the number of concurrent runspaces, use the -ThrottleLimit parameter 21 | 22 | ```PowerShell 23 | # process lots of crash dumps 24 | Get-ChildItem -Recurse *.dmp | Invoke-Parallel -ThrottleLimit 64 -ProgressActivity "Processing dumps" { 25 | [PSCustomObject] @{ Dump=$_; Analysis = cdb.exe -z $_.fullname -c '"!analyze -v;q"' 26 | } 27 | ``` 28 | 29 | The overhead of spinning up new PowerShell classes is non-zero. Invoke-Parallel is useful when you have items with high latency or that is long running. 30 | 31 | ![image](https://github.com/powercode/PSParallel/raw/master/images/Invoke-Parallel.png) 32 | 33 | ## Contributions 34 | 35 | Pull requests and/or suggestions are more than welcome. 36 | 37 | ### Acknowledgements 38 | 39 | The idea and the basis for the implementation comes from [RamblingCookieMonster](https://github.com/RamblingCookieMonster). 40 | Kudos for that implementation also goes to Boe Prox(@proxb) and Sergei Vorobev(@xvorsx). 41 | -------------------------------------------------------------------------------- /images/Invoke-Parallel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powercode/PSParallel/652ea45f2f24603673fe0ba3af1a502136eca411/images/Invoke-Parallel.png -------------------------------------------------------------------------------- /images/PSParallel_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powercode/PSParallel/652ea45f2f24603673fe0ba3af1a502136eca411/images/PSParallel_icon.png -------------------------------------------------------------------------------- /module/PSParallel.psd1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powercode/PSParallel/652ea45f2f24603673fe0ba3af1a502136eca411/module/PSParallel.psd1 -------------------------------------------------------------------------------- /module/en-US/PSParallel.dll-Help.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 11 | 12 | 13 | Invoke-Parallel 14 | 15 | Invokes the provided scriptblock once for each input, in parallel. 16 | 17 | 18 | 19 | 20 | Invoke 21 | Parallel 22 | 23 | 24 | 25 | The cmdlet uses a RunspacePool an and invokes the provied scriptblock once for each input. 26 | 27 | To control the environment of the scriptblock, the ImportModule, ImportVariable and ImportFunction parameters can be use 28 | d. 29 | 30 | 31 | 32 | 33 | Invoke-Parallel 34 | 35 | ScriptBlock 36 | 37 | Specifies the operation that is performed on each input object. Enter a script block that describes the operation. 38 | 39 | ScriptBlock 40 | 41 | 42 | ParentProgressId 43 | 44 | Identifies the parent activity of the current activity. Use the value -1 if the current activity has no parent activity. 45 | 46 | Int32 47 | 48 | 49 | ProgressId 50 | 51 | Specifies an ID that distinguishes each progress bar from the others. Use this parameter when you are creating more than 52 | one progress bar in a single command. If the progress bars do not have different IDs, they are superimposed instead of 53 | being displayed in a series. 54 | 55 | Int32 56 | 57 | 58 | ProgressActivity 59 | 60 | Specifies the first line of progress text in the heading above the status bar. This text describes the activity whose pr 61 | ogress is being reported. 62 | 63 | String 64 | 65 | 66 | ThrottleLimit 67 | 68 | Specifies the maximum number of concurrent connections that can be established to run this command. If you omit this 69 | parameter or enter a value of 0, the default value, 16, is used. 70 | 71 | Int32 72 | 73 | 74 | InitialSessionState 75 | 76 | The session state used by the runspaces when invoking ScriptBlock. This provides the functions, variables, drives, etc 77 | available to the ScriptBlock. 78 | By default, InitialSessionState.Create2() is used and the functions and variables from the current scope is then 79 | imported. 80 | 81 | InitialSessionState 82 | 83 | 84 | InputObject 85 | 86 | Specifies the input objects. Invoke-Parallel runs the script block on each input object in parallel. Enter a variable th 87 | at contains the objects, or type a command or expression that gets the objects. 88 | 89 | PSObject 90 | 91 | 92 | 93 | Invoke-Parallel 94 | 95 | ScriptBlock 96 | 97 | Specifies the operation that is performed on each input object. Enter a script block that describes the operation. 98 | 99 | ScriptBlock 100 | 101 | 102 | ThrottleLimit 103 | 104 | Specifies the maximum number of concurrent connections that can be established to run this command. If you omit this 105 | parameter or enter a value of 0, the default value, 16, is used. 106 | 107 | Int32 108 | 109 | 110 | InputObject 111 | 112 | Specifies the input objects. Invoke-Parallel runs the script block on each input object in parallel. Enter a variable th 113 | at contains the objects, or type a command or expression that gets the objects. 114 | 115 | PSObject 116 | 117 | 118 | NoProgress 119 | 120 | Will not show progress from Invoke-Progress. Progress from the scriptblock will still be displayed. 121 | 122 | SwitchParameter 123 | 124 | 125 | InitialSessionState 126 | 127 | The session state used by the runspaces when invoking ScriptBlock. This provides the functions, variables, drives, etc 128 | available to the ScriptBlock. 129 | By default, InitialSessionState.Create2() is used and the functions and variables from the current scope is then 130 | imported. 131 | 132 | InitialSessionState 133 | 134 | 135 | 136 | 137 | 138 | 139 | ScriptBlock 140 | 141 | Specifies the operation that is performed on each input object. Enter a script block that describes the operation. 142 | 143 | ScriptBlock 144 | 145 | ScriptBlock 146 | 147 | 148 | 149 | 150 | 151 | ParentProgressId 152 | 153 | Identifies the parent activity of the current activity. Use the value -1 if the current activity has no parent activity. 154 | 155 | Int32 156 | 157 | Int32 158 | 159 | 160 | 161 | 162 | 163 | ProgressId 164 | 165 | Specifies an ID that distinguishes each progress bar from the others. Use this parameter when you are creating more than 166 | one progress bar in a single command. If the progress bars do not have different IDs, they are superimposed instead of 167 | being displayed in a series. 168 | 169 | Int32 170 | 171 | Int32 172 | 173 | 174 | 175 | 176 | 177 | ProgressActivity 178 | 179 | Specifies the first line of progress text in the heading above the status bar. This text describes the activity whose pr 180 | ogress is being reported. 181 | 182 | String 183 | 184 | String 185 | 186 | 187 | 188 | 189 | 190 | ThrottleLimit 191 | 192 | Specifies the maximum number of concurrent connections that can be established to run this command. If you omit this 193 | parameter or enter a value of 0, the default value, 16, is used. 194 | 195 | Int32 196 | 197 | Int32 198 | 199 | 200 | 201 | 202 | 203 | InputObject 204 | 205 | Specifies the input objects. Invoke-Parallel runs the script block on each input object in parallel. Enter a variable th 206 | at contains the objects, or type a command or expression that gets the objects. 207 | 208 | PSObject 209 | 210 | PSObject 211 | 212 | 213 | 214 | 215 | 216 | NoProgress 217 | 218 | Will not show progress from Invoke-Progress. Progress from the scriptblock will still be displayed. 219 | 220 | SwitchParameter 221 | 222 | SwitchParameter 223 | 224 | 225 | 226 | 227 | 228 | InitialSessionState 229 | 230 | The session state used by the runspaces when invoking ScriptBlock. This provides the functions, variables, drives, etc 231 | available to the ScriptBlock. 232 | By default, InitialSessionState.Create2() is used and the functions and variables from the current scope is then 233 | imported. 234 | 235 | InitialSessionState 236 | 237 | InitialSessionState 238 | 239 | 240 | 241 | 242 | 243 | Parameter8 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | System.Management.Automation.PSObject 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | -------------------------- EXAMPLE 1 -------------------------- 276 | 277 | PS C:\> 278 | 279 | (1..255).ForEach{"192.168.0.$_"} | 280 | Invoke-Parallel {$ip = [IPAddress]$_; $res = ping.exe -n 1 -4 -w 20 -a $_; [PSCustomObject] @{IP=$ip;Res=$res}} -ThrottleLimit 64 281 | 282 | This example pings all iP v4 addresses on a subnet, specifying Throttlelimit to 64, i.e. running up to 64 runspaces 283 | in parallel. 284 | 285 | 286 | 287 | 288 | 289 | -------------------------------------------------------------------------------- /module/en-US/about_PSParallel.Help.txt: -------------------------------------------------------------------------------- 1 | TOPIC 2 | about_PSParallel -------------------------------------------------------------------------------- /scripts/Install.ps1: -------------------------------------------------------------------------------- 1 | $manPath = Get-ChildItem -recurse $PSScriptRoot/../module -include *.psd1 | Select-Object -first 1 2 | $man = Test-ModuleManifest $manPath 3 | 4 | $name = $man.Name 5 | [string]$version = $man.Version 6 | $moduleSourceDir = "$PSScriptRoot/$name" 7 | $moduleDir = "~/documents/WindowsPowerShell/Modules/$name/$version/" 8 | 9 | [string]$rootDir = Resolve-Path $PSSCriptRoot/.. 10 | 11 | $InstallDirectory = $moduleDir 12 | 13 | if ('' -eq $InstallDirectory) 14 | { 15 | $personalModules = Join-Path -Path ([Environment]::GetFolderPath('MyDocuments')) -ChildPath WindowsPowerShell\Modules\ 16 | if (($env:PSModulePath -split ';') -notcontains $personalModules) 17 | { 18 | Write-Warning "$personalModules is not in `$env:PSModulePath" 19 | } 20 | 21 | if (!(Test-Path $personalModules)) 22 | { 23 | Write-Error "$personalModules does not exist" 24 | } 25 | 26 | $InstallDirectory = Join-Path -Path $personalModules -ChildPath PSParallel 27 | } 28 | 29 | if(-not (Test-Path $InstallDirectory)) 30 | { 31 | $null = mkdir $InstallDirectory 32 | } 33 | 34 | @( 35 | 'module\PSParallel.psd1' 36 | 'src\PsParallel\bin\Release\PSParallel.dll' 37 | ).Foreach{Copy-Item "$rootdir\$_" -Destination $InstallDirectory } 38 | 39 | $lang = @('en-us') 40 | 41 | $lang.Foreach{ 42 | $lang = $_ 43 | $langDir = "$InstallDirectory\$lang" 44 | if(-not (Test-Path $langDir)) 45 | { 46 | $null = MkDir $langDir 47 | } 48 | 49 | @( 50 | 'PSParallel.dll-Help.xml' 51 | 'about_PSParallel.Help.txt' 52 | ).Foreach{Copy-Item "$rootDir\module\$lang\$_" -Destination $langDir} 53 | } 54 | 55 | Get-ChildItem -Recurse -Path $InstallDirectory 56 | 57 | $cert =Get-ChildItem cert:\CurrentUser\My -CodeSigningCert 58 | if($cert) 59 | { 60 | Get-ChildItem -File $InstallDirectory -Include *.dll,*.psd1 -Recurse | Set-AuthenticodeSignature -Certificate $cert -TimestampServer http://timestamp.verisign.com/scripts/timstamp.dll 61 | } 62 | -------------------------------------------------------------------------------- /scripts/Publish-ToGallery.ps1: -------------------------------------------------------------------------------- 1 | $manPath = Get-ChildItem -recurse $PSScriptRoot/../module -include *.psd1 | Select-Object -first 1 2 | $man = Test-ModuleManifest $manPath 3 | 4 | $name = $man.Name 5 | [string]$version = $man.Version 6 | 7 | $p = @{ 8 | Name = $name 9 | NuGetApiKey = $NuGetApiKey 10 | RequiredVersion = $version 11 | } 12 | 13 | Publish-Module @p 14 | -------------------------------------------------------------------------------- /scripts/dbg.ps1: -------------------------------------------------------------------------------- 1 | param( 2 | [int] $ThrottleLimit = 3, 3 | [int] $Milliseconds = 500 4 | ) 5 | 6 | Import-Module PSParallel -RequiredVersion 2.2.1 7 | 8 | function new-philosopher { 9 | param($name, [string[]] $treats) 10 | [PSCustomObject] @{ 11 | Name = $name 12 | Treats = $treats 13 | } 14 | } 15 | 16 | 17 | $philosopherData = @( 18 | new-philosopher 'Immanuel Kant' 'was a real pissant','who where very rarely stable' 19 | new-philosopher 'Heidegger' 'was a boozy beggar', 'Who could think you under the table' 20 | new-philosopher 'David Hume' 'could out-consume Schopenhauer and Hegel' 21 | new-philosopher 'Wittgenstein' 'was a beery swine', 'Who was just as sloshed as Schlegel' 22 | new-philosopher 'John Stuart Mill' 'of his own free will', 'On half a pint of shandy was particularly ill' 23 | new-philosopher 'Nietzsche' 'There''s nothing Nietzsche couldn''t teach ya', 'Bout the raising of the wrist.' 24 | new-philosopher 'Plato' 'they say, could stick it away', 'Half a crate of whiskey every day' 25 | new-philosopher 'Aristotle' 'was a bugger for the bottle' 26 | new-philosopher 'Hobbes' 'was fond of his dram' 27 | new-philosopher 'Rene Descartes' 'was a drunken fart:', 'I drink, therefore I am' 28 | new-philosopher 'Socrates' 'is particularly missed','A lovely little thinker but a bugger when he''s pissed!' 29 | ) 30 | 31 | 32 | 1..100 | invoke-parallel -Throttle $ThrottleLimit { 33 | 34 | 35 | 36 | $pd = $philosopherData[($_ -1)% $philosopherData.Count] 37 | 38 | 1..100 | ForEach-Object { 39 | $op = switch($_ % 8) 40 | { 41 | 0 { 'sleeping' } 42 | 1 { 'drinking' } 43 | 2 { 'drinking' } 44 | 3 { 'thinking' } 45 | 4 { 'drinking' } 46 | 5 { 'drinking' } 47 | 6 { 'eating' } 48 | 7 { 'drinking' } 49 | } 50 | 51 | $status = $pd.Treats[$_ % $pd.Treats.Length] 52 | 53 | $name = $pd.Name 54 | $currentOperation = "$name is currently $op" 55 | Write-Progress -id $PSParallelProgressId -percent $_ -activity $pd.Name -Status $status -CurrentOperation $currentOperation 56 | Start-Sleep -milliseconds ($Milliseconds + 100 * (Get-Random -Minimum 3 -Maximum 7)) 57 | } 58 | } -------------------------------------------------------------------------------- /src/PSParallel/GlobalSuppressions.cs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/powercode/PSParallel/652ea45f2f24603673fe0ba3af1a502136eca411/src/PSParallel/GlobalSuppressions.cs -------------------------------------------------------------------------------- /src/PSParallel/InvokeParallelCommand.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Collections.ObjectModel; 4 | using System.Diagnostics; 5 | using System.Linq; 6 | using System.Management.Automation; 7 | using System.Management.Automation.Runspaces; 8 | using System.Threading; 9 | 10 | // ReSharper disable UnusedAutoPropertyAccessor.Global 11 | // ReSharper disable AutoPropertyCanBeMadeGetOnly.Global 12 | // ReSharper disable MemberCanBePrivate.Global 13 | namespace PSParallel 14 | { 15 | [Alias("ipa")] 16 | [Cmdlet("Invoke", "Parallel", DefaultParameterSetName = "Progress")] 17 | public sealed class InvokeParallelCommand : PSCmdlet, IDisposable 18 | { 19 | [Parameter(Mandatory = true, Position = 0)] 20 | public ScriptBlock ScriptBlock { get; set; } 21 | 22 | [Parameter(ParameterSetName = "Progress")] 23 | [Alias("ppi")] 24 | public int ParentProgressId { get; set; } = -1; 25 | 26 | [Parameter(ParameterSetName = "Progress")] 27 | [Alias("pi")] 28 | public int ProgressId { get; set; } = 1000; 29 | 30 | [Parameter(ParameterSetName = "Progress")] 31 | [Alias("pa")] 32 | [ValidateNotNullOrEmpty] 33 | public string ProgressActivity { get; set; } = "Invoke-Parallel"; 34 | 35 | [Parameter] 36 | [ValidateRange(1, 128)] 37 | public int ThrottleLimit { get; set; } = 32; 38 | 39 | [Parameter] 40 | [AllowNull] 41 | [Alias("iss")] 42 | public InitialSessionState InitialSessionState { get; set; } 43 | 44 | [Parameter(ValueFromPipeline = true, Mandatory = true)] 45 | public PSObject InputObject { get; set; } 46 | 47 | [Parameter(ParameterSetName = "NoProgress")] 48 | public SwitchParameter NoProgress { get; set; } 49 | 50 | private readonly CancellationTokenSource _cancelationTokenSource = new CancellationTokenSource(); 51 | internal PowershellPool PowershellPool; 52 | 53 | private static InitialSessionState GetSessionState(SessionState sessionState) 54 | { 55 | var initialSessionState = InitialSessionState.CreateDefault2(); 56 | CaptureVariables(sessionState, initialSessionState); 57 | CaptureFunctions(sessionState, initialSessionState); 58 | return initialSessionState; 59 | } 60 | 61 | private static IEnumerable GetFunctions(SessionState sessionState) 62 | { 63 | try 64 | { 65 | var functionDrive = sessionState.InvokeProvider.Item.Get("function:"); 66 | return (Dictionary.ValueCollection)functionDrive[0].BaseObject; 67 | 68 | } 69 | catch (DriveNotFoundException) 70 | { 71 | return new FunctionInfo[] { }; 72 | } 73 | } 74 | 75 | private static IEnumerable GetVariables(SessionState sessionState) 76 | { 77 | try 78 | { 79 | string[] noTouchVariables = { "null", "true", "false", "Error" }; 80 | var variables = sessionState.InvokeProvider.Item.Get("Variable:"); 81 | var psVariables = (IEnumerable)variables[0].BaseObject; 82 | 83 | return psVariables.Where(p => !noTouchVariables.Contains(p.Name)); 84 | } 85 | catch (DriveNotFoundException) 86 | { 87 | return new PSVariable[] { }; 88 | } 89 | } 90 | 91 | private static void CaptureFunctions(SessionState sessionState, InitialSessionState initialSessionState) 92 | { 93 | var functions = GetFunctions(sessionState); 94 | foreach (var func in functions) 95 | { 96 | initialSessionState.Commands.Add(new SessionStateFunctionEntry(func.Name, func.Definition)); 97 | } 98 | } 99 | 100 | private static void CaptureVariables(SessionState sessionState, InitialSessionState initialSessionState) 101 | { 102 | var variables = GetVariables(sessionState); 103 | foreach (var variable in variables) 104 | { 105 | var existing = initialSessionState.Variables[variable.Name].FirstOrDefault(); 106 | if (existing != null) 107 | { 108 | if ((existing.Options & (ScopedItemOptions.Constant | ScopedItemOptions.ReadOnly)) != ScopedItemOptions.None) 109 | { 110 | continue; 111 | } 112 | else 113 | { 114 | initialSessionState.Variables.Remove(existing.Name, existing.GetType()); 115 | initialSessionState.Variables.Add(new SessionStateVariableEntry(variable.Name, variable.Value, variable.Description, variable.Options, variable.Attributes)); 116 | } 117 | } 118 | else 119 | { 120 | initialSessionState.Variables.Add(new SessionStateVariableEntry(variable.Name, variable.Value, variable.Description, variable.Options, variable.Attributes)); 121 | } 122 | } 123 | } 124 | 125 | private void ValidateParameters() 126 | { 127 | if (NoProgress) 128 | { 129 | var boundParameters = MyInvocation.BoundParameters; 130 | foreach (var p in new[] { nameof(ProgressActivity), nameof(ParentProgressId), nameof(ProgressId) }) 131 | { 132 | if (!boundParameters.ContainsKey(p)) continue; 133 | var argumentException = new ArgumentException($"'{p}' must not be specified together with 'NoProgress'", p); 134 | ThrowTerminatingError(new ErrorRecord(argumentException, "InvalidProgressParam", ErrorCategory.InvalidArgument, p)); 135 | } 136 | } 137 | } 138 | 139 | InitialSessionState GetSessionState() 140 | { 141 | if (MyInvocation.BoundParameters.ContainsKey(nameof(InitialSessionState))) 142 | { 143 | if (InitialSessionState == null) 144 | { 145 | return InitialSessionState.Create(); 146 | } 147 | return InitialSessionState; 148 | } 149 | return GetSessionState(base.SessionState); 150 | } 151 | 152 | 153 | private WorkerBase _worker; 154 | protected override void BeginProcessing() 155 | { 156 | ValidateParameters(); 157 | var iss = GetSessionState(); 158 | PowershellPool = new PowershellPool(ThrottleLimit, iss, _cancelationTokenSource.Token); 159 | _worker = NoProgress ? (WorkerBase) new NoProgressWorker(this) : new ProgressWorker(this); 160 | } 161 | 162 | 163 | protected override void ProcessRecord() 164 | { 165 | _worker.ProcessRecord(InputObject); 166 | } 167 | 168 | protected override void EndProcessing() 169 | { 170 | _worker.EndProcessing(); 171 | 172 | } 173 | 174 | protected override void StopProcessing() 175 | { 176 | _cancelationTokenSource.Cancel(); 177 | PowershellPool?.Stop(); 178 | } 179 | 180 | private void WriteOutputs() 181 | { 182 | Debug.WriteLine("Processing output"); 183 | if (_cancelationTokenSource.IsCancellationRequested) 184 | { 185 | return; 186 | } 187 | var streams = PowershellPool.Streams; 188 | foreach (var o in streams.Output.ReadAll()) 189 | { 190 | WriteObject(o, false); 191 | } 192 | 193 | foreach (var o in streams.Debug.ReadAll()) 194 | { 195 | WriteDebug(o.Message); 196 | } 197 | foreach (var e in streams.Error.ReadAll()) 198 | { 199 | WriteError(e); 200 | } 201 | foreach (var w in streams.Warning.ReadAll()) 202 | { 203 | WriteWarning(w.Message); 204 | } 205 | foreach (var i in streams.Information.ReadAll()) 206 | { 207 | WriteInformation(i); 208 | } 209 | foreach (var v in streams.Verbose.ReadAll()) 210 | { 211 | WriteVerbose(v.Message); 212 | } 213 | _worker.WriteProgress(streams.ReadAllProgress()); 214 | } 215 | 216 | public void Dispose() 217 | { 218 | PowershellPool?.Dispose(); 219 | _cancelationTokenSource.Dispose(); 220 | } 221 | 222 | 223 | private abstract class WorkerBase 224 | { 225 | protected readonly InvokeParallelCommand Cmdlet; 226 | protected readonly PowershellPool Pool; 227 | protected bool Stopping => Cmdlet.Stopping; 228 | protected void WriteOutputs() => Cmdlet.WriteOutputs(); 229 | protected void WriteProgress(ProgressRecord record) => Cmdlet.WriteProgress(record); 230 | public abstract void ProcessRecord(PSObject inputObject); 231 | public abstract void EndProcessing(); 232 | public abstract void WriteProgress(Collection progress); 233 | protected ScriptBlock ScriptBlock => Cmdlet.ScriptBlock; 234 | 235 | protected WorkerBase(InvokeParallelCommand cmdlet) 236 | { 237 | Cmdlet = cmdlet; 238 | Pool = cmdlet.PowershellPool; 239 | } 240 | } 241 | 242 | class NoProgressWorker : WorkerBase 243 | { 244 | public NoProgressWorker(InvokeParallelCommand cmdlet) : base(cmdlet) 245 | { 246 | } 247 | 248 | public override void ProcessRecord(PSObject inputObject) 249 | { 250 | while (!Pool.TryAddInput(Cmdlet.ScriptBlock, Cmdlet.InputObject)) 251 | { 252 | Cmdlet.WriteOutputs(); 253 | } 254 | } 255 | 256 | public override void EndProcessing() 257 | { 258 | while (!Pool.WaitForAllPowershellCompleted(100)) 259 | { 260 | if (Stopping) 261 | { 262 | return; 263 | } 264 | WriteOutputs(); 265 | } 266 | WriteOutputs(); 267 | } 268 | 269 | public override void WriteProgress(Collection progress) 270 | { 271 | foreach (var p in progress) 272 | { 273 | base.WriteProgress(p); 274 | } 275 | } 276 | } 277 | 278 | class ProgressWorker : WorkerBase 279 | { 280 | readonly ProgressManager _progressManager; 281 | private readonly List _input; 282 | private int _lastEstimate = -1; 283 | public ProgressWorker(InvokeParallelCommand cmdlet) : base(cmdlet) 284 | { 285 | _progressManager = new ProgressManager(cmdlet.ProgressId, cmdlet.ProgressActivity, $"Processing with {cmdlet.ThrottleLimit} workers", cmdlet.ParentProgressId); 286 | _input = new List(500); 287 | } 288 | 289 | public override void ProcessRecord(PSObject inputObject) 290 | { 291 | _input.Add(inputObject); 292 | } 293 | 294 | public override void EndProcessing() 295 | { 296 | try 297 | { 298 | _progressManager.TotalCount = _input.Count; 299 | var lastPercentComplete = -1; 300 | foreach (var i in _input) 301 | { 302 | var processed = Pool.GetEstimatedProgressCount(); 303 | _lastEstimate = processed; 304 | _progressManager.SetCurrentOperation($"Starting processing of {i}"); 305 | _progressManager.UpdateCurrentProgressRecord(processed); 306 | var pr = _progressManager.ProgressRecord; 307 | if (lastPercentComplete != pr.PercentComplete) 308 | { 309 | WriteProgress(pr); 310 | lastPercentComplete = pr.PercentComplete; 311 | } 312 | 313 | while (!Pool.TryAddInput(ScriptBlock, i)) 314 | { 315 | WriteOutputs(); 316 | } 317 | } 318 | _progressManager.SetCurrentOperation("All work queued. Waiting for remaining work to complete."); 319 | while (!Pool.WaitForAllPowershellCompleted(100)) 320 | { 321 | WriteProgressIfUpdated(); 322 | if (Stopping) 323 | { 324 | return; 325 | } 326 | WriteOutputs(); 327 | } 328 | WriteOutputs(); 329 | } 330 | finally 331 | { 332 | _progressManager.UpdateCurrentProgressRecord(Pool.GetEstimatedProgressCount()); 333 | WriteProgress(_progressManager.Completed()); 334 | } 335 | } 336 | 337 | public override void WriteProgress(Collection progress) 338 | { 339 | foreach (var p in progress) 340 | { 341 | if (p.ActivityId != _progressManager.ActivityId) 342 | { 343 | p.ParentActivityId = _progressManager.ActivityId; 344 | } 345 | WriteProgress(p); 346 | } 347 | if (progress.Count > 0) 348 | { 349 | WriteProgressIfUpdated(); 350 | } 351 | } 352 | 353 | private void WriteProgressIfUpdated() 354 | { 355 | var estimatedCompletedCount = Pool.GetEstimatedProgressCount(); 356 | if (_lastEstimate != estimatedCompletedCount) 357 | { 358 | _lastEstimate = estimatedCompletedCount; 359 | _progressManager.UpdateCurrentProgressRecord(estimatedCompletedCount); 360 | WriteProgress(_progressManager.ProgressRecord); 361 | } 362 | } 363 | } 364 | } 365 | } 366 | -------------------------------------------------------------------------------- /src/PSParallel/PSParallel.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {D660B110-98CC-456A-B0A2-ED1C3BE9A25A} 8 | Library 9 | Properties 10 | PSParallel 11 | PSParallel 12 | v4.5 13 | 512 14 | 15 | 16 | 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | false 25 | 26 | 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | false 34 | 35 | 36 | 37 | False 38 | ..\..\..\..\..\..\..\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.Utility\v4.0_3.0.0.0__31bf3856ad364e35\Microsoft.PowerShell.Commands.Utility.dll 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 64 | -------------------------------------------------------------------------------- /src/PSParallel/PowerShellPoolMember.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Management.Automation; 3 | using System.Management.Automation.Runspaces; 4 | using System.Threading; 5 | 6 | namespace PSParallel 7 | { 8 | class PowerShellPoolMember : IDisposable 9 | { 10 | private readonly PowershellPool _pool; 11 | private readonly int _index; 12 | private readonly InitialSessionState _initialSessionState; 13 | private readonly PowerShellPoolStreams _poolStreams; 14 | private PowerShell _powerShell; 15 | public PowerShell PowerShell => _powerShell; 16 | public int Index => _index ; 17 | 18 | private readonly PSDataCollection _input =new PSDataCollection(); 19 | private PSDataCollection _output; 20 | private int _percentComplete; 21 | public int PercentComplete 22 | { 23 | get { return _percentComplete; } 24 | set { _percentComplete = value; } 25 | } 26 | 27 | 28 | public PowerShellPoolMember(PowershellPool pool, int index, InitialSessionState initialSessionState) 29 | { 30 | _pool = pool; 31 | _index = index; 32 | _initialSessionState = initialSessionState; 33 | _poolStreams = _pool.Streams; 34 | _input.Complete(); 35 | CreatePowerShell(initialSessionState); 36 | } 37 | 38 | private void PowerShellOnInvocationStateChanged(object sender, PSInvocationStateChangedEventArgs psInvocationStateChangedEventArgs) 39 | { 40 | switch (psInvocationStateChangedEventArgs.InvocationStateInfo.State) 41 | { 42 | case PSInvocationState.Stopped: 43 | ReleasePowerShell(); 44 | _pool.ReportStopped(this); 45 | break; 46 | case PSInvocationState.Completed: 47 | case PSInvocationState.Failed: 48 | ResetPowerShell(); 49 | _pool.ReportAvailable(this); 50 | break; 51 | } 52 | } 53 | 54 | private void CreatePowerShell(InitialSessionState initialSessionState) 55 | { 56 | var powerShell = PowerShell.Create(RunspaceMode.NewRunspace); 57 | var runspace = RunspaceFactory.CreateRunspace(initialSessionState); 58 | runspace.ApartmentState = ApartmentState.MTA; 59 | powerShell.Runspace = runspace; 60 | runspace.Open(); 61 | HookStreamEvents(powerShell.Streams); 62 | powerShell.InvocationStateChanged += PowerShellOnInvocationStateChanged; 63 | _powerShell = powerShell; 64 | _output = new PSDataCollection(); 65 | _output.DataAdded += OutputOnDataAdded; 66 | } 67 | 68 | public void ResetPowerShell() 69 | { 70 | UnhookStreamEvents(_powerShell.Streams); 71 | _powerShell.Runspace.ResetRunspaceState(); 72 | var runspace = _powerShell.Runspace; 73 | _powerShell = PowerShell.Create(RunspaceMode.NewRunspace); 74 | _powerShell.Runspace = runspace; 75 | 76 | HookStreamEvents(_powerShell.Streams); 77 | _powerShell.InvocationStateChanged += PowerShellOnInvocationStateChanged; 78 | _output = new PSDataCollection(); 79 | _output.DataAdded += OutputOnDataAdded; 80 | } 81 | 82 | private void ReleasePowerShell() 83 | { 84 | UnhookStreamEvents(_powerShell.Streams); 85 | _powerShell.InvocationStateChanged -= PowerShellOnInvocationStateChanged; 86 | _output.DataAdded -= OutputOnDataAdded; 87 | _powerShell.Dispose(); 88 | } 89 | 90 | 91 | private void HookStreamEvents(PSDataStreams streams) 92 | { 93 | streams.Debug.DataAdded += DebugOnDataAdded; 94 | streams.Error.DataAdded += ErrorOnDataAdded; 95 | streams.Progress.DataAdded += ProgressOnDataAdded; 96 | streams.Information.DataAdded += InformationOnDataAdded; 97 | streams.Verbose.DataAdded += VerboseOnDataAdded; 98 | streams.Warning.DataAdded += WarningOnDataAdded; 99 | } 100 | 101 | 102 | private void UnhookStreamEvents(PSDataStreams streams) 103 | { 104 | streams.Warning.DataAdded -= WarningOnDataAdded; 105 | streams.Verbose.DataAdded -= VerboseOnDataAdded; 106 | streams.Information.DataAdded -= InformationOnDataAdded; 107 | streams.Progress.DataAdded -= ProgressOnDataAdded; 108 | streams.Error.DataAdded -= ErrorOnDataAdded; 109 | streams.Debug.DataAdded -= DebugOnDataAdded; 110 | } 111 | 112 | 113 | public void BeginInvoke(ScriptBlock scriptblock, PSObject inputObject) 114 | { 115 | _percentComplete = 0; 116 | string command = $"param($_,$PSItem, $PSPArallelIndex,$PSParallelProgressId){scriptblock}"; 117 | _powerShell.AddScript(command) 118 | .AddParameter("_", inputObject) 119 | .AddParameter("PSItem", inputObject) 120 | .AddParameter("PSParallelIndex", _index) 121 | .AddParameter("PSParallelProgressId", _index + 1000); 122 | _powerShell.BeginInvoke(_input, _output); 123 | } 124 | 125 | public void Dispose() 126 | { 127 | var ps = _powerShell; 128 | if (ps != null) 129 | { 130 | UnhookStreamEvents(ps.Streams); 131 | ps.Runspace?.Dispose(); 132 | ps.Dispose(); 133 | } 134 | _output.Dispose(); 135 | _input.Dispose(); 136 | } 137 | 138 | private void OutputOnDataAdded(object sender, DataAddedEventArgs dataAddedEventArgs) 139 | { 140 | var item = ((PSDataCollection)sender)[dataAddedEventArgs.Index]; 141 | _poolStreams.Output.Add(item); 142 | } 143 | 144 | 145 | private void InformationOnDataAdded(object sender, DataAddedEventArgs dataAddedEventArgs) 146 | { 147 | var ir = ((PSDataCollection)sender)[dataAddedEventArgs.Index]; 148 | _poolStreams.Information.Add(ir); 149 | } 150 | 151 | private void ProgressOnDataAdded(object sender, DataAddedEventArgs dataAddedEventArgs) 152 | { 153 | var psDataCollection = ((PSDataCollection) sender); 154 | var record = psDataCollection[dataAddedEventArgs.Index]; 155 | _poolStreams.AddProgress(record, _index); 156 | } 157 | 158 | private void ErrorOnDataAdded(object sender, DataAddedEventArgs dataAddedEventArgs) 159 | { 160 | var record = ((PSDataCollection)sender)[dataAddedEventArgs.Index]; 161 | _poolStreams.Error.Add(record); 162 | } 163 | 164 | private void DebugOnDataAdded(object sender, DataAddedEventArgs dataAddedEventArgs) 165 | { 166 | var record = ((PSDataCollection)sender)[dataAddedEventArgs.Index]; 167 | _poolStreams.Debug.Add(record); 168 | } 169 | 170 | private void WarningOnDataAdded(object sender, DataAddedEventArgs dataAddedEventArgs) 171 | { 172 | var record = ((PSDataCollection)sender)[dataAddedEventArgs.Index]; 173 | _poolStreams.Warning.Add(record); 174 | } 175 | 176 | private void VerboseOnDataAdded(object sender, DataAddedEventArgs dataAddedEventArgs) 177 | { 178 | var record = ((PSDataCollection)sender)[dataAddedEventArgs.Index]; 179 | _poolStreams.Verbose.Add(record); 180 | } 181 | 182 | public void Stop() 183 | { 184 | if(_powerShell.InvocationStateInfo.State != PSInvocationState.Stopped) 185 | { 186 | UnhookStreamEvents(_powerShell.Streams); 187 | _powerShell.BeginStop(OnStopped, null); 188 | } 189 | } 190 | 191 | private void OnStopped(IAsyncResult ar) 192 | { 193 | var ps = _powerShell; 194 | if (ps == null) 195 | { 196 | return; 197 | } 198 | ps.EndStop(ar); 199 | _powerShell = null; 200 | } 201 | } 202 | } -------------------------------------------------------------------------------- /src/PSParallel/PowerShellPoolStreams.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Collections.ObjectModel; 4 | using System.Linq; 5 | using System.Management.Automation; 6 | 7 | namespace PSParallel 8 | { 9 | class PowerShellPoolStreams : IDisposable 10 | { 11 | public PSDataCollection Output { get; } = new PSDataCollection(100); 12 | public PSDataCollection Debug { get; } = new PSDataCollection(); 13 | private PSDataCollection Progress { get; } = new PSDataCollection(); 14 | public PSDataCollection Error { get; } = new PSDataCollection(); 15 | public PSDataCollection Warning { get; } = new PSDataCollection(); 16 | public PSDataCollection Information { get; } = new PSDataCollection(); 17 | public PSDataCollection Verbose { get; } = new PSDataCollection(); 18 | 19 | public void Dispose() 20 | { 21 | Output.Dispose(); 22 | Debug.Dispose(); 23 | Progress.Dispose(); 24 | Error.Dispose(); 25 | Information.Dispose(); 26 | Verbose.Dispose(); 27 | } 28 | 29 | public void AddProgress(ProgressRecord progress, int index) 30 | { 31 | DoAddProgress(progress); 32 | OnProgressChanged(progress.PercentComplete, index); 33 | } 34 | 35 | public void ClearProgress(int index) 36 | { 37 | OnProgressChanged(0, index); 38 | } 39 | 40 | protected void DoAddProgress(ProgressRecord progress) 41 | { 42 | Progress.Add(progress); 43 | } 44 | 45 | protected virtual void OnProgressChanged(int progress, int index){} 46 | 47 | public Collection ReadAllProgress() 48 | { 49 | return Progress.ReadAll(); 50 | } 51 | } 52 | 53 | class ProgressTrackingPowerShellPoolStreams : PowerShellPoolStreams 54 | { 55 | private readonly int _maxPoolSize; 56 | private readonly int[] _poolProgress; 57 | private int _currentProgress; 58 | public ProgressTrackingPowerShellPoolStreams(int maxPoolSize) 59 | { 60 | _maxPoolSize = maxPoolSize; 61 | _poolProgress = new int[maxPoolSize]; 62 | } 63 | 64 | protected override void OnProgressChanged(int progress, int index) 65 | { 66 | lock(_poolProgress) { 67 | _poolProgress[index] = progress; 68 | _currentProgress = _poolProgress.Sum(); 69 | } 70 | } 71 | 72 | public int PoolPercentComplete => _currentProgress/_maxPoolSize; 73 | 74 | } 75 | 76 | } -------------------------------------------------------------------------------- /src/PSParallel/PowershellPool.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | using System.Collections.Generic; 4 | using System.Diagnostics; 5 | using System.Diagnostics.Contracts; 6 | using System.Management.Automation; 7 | using System.Management.Automation.Host; 8 | using System.Management.Automation.Runspaces; 9 | using System.Threading; 10 | 11 | 12 | namespace PSParallel 13 | { 14 | sealed class PowershellPool : IDisposable 15 | { 16 | private readonly object _countLock = new object(); 17 | private int _busyCount; 18 | private readonly CancellationToken _cancellationToken; 19 | private readonly List _poolMembers; 20 | private readonly InitialSessionState _initialSessionState; 21 | private readonly BlockingCollection _availablePoolMembers = new BlockingCollection(new ConcurrentQueue()); 22 | public readonly PowerShellPoolStreams Streams = new PowerShellPoolStreams(); 23 | private int _processedCount; 24 | 25 | public PowershellPool(int poolSize, InitialSessionState initialSessionState, CancellationToken cancellationToken) 26 | { 27 | _poolMembers= new List(poolSize); 28 | _initialSessionState = initialSessionState; 29 | _cancellationToken = cancellationToken; 30 | 31 | for (var i = 0; i < poolSize; i++) 32 | { 33 | var powerShellPoolMember = new PowerShellPoolMember(this, i+1, initialSessionState); 34 | _poolMembers.Add(powerShellPoolMember); 35 | _availablePoolMembers.Add(powerShellPoolMember); 36 | } 37 | } 38 | 39 | private int GetPartiallyProcessedCount() 40 | { 41 | var totalPercentComplete = 0; 42 | var count = _poolMembers.Count; 43 | for (int i = 0; i < count; ++i) 44 | { 45 | var percentComplete = _poolMembers[i].PercentComplete; 46 | if (percentComplete < 0) 47 | { 48 | percentComplete = 0; 49 | } 50 | else if(percentComplete > 100) 51 | { 52 | percentComplete = 100; 53 | } 54 | totalPercentComplete += percentComplete; 55 | } 56 | var partiallyProcessedCount = totalPercentComplete / 100; 57 | return partiallyProcessedCount; 58 | } 59 | 60 | public int GetEstimatedProgressCount() 61 | { 62 | lock(_countLock) { 63 | return _processedCount + GetPartiallyProcessedCount(); 64 | } 65 | } 66 | 67 | public bool TryAddInput(ScriptBlock scriptblock,PSObject inputObject) 68 | { 69 | PowerShellPoolMember poolMember; 70 | if(!TryWaitForAvailablePowershell(100, out poolMember)) 71 | { 72 | return false; 73 | } 74 | 75 | Interlocked.Increment(ref _busyCount); 76 | poolMember.BeginInvoke(scriptblock, inputObject); 77 | return true; 78 | } 79 | 80 | 81 | 82 | public bool WaitForAllPowershellCompleted(int timeoutMilliseconds) 83 | { 84 | Contract.Requires(timeoutMilliseconds >= 0); 85 | var startTicks = Environment.TickCount; 86 | var currendTicks = startTicks; 87 | while (currendTicks - startTicks < timeoutMilliseconds) 88 | { 89 | currendTicks = Environment.TickCount; 90 | if (_cancellationToken.IsCancellationRequested) 91 | { 92 | return false; 93 | } 94 | if (Interlocked.CompareExchange(ref _busyCount, 0, 0) == 0) 95 | { 96 | return true; 97 | } 98 | Thread.Sleep(10); 99 | } 100 | return false; 101 | } 102 | 103 | private bool TryWaitForAvailablePowershell(int milliseconds, out PowerShellPoolMember poolMember) 104 | { 105 | if (!_availablePoolMembers.TryTake(out poolMember, milliseconds, _cancellationToken)) 106 | { 107 | _cancellationToken.ThrowIfCancellationRequested(); 108 | Debug.WriteLine("WaitForAvailablePowershell - TryTake failed"); 109 | poolMember = null; 110 | return false; 111 | } 112 | return true; 113 | } 114 | 115 | 116 | public void Dispose() 117 | { 118 | Streams.Dispose(); 119 | _availablePoolMembers.Dispose(); 120 | } 121 | 122 | public void ReportAvailable(PowerShellPoolMember poolmember) 123 | { 124 | Interlocked.Decrement(ref _busyCount); 125 | lock (_countLock) 126 | { 127 | _processedCount++; 128 | poolmember.PercentComplete = 0; 129 | } 130 | 131 | poolmember.PercentComplete = 0; 132 | while (!_availablePoolMembers.TryAdd(poolmember, 1000, _cancellationToken)) 133 | { 134 | _cancellationToken.ThrowIfCancellationRequested(); 135 | Debug.WriteLine("WaitForAvailablePowershell - TryAdd failed"); 136 | } 137 | } 138 | 139 | public void ReportStopped(PowerShellPoolMember powerShellPoolMember) 140 | { 141 | Interlocked.Decrement(ref _busyCount); 142 | } 143 | 144 | public void Stop() 145 | { 146 | _availablePoolMembers.CompleteAdding(); 147 | foreach (var poolMember in _poolMembers) 148 | { 149 | poolMember.Stop(); 150 | } 151 | WaitForAllPowershellCompleted(5000); 152 | } 153 | } 154 | } -------------------------------------------------------------------------------- /src/PSParallel/ProgressManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Management.Automation; 4 | namespace PSParallel 5 | { 6 | class ProgressManager 7 | { 8 | public int TotalCount { get; set; } 9 | private ProgressRecord _progressRecord; 10 | private readonly Stopwatch _stopwatch; 11 | private string _currentOperation; 12 | 13 | public ProgressManager(int activityId, string activity, string statusDescription, int parentActivityId = -1, int totalCount = 0) 14 | { 15 | TotalCount = totalCount; 16 | _stopwatch = new Stopwatch(); 17 | _progressRecord = new ProgressRecord(activityId, activity, statusDescription) {ParentActivityId = parentActivityId}; 18 | } 19 | 20 | 21 | private void UpdateCurrentProgressRecordInternal(int count) 22 | { 23 | if (!_stopwatch.IsRunning && TotalCount > 0) 24 | { 25 | _stopwatch.Start(); 26 | } 27 | var current = TotalCount > 0 ? $"({count}/{TotalCount}) {_currentOperation}" : _currentOperation; 28 | var pr = _progressRecord.Clone(); 29 | pr.CurrentOperation = current; 30 | pr.RecordType = ProgressRecordType.Processing; 31 | if (TotalCount > 0) 32 | { 33 | pr.PercentComplete = GetPercentComplete(count); 34 | pr.SecondsRemaining = GetSecondsRemaining(count); 35 | } 36 | _progressRecord = pr; 37 | } 38 | 39 | public void SetCurrentOperation(string currentOperation) 40 | { 41 | _currentOperation = currentOperation; 42 | } 43 | 44 | public void UpdateCurrentProgressRecord(int count) 45 | { 46 | 47 | UpdateCurrentProgressRecordInternal(count); 48 | } 49 | 50 | public ProgressRecord ProgressRecord => _progressRecord; 51 | 52 | 53 | public ProgressRecord Completed() 54 | { 55 | _stopwatch.Reset(); 56 | _progressRecord = _progressRecord.WithRecordType(ProgressRecordType.Completed); 57 | return _progressRecord; 58 | } 59 | 60 | 61 | private int GetSecondsRemaining(int count) 62 | { 63 | var secondsRemaining = count == 0 ? -1 : (int) ((TotalCount - count)*_stopwatch.ElapsedMilliseconds/1000/count); 64 | return secondsRemaining; 65 | } 66 | 67 | private int GetPercentComplete(int count) 68 | { 69 | var percentComplete = count*100/TotalCount; 70 | return percentComplete; 71 | } 72 | 73 | public int ActivityId => _progressRecord.ActivityId; 74 | } 75 | 76 | 77 | class ProgressProjector 78 | { 79 | private readonly Stopwatch _stopWatch; 80 | private int _percentComplete; 81 | public ProgressProjector() 82 | { 83 | _stopWatch = new Stopwatch(); 84 | _percentComplete = -1; 85 | } 86 | 87 | public void ReportProgress(int percentComplete) 88 | { 89 | if (percentComplete > 100) 90 | { 91 | percentComplete = 100; 92 | } 93 | _percentComplete = percentComplete; 94 | } 95 | 96 | public bool IsValid => _percentComplete > 0 && _stopWatch.IsRunning; 97 | public TimeSpan Elapsed => _stopWatch.Elapsed; 98 | 99 | public TimeSpan ProjectedTotalTime => new TimeSpan(Elapsed.Ticks * 100 / _percentComplete); 100 | 101 | public void Start() 102 | { 103 | _stopWatch.Start(); 104 | _percentComplete = 0; 105 | } 106 | 107 | public void Stop() 108 | { 109 | _stopWatch.Stop(); 110 | } 111 | } 112 | 113 | static class ProgressRecordExtension 114 | { 115 | static ProgressRecord CloneProgressRecord(ProgressRecord record) 116 | { 117 | return new ProgressRecord(record.ActivityId, record.Activity, record.StatusDescription) 118 | { 119 | CurrentOperation = record.CurrentOperation, 120 | ParentActivityId = record.ParentActivityId, 121 | SecondsRemaining = record.SecondsRemaining, 122 | PercentComplete = record.PercentComplete, 123 | RecordType = record.RecordType 124 | }; 125 | } 126 | 127 | public static ProgressRecord Clone(this ProgressRecord record) 128 | { 129 | return CloneProgressRecord(record); 130 | } 131 | 132 | public static ProgressRecord WithCurrentOperation(this ProgressRecord record, string currentOperation) 133 | { 134 | var r = CloneProgressRecord(record); 135 | r.CurrentOperation = currentOperation; 136 | return r; 137 | } 138 | 139 | public static ProgressRecord WithRecordType(this ProgressRecord record, ProgressRecordType recordType) 140 | { 141 | var r = CloneProgressRecord(record); 142 | r.RecordType = recordType; 143 | return r; 144 | } 145 | 146 | public static ProgressRecord WithPercentCompleteAndSecondsRemaining(this ProgressRecord record, int percentComplete, int secondsRemaining) 147 | { 148 | var r = CloneProgressRecord(record); 149 | r.PercentComplete = percentComplete; 150 | r.SecondsRemaining = secondsRemaining; 151 | return r; 152 | } 153 | 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /src/PSParallel/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("PSParallell")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("PSParallell")] 12 | [assembly: AssemblyCopyright("Copyright © 2016")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid("d660b110-98cc-456a-b0a2-ed1c3be9a25a")] 23 | 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the values or you can default the Build and Revision Numbers 32 | // by using the '*' as shown below: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("2.2.3.0")] 36 | -------------------------------------------------------------------------------- /src/PSParallelTests/InvokeParallelTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Management.Automation; 4 | using System.Management.Automation.Runspaces; 5 | using Microsoft.PowerShell.Commands; 6 | using Microsoft.VisualStudio.TestTools.UnitTesting; 7 | using PSParallel; 8 | 9 | namespace PSParallelTests 10 | { 11 | [TestClass] 12 | public sealed class InvokeParallelTests : IDisposable 13 | { 14 | readonly RunspacePool m_runspacePool; 15 | readonly InitialSessionState _iss; 16 | public InvokeParallelTests() 17 | { 18 | _iss = CreateInitialSessionState(); 19 | m_runspacePool = RunspaceFactory.CreateRunspacePool(_iss); 20 | m_runspacePool.SetMaxRunspaces(10); 21 | m_runspacePool.Open(); 22 | } 23 | 24 | private static InitialSessionState CreateInitialSessionState() 25 | { 26 | var iss = InitialSessionState.Create(); 27 | iss.LanguageMode = PSLanguageMode.FullLanguage; 28 | iss.Commands.Add(new[] 29 | { 30 | new SessionStateCmdletEntry("Write-Error", typeof(WriteErrorCommand), null), 31 | new SessionStateCmdletEntry("Write-Verbose", typeof(WriteVerboseCommand), null), 32 | new SessionStateCmdletEntry("Write-Debug", typeof(WriteDebugCommand), null), 33 | new SessionStateCmdletEntry("Write-Progress", typeof(WriteProgressCommand), null), 34 | new SessionStateCmdletEntry("Write-Warning", typeof(WriteWarningCommand), null), 35 | new SessionStateCmdletEntry("Write-Information", typeof(WriteInformationCommand), null), 36 | new SessionStateCmdletEntry("Invoke-Parallel", typeof(InvokeParallelCommand), null), 37 | }); 38 | iss.Providers.Add(new SessionStateProviderEntry("Function", typeof(FunctionProvider), null)); 39 | iss.Providers.Add(new SessionStateProviderEntry("Variable", typeof(VariableProvider), null)); 40 | return iss; 41 | } 42 | 43 | [TestMethod] 44 | public void TestOutput() 45 | { 46 | using (var ps = PowerShell.Create()) 47 | { 48 | ps.RunspacePool = m_runspacePool; 49 | 50 | ps.AddCommand("Invoke-Parallel") 51 | .AddParameter("ScriptBlock", ScriptBlock.Create("$_* 2")) 52 | .AddParameter("ThrottleLimit", 1); 53 | var input = new PSDataCollection {1,2,3,4,5}; 54 | input.Complete(); 55 | var output = ps.Invoke(input); 56 | var sum = output.Aggregate(0, (a, b) => a + b); 57 | Assert.AreEqual(30, sum); 58 | } 59 | } 60 | 61 | [TestMethod] 62 | public void TestParallelOutput() 63 | { 64 | using (var ps = PowerShell.Create()) 65 | { 66 | //ps.RunspacePool = m_runspacePool; 67 | 68 | ps.AddCommand("Invoke-Parallel") 69 | .AddParameter("ScriptBlock", ScriptBlock.Create("$_* 2")) 70 | .AddParameter("ThrottleLimit", 10); 71 | var input = new PSDataCollection(Enumerable.Range(1, 1000)); 72 | input.Complete(); 73 | var output = ps.Invoke(input); 74 | var sum = output.Aggregate(0, (a, b) => a + b); 75 | Assert.AreEqual(1001000, sum); 76 | } 77 | } 78 | 79 | [TestMethod] 80 | public void TestVerboseOutput() 81 | { 82 | using (var ps = PowerShell.Create()) 83 | { 84 | ps.RunspacePool = m_runspacePool; 85 | ps.AddScript("$VerbosePreference=[System.Management.Automation.ActionPreference]::Continue", false).Invoke(); 86 | ps.Commands.Clear(); 87 | ps.AddStatement() 88 | .AddCommand("Invoke-Parallel", false) 89 | .AddParameter("ScriptBlock", ScriptBlock.Create("Write-Verbose $_")) 90 | .AddParameter("ThrottleLimit", 1); 91 | var input = new PSDataCollection {1, 2, 3, 4, 5}; 92 | input.Complete(); 93 | ps.Invoke(input); 94 | Assert.IsFalse(ps.HadErrors, "We don't expect errors here"); 95 | var vrb = ps.Streams.Verbose.ReadAll(); 96 | Assert.IsTrue(vrb.Any(v => v.Message == "1"), "Some verbose message should be '1'"); 97 | } 98 | } 99 | 100 | [TestMethod] 101 | public void TestNoVerboseOutputWithoutPreference() 102 | { 103 | using (var ps = PowerShell.Create()) 104 | { 105 | ps.Runspace = RunspaceFactory.CreateRunspace(); 106 | ps.Runspace.Open(); 107 | ps.AddStatement() 108 | .AddCommand("Invoke-Parallel", false) 109 | .AddParameter("ScriptBlock", ScriptBlock.Create("Write-Verbose $_")) 110 | .AddParameter("ThrottleLimit", 1); 111 | var input = new PSDataCollection {1, 2, 3, 4, 5}; 112 | input.Complete(); 113 | ps.Invoke(input); 114 | Assert.IsFalse(ps.HadErrors, "We don't expect errors here"); 115 | var vrb = ps.Streams.Verbose.ReadAll(); 116 | Assert.IsFalse(vrb.Any(v => v.Message == "1"), "No verbose message should be '1'"); 117 | ps.Runspace.Dispose(); 118 | } 119 | } 120 | 121 | [TestMethod] 122 | public void TestDebugOutput() 123 | { 124 | using (var ps = PowerShell.Create()) 125 | { 126 | using (var rs = RunspaceFactory.CreateRunspace(_iss)) 127 | { 128 | rs.Open(); 129 | ps.Runspace = rs; 130 | ps.AddScript("$DebugPreference=[System.Management.Automation.ActionPreference]::Continue", false).Invoke(); 131 | ps.Commands.Clear(); 132 | ps.AddStatement() 133 | .AddCommand("Invoke-Parallel", false) 134 | .AddParameter("ScriptBlock", ScriptBlock.Create("Write-Debug $_")) 135 | .AddParameter("ThrottleLimit", 1); 136 | var input = new PSDataCollection { 1, 2, 3, 4, 5 }; 137 | input.Complete(); 138 | ps.Invoke(input); 139 | Assert.IsFalse(ps.HadErrors, "We don't expect errors here"); 140 | var dbg = ps.Streams.Debug.ReadAll(); 141 | Assert.IsTrue(dbg.Any(d => d.Message == "1"), "Some debug message should be '1'"); 142 | } 143 | } 144 | } 145 | 146 | [TestMethod] 147 | public void TestNoDebugOutputWithoutPreference() 148 | { 149 | using (var ps = PowerShell.Create()) 150 | { 151 | ps.RunspacePool = m_runspacePool; 152 | ps.Commands.Clear(); 153 | ps.AddStatement() 154 | .AddCommand("Invoke-Parallel", false) 155 | .AddParameter("ScriptBlock", ScriptBlock.Create("Write-Debug $_")) 156 | .AddParameter("ThrottleLimit", 1); 157 | var input = new PSDataCollection {1, 2, 3, 4, 5}; 158 | input.Complete(); 159 | ps.Invoke(input); 160 | var dbg = ps.Streams.Debug.ReadAll(); 161 | Assert.IsFalse(dbg.Any(d => d.Message == "1"), "No debug message should be '1'"); 162 | } 163 | } 164 | 165 | [TestMethod] 166 | public void TestWarningOutput() 167 | { 168 | 169 | using (var ps = PowerShell.Create()) 170 | { 171 | ps.RunspacePool = m_runspacePool; 172 | ps.AddScript("$WarningPreference='Continue'", false).Invoke(); 173 | ps.Commands.Clear(); 174 | ps.AddStatement() 175 | .AddCommand("Invoke-Parallel", false) 176 | .AddParameter("ScriptBlock", ScriptBlock.Create("Write-Warning $_")) 177 | .AddParameter("ThrottleLimit", 1); 178 | var input = new PSDataCollection {1, 2, 3, 4, 5}; 179 | input.Complete(); 180 | ps.Invoke(input); 181 | var wrn = ps.Streams.Warning.ReadAll(); 182 | Assert.IsTrue(wrn.Any(w => w.Message == "1"), "Some warning message should be '1'"); 183 | } 184 | } 185 | 186 | [TestMethod] 187 | public void TestNoWarningOutputWithoutPreference() 188 | { 189 | using (var ps = PowerShell.Create()) 190 | { 191 | ps.RunspacePool = m_runspacePool; 192 | ps.AddScript("$WarningPreference='SilentlyContinue'", false).Invoke(); 193 | ps.Commands.Clear(); 194 | ps.AddStatement() 195 | .AddCommand("Invoke-Parallel", false) 196 | .AddParameter("ScriptBlock", ScriptBlock.Create("Write-Warning $_")) 197 | .AddParameter("ThrottleLimit", 1); 198 | var input = new PSDataCollection {1, 2, 3, 4, 5}; 199 | input.Complete(); 200 | ps.Invoke(input); 201 | var wrn = ps.Streams.Warning.ReadAll(); 202 | Assert.IsFalse(wrn.Any(w => w.Message == "1"), "No warning message should be '1'"); 203 | } 204 | } 205 | 206 | 207 | [TestMethod] 208 | public void TestErrorOutput() 209 | { 210 | using (var ps = PowerShell.Create()) 211 | { 212 | ps.RunspacePool = m_runspacePool; 213 | ps.AddScript("$ErrorActionPreference='Continue'", false).Invoke(); 214 | ps.Commands.Clear(); 215 | ps.AddStatement() 216 | .AddCommand("Invoke-Parallel", false) 217 | .AddParameter("ScriptBlock", ScriptBlock.Create("Write-Error -Message $_ -TargetObject $_")) 218 | .AddParameter("ThrottleLimit", 1); 219 | var input = new PSDataCollection {1, 2, 3, 4, 5}; 220 | input.Complete(); 221 | ps.Invoke(input); 222 | var err = ps.Streams.Error.ReadAll(); 223 | Assert.IsTrue(err.Any(e => e.Exception.Message == "1"), "Some warning message should be '1'"); 224 | } 225 | } 226 | 227 | [TestMethod] 228 | public void TestNoErrorOutputWithoutPreference() 229 | { 230 | using (var ps = PowerShell.Create()) 231 | { 232 | ps.RunspacePool = m_runspacePool; 233 | ps.AddScript("$ErrorActionPreference='SilentlyContinue'", false).Invoke(); 234 | ps.Commands.Clear(); 235 | ps.AddStatement() 236 | .AddCommand("Invoke-Parallel", false) 237 | .AddParameter("ScriptBlock", ScriptBlock.Create("Write-Error -message $_ -TargetObject $_")) 238 | .AddParameter("ThrottleLimit", 1); 239 | var input = new PSDataCollection {1, 2, 3, 4, 5}; 240 | input.Complete(); 241 | ps.Invoke(input); 242 | var err = ps.Streams.Error.ReadAll(); 243 | Assert.IsFalse(err.Any(e => e.Exception.Message == "1"), "No Error message should be '1'"); 244 | } 245 | } 246 | 247 | [TestMethod] 248 | public void TestBinaryExpressionVariableCapture() 249 | { 250 | using (var ps = PowerShell.Create()) 251 | { 252 | ps.RunspacePool = m_runspacePool; 253 | ps.AddScript("[int]$x=10", false).Invoke(); 254 | ps.Commands.Clear(); 255 | ps.AddStatement() 256 | .AddCommand("Invoke-Parallel", false) 257 | .AddParameter("ScriptBlock", ScriptBlock.Create("$x -eq 10")) 258 | .AddParameter("ThrottleLimit", 1) 259 | .AddParameter("InputObject", 1); 260 | 261 | var result = ps.Invoke().First(); 262 | Assert.IsTrue(result); 263 | } 264 | } 265 | 266 | [TestMethod] 267 | public void TestAssingmentExpressionVariableCapture() 268 | { 269 | using (var ps = PowerShell.Create()) 270 | { 271 | ps.RunspacePool = m_runspacePool; 272 | ps.AddScript("[int]$x=10;", false).Invoke(); 273 | ps.Commands.Clear(); 274 | ps.AddStatement() 275 | .AddCommand("Invoke-Parallel", false) 276 | .AddParameter("ScriptBlock", ScriptBlock.Create("$y = $x * 5; $y")) 277 | .AddParameter("ThrottleLimit", 1) 278 | .AddParameter("InputObject", 1); 279 | 280 | var result = ps.Invoke().First(); 281 | Assert.AreEqual(50, result); 282 | } 283 | } 284 | 285 | [TestMethod] 286 | public void TestProgressOutput() 287 | { 288 | using (var ps = PowerShell.Create()) 289 | { 290 | ps.RunspacePool = m_runspacePool; 291 | ps.AddScript("$ProgressPreference='Continue'", false).Invoke(); 292 | ps.AddStatement() 293 | .AddCommand("Invoke-Parallel", false) 294 | .AddParameter("ScriptBlock", 295 | ScriptBlock.Create("Write-Progress -activity 'Test' -Status 'Status' -currentoperation $_")) 296 | .AddParameter("ThrottleLimit", 1); 297 | 298 | var input = new PSDataCollection {1, 2, 3, 4, 5}; 299 | input.Complete(); 300 | ps.Invoke(input); 301 | var progress = ps.Streams.Progress.ReadAll(); 302 | Assert.IsTrue(10 < progress.Count(pr => pr.Activity == "Invoke-Parallel" || pr.Activity == "Test")); 303 | } 304 | } 305 | 306 | [TestMethod] 307 | public void TestProgressOutput2Workers() 308 | { 309 | using (var ps = PowerShell.Create()) 310 | { 311 | ps.RunspacePool = m_runspacePool; 312 | ps.AddScript("$ProgressPreference='Continue'", false).Invoke(); 313 | ps.AddStatement() 314 | .AddCommand("Invoke-Parallel", false) 315 | .AddParameter("ScriptBlock", 316 | ScriptBlock.Create("Write-Progress -activity 'Test' -Status 'Status' -currentoperation $_")) 317 | .AddParameter("ThrottleLimit", 2); 318 | 319 | var input = new PSDataCollection { 1, 2, 3, 4, 5, 6, 7,8, 9,10 }; 320 | input.Complete(); 321 | ps.Invoke(input); 322 | var progress = ps.Streams.Progress.ReadAll(); 323 | Assert.IsTrue(19 <= progress.Count(pr => pr.Activity == "Invoke-Parallel" || pr.Activity == "Test")); 324 | } 325 | } 326 | 327 | 328 | [TestMethod] 329 | public void TestNoProgressOutput() 330 | { 331 | using (var ps = PowerShell.Create()) 332 | { 333 | ps.RunspacePool = m_runspacePool; 334 | 335 | ps.AddStatement() 336 | .AddCommand("Invoke-Parallel", false) 337 | .AddParameter("ScriptBlock", 338 | ScriptBlock.Create("Write-Progress -activity 'Test' -Status 'Status' -currentoperation $_")) 339 | .AddParameter("ThrottleLimit", 1) 340 | .AddParameter("NoProgress"); 341 | 342 | var input = new PSDataCollection {1, 2, 3, 4, 5}; 343 | input.Complete(); 344 | ps.Invoke(input); 345 | var progress = ps.Streams.Progress.ReadAll(); 346 | Assert.IsFalse(progress.Any(pr => pr.Activity == "Invoke-Parallel")); 347 | Assert.AreEqual(5, progress.Count(pr => pr.Activity == "Test")); 348 | } 349 | } 350 | 351 | 352 | [TestMethod] 353 | public void TestFunctionCaptureOutput() 354 | { 355 | using (var ps = PowerShell.Create()) 356 | { 357 | ps.RunspacePool = m_runspacePool; 358 | ps.AddScript(@" 359 | function foo($x) {return $x * 2} 360 | ", false); 361 | ps.AddStatement() 362 | .AddCommand("Invoke-Parallel", false) 363 | .AddParameter("ScriptBlock", ScriptBlock.Create("foo $_")) 364 | .AddParameter("ThrottleLimit", 1) 365 | .AddParameter("NoProgress"); 366 | 367 | var input = new PSDataCollection {1, 2, 3, 4, 5}; 368 | input.Complete(); 369 | var output = ps.Invoke(input); 370 | var sum = output.Aggregate(0, (a, b) => a + b); 371 | Assert.AreEqual(30, sum); 372 | } 373 | } 374 | 375 | 376 | 377 | [TestMethod] 378 | public void TestRecursiveFunctionCaptureOutput() 379 | { 380 | using (var ps = PowerShell.Create()) 381 | { 382 | ps.RunspacePool = m_runspacePool; 383 | ps.AddScript(@" 384 | function foo($x) {return 2 * $x} 385 | function bar($x) {return 3 * (foo $x)} 386 | ", false); 387 | 388 | ps.AddStatement() 389 | .AddCommand("Invoke-Parallel", false) 390 | .AddParameter("ScriptBlock", ScriptBlock.Create("bar $_")) 391 | .AddParameter("ThrottleLimit", 1) 392 | .AddParameter("NoProgress"); 393 | 394 | var input = new PSDataCollection {1, 2, 3, 4, 5}; 395 | input.Complete(); 396 | var output = ps.Invoke(input); 397 | var sum = output.Aggregate(0, (a, b) => a + b); 398 | Assert.AreEqual(90, sum); 399 | } 400 | } 401 | 402 | 403 | public void Dispose() 404 | { 405 | m_runspacePool.Dispose(); 406 | } 407 | } 408 | } 409 | -------------------------------------------------------------------------------- /src/PSParallelTests/PSParallelTests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | {8E181CAF-1652-46A4-82B0-D6750EE25E53} 7 | Library 8 | Properties 9 | PSParallelTests 10 | PSParallelTests 11 | v4.6 12 | 512 13 | {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 14 | 10.0 15 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 16 | $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages 17 | False 18 | UnitTest 19 | 20 | 21 | true 22 | full 23 | false 24 | bin\Debug\ 25 | DEBUG;TRACE 26 | prompt 27 | 4 28 | 29 | 30 | pdbonly 31 | true 32 | bin\Release\ 33 | TRACE 34 | prompt 35 | 4 36 | 37 | 38 | 39 | False 40 | C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.Utility\v4.0_3.0.0.0__31bf3856ad364e35\Microsoft.PowerShell.Commands.Utility.dll 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | {d660b110-98cc-456a-b0a2-ed1c3be9a25a} 64 | PSParallel 65 | 66 | 67 | 68 | 69 | 70 | 71 | False 72 | 73 | 74 | False 75 | 76 | 77 | False 78 | 79 | 80 | False 81 | 82 | 83 | 84 | 85 | 86 | 87 | 94 | -------------------------------------------------------------------------------- /src/PSParallelTests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("PSParallelTests")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("PSParallelTests")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("8e181caf-1652-46a4-82b0-d6750ee25e53")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | --------------------------------------------------------------------------------