PoshRSJob.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
$ScriptPath = Split-Path $MyInvocation.MyCommand.Path
$PSModule = $ExecutionContext.SessionState.Module 
$PSModuleRoot = $PSModule.ModuleBase
#region Custom Object
If ($PSVersionTable.PSVersion.Major -gt 2) {
    Write-Verbose "Creating custom RSJob object through reflection"
    #region Module Builder
    $Domain = [AppDomain]::CurrentDomain
    $DynAssembly = New-Object System.Reflection.AssemblyName('etc')
    $AssemblyBuilder = $Domain.DefineDynamicAssembly($DynAssembly, [System.Reflection.Emit.AssemblyBuilderAccess]::Run) # Only run in memory
    $ModuleBuilder = $AssemblyBuilder.DefineDynamicModule('etc', $False)
    #endregion Module Builder
 
    #region V3+ Class Creation
 
    #region V2UsingVariable
    #region Build Class
    $TypeBuilder = $ModuleBuilder.DefineType('PoshRS.PowerShell.V2UsingVariable', 'Public, Class')
    #endregion Build Class
 
    #region Properties
    [void]$TypeBuilder.DefineField('Name',[string],'Public')
    [void]$TypeBuilder.DefineProperty('Name','HasDefault',[string],$Null)
    [void]$TypeBuilder.DefineField('NewName',[string],'Public')
    [void]$TypeBuilder.DefineProperty('NewName','HasDefault',[string],$Null)
    [void]$TypeBuilder.DefineField('Value',[object],'Public')
    [void]$TypeBuilder.DefineProperty('Value','HasDefault',[string],$Null)
    [void]$TypeBuilder.DefineField('NewVarName',[string],'Public')
    [void]$TypeBuilder.DefineProperty('NewVarName','HasDefault',[string],$Null)
    #endregion Properties
 
    #Create the type
    [void]$TypeBuilder.CreateType()
    #endregion V2UsingVariable
 
    #region RSRunspacePool
    #region Build Class
    $TypeBuilder = $ModuleBuilder.DefineType('PoshRS.PowerShell.RSRunspacePool', 'Public, Class')
    #endregion Build Class
 
    #region Properties
    [void]$TypeBuilder.DefineField('RunspacePool',[System.Management.Automation.Runspaces.RunspacePool],'Public')
    [void]$TypeBuilder.DefineProperty('RunspacePool','HasDefault',[System.Management.Automation.Runspaces.RunspacePool],$Null)
    [void]$TypeBuilder.DefineField('State',[System.Management.Automation.Runspaces.RunspacePoolState],'Public')
    [void]$TypeBuilder.DefineProperty('State','HasDefault',[System.Management.Automation.Runspaces.RunspacePoolState],$Null)
    [void]$TypeBuilder.DefineField('AvailableJobs',[int],'Public')
    [void]$TypeBuilder.DefineProperty('AvailableJobs','HasDefault',[int],$Null)
    [void]$TypeBuilder.DefineField('MaxJobs',[int],'Public')
    [void]$TypeBuilder.DefineProperty('MaxJobs','HasDefault',[int],$Null)
    [void]$TypeBuilder.DefineField('LastActivity',[DateTime],'Public')
    [void]$TypeBuilder.DefineProperty('LastActivity','HasDefault',[DateTime],$Null)
    [void]$TypeBuilder.DefineField('RunspacePoolID',[System.Guid],'Public')
    [void]$TypeBuilder.DefineProperty('RunspacePoolID','HasDefault',[System.Guid],$Null)
    [void]$TypeBuilder.DefineField('CanDispose',[bool],'Public')
    [void]$TypeBuilder.DefineProperty('CanDispose','HasDefault',[bool],$Null)
    #endregion Properties
 
    #Create the type
    [void]$TypeBuilder.CreateType()
    #endregion RSRunspacePool
 
    #region RSJob
    #region Build Class
    $TypeBuilder = $ModuleBuilder.DefineType('PoshRS.PowerShell.RSJob', 'Public, Class')
    #endregion Build Class
 
    #region Properties
    [void]$TypeBuilder.DefineField('Name',[string],'Public')
    [void]$TypeBuilder.DefineProperty('Name','HasDefault',[string],$Null)
    [void]$TypeBuilder.DefineField('ID',[int],'Public')
    [void]$TypeBuilder.DefineProperty('ID','HasDefault',[int],$Null)
    [void]$TypeBuilder.DefineField('State',[System.Management.Automation.PSInvocationState],'Public')
    [void]$TypeBuilder.DefineProperty('State','HasDefault',[System.Management.Automation.PSInvocationState],$Null)
    [void]$TypeBuilder.DefineField('InstanceID',[System.Guid],'Public')
    [void]$TypeBuilder.DefineProperty('InstanceID','HasDefault',[System.Guid],$Null)
    [void]$TypeBuilder.DefineField('Handle',[object],'Public')
    [void]$TypeBuilder.DefineProperty('Handle','HasDefault',[object],$Null)
    [void]$TypeBuilder.DefineField('Runspace',[object],'Public')
    [void]$TypeBuilder.DefineProperty('Runspace','HasDefault',[object],$Null)
    [void]$TypeBuilder.DefineField('InnerJob',[System.Management.Automation.PowerShell],'Public')
    [void]$TypeBuilder.DefineProperty('InnerJob','HasDefault',[System.Management.Automation.PowerShell],$Null)
    [void]$TypeBuilder.DefineField('Finished',[System.Threading.ManualResetEvent],'Public')
    [void]$TypeBuilder.DefineProperty('Finished','HasDefault',[System.Threading.ManualResetEvent],$Null)
    [void]$TypeBuilder.DefineField('Command',[string],'Public')
    [void]$TypeBuilder.DefineProperty('Command','HasDefault',[string],$Null)
    [void]$TypeBuilder.DefineField('Error',[System.Management.Automation.PSDataCollection[System.Management.Automation.ErrorRecord]],'Public')
    [void]$TypeBuilder.DefineProperty('Error','HasDefault',[System.Management.Automation.PSDataCollection[System.Management.Automation.ErrorRecord]],$Null)
    [void]$TypeBuilder.DefineField('Verbose',[System.Management.Automation.PSDataCollection[System.Management.Automation.VerboseRecord]],'Public')
    [void]$TypeBuilder.DefineProperty('Verbose','HasDefault',[System.Management.Automation.PSDataCollection[System.Management.Automation.VerboseRecord]],$Null)
    [void]$TypeBuilder.DefineField('Debug',[System.Management.Automation.PSDataCollection[System.Management.Automation.DebugRecord]],'Public')
    [void]$TypeBuilder.DefineProperty('Debug','HasDefault',[System.Management.Automation.PSDataCollection[System.Management.Automation.DebugRecord]],$Null)
    [void]$TypeBuilder.DefineField('Warning',[System.Management.Automation.PSDataCollection[System.Management.Automation.WarningRecord]],'Public')
    [void]$TypeBuilder.DefineProperty('Warning','HasDefault',[System.Management.Automation.PSDataCollection[System.Management.Automation.WarningRecord]],$Null)
    [void]$TypeBuilder.DefineField('Progress',[System.Management.Automation.PSDataCollection[System.Management.Automation.ProgressRecord]],'Public')
    [void]$TypeBuilder.DefineProperty('Progress','HasDefault',[System.Management.Automation.PSDataCollection[System.Management.Automation.ProgressRecord]],$Null)
    [void]$TypeBuilder.DefineField('HasMoreData',[bool],'Public')
    [void]$TypeBuilder.DefineProperty('HasMoreData','HasDefault',[bool],$Null)
    [void]$TypeBuilder.DefineField('HasErrors',[bool],'Public')
    [void]$TypeBuilder.DefineProperty('HasErrors','HasDefault',[bool],$Null)
    [void]$TypeBuilder.DefineField('Output',[Object],'Public')
    [void]$TypeBuilder.DefineProperty('Output','HasDefault',[Object],$Null)
    [void]$TypeBuilder.DefineField('RunspacePoolID',[System.Guid],'Public')
    [void]$TypeBuilder.DefineProperty('RunspacePoolID','HasDefault',[System.Guid],$Null)
    [void]$TypeBuilder.DefineField('Completed',[bool],'Public')
    [void]$TypeBuilder.DefineProperty('Completed','HasDefault',[bool],$Null)
    [void]$TypeBuilder.DefineField('Batch',[string],'Public')
    [void]$TypeBuilder.DefineProperty('Batch','HasDefault',[string],$Null)
    #endregion Properties
 
    #Create the type
    [void]$TypeBuilder.CreateType()
    #endregion RSJob
 
    #endregion V3+ Class Creation
}
Else {
#region V2 Class creation
Add-Type -TypeDefinition @"
    using System;

    namespace PoshRS.PowerShell
    {
        public class RSJob
        {
            public string Name;
            public int ID;
            public System.Management.Automation.PSInvocationState State;
            public System.Guid InstanceID;
            public object Handle;
            public object Runspace;
            public System.Management.Automation.PowerShell InnerJob;
            public System.Threading.ManualResetEvent Finished;
            public string Command;
            public System.Management.Automation.PSDataCollection<System.Management.Automation.ErrorRecord> Error;
            public System.Management.Automation.PSDataCollection<System.Management.Automation.VerboseRecord> Verbose;
            public System.Management.Automation.PSDataCollection<System.Management.Automation.DebugRecord> Debug;
            public System.Management.Automation.PSDataCollection<System.Management.Automation.WarningRecord> Warning;
            public System.Management.Automation.PSDataCollection<System.Management.Automation.ProgressRecord> Progress;
            public bool HasMoreData;
            public bool HasErrors;
            public object Output;
            public System.Guid RunspacePoolID;
            public bool Completed = false;
            public string Batch;
        }
        public class RSRunspacePool
        {
            public System.Management.Automation.Runspaces.RunspacePool RunspacePool;
            public System.Management.Automation.Runspaces.RunspacePoolState State;
            public int AvailableJobs;
            public int MaxJobs;
            public DateTime LastActivity = DateTime.MinValue;
            public System.Guid RunspacePoolID;
            public bool CanDispose = false;
        }
        public class V2UsingVariable
        {
            public string Name;
            public string NewName;
            public object Value;
            public string NewVarName;
        }
    }
"@
 -Language CSharp
}
#endregion V2 Class creation
#endregion Custom Object

#region RSJob Collections
Write-Verbose "Creating RS collections"
New-Variable PoshRS_Jobs -Value ([System.Collections.ArrayList]::Synchronized([System.Collections.ArrayList]@())) -Option ReadOnly -Scope Script
New-Variable PoshRS_jobCleanup -Value ([hashtable]::Synchronized(@{})) -Option ReadOnly -Scope Script
New-Variable PoshRS_JobID -Value ([int64]0) -Option ReadOnly -Scope Script
New-Variable PoshRS_RunspacePools -Value ([System.Collections.ArrayList]::Synchronized([System.Collections.ArrayList]@())) -Option ReadOnly -Scope Script
New-Variable PoshRS_RunspacePoolCleanup -Value ([hashtable]::Synchronized(@{})) -Option ReadOnly -Scope Script
#endregion RSJob Collections

#region Cleanup Routine
Write-Verbose "Creating routine to monitor RS jobs"
$PoshRS_jobCleanup.Flag=$True
$PoshRS_jobCleanup.Host = $Host
$PoshRS_jobCleanup.Runspace =[runspacefactory]::CreateRunspace()   
$PoshRS_jobCleanup.Runspace.Open()         
$PoshRS_jobCleanup.Runspace.SessionStateProxy.SetVariable("PoshRS_jobCleanup",$PoshRS_jobCleanup)     
$PoshRS_jobCleanup.Runspace.SessionStateProxy.SetVariable("PoshRS_Jobs",$PoshRS_Jobs) 
$PoshRS_jobCleanup.PowerShell = [PowerShell]::Create().AddScript({
    #Routine to handle completed runspaces
    #$PoshRS_jobCleanup.Host.UI.WriteVerboseLine("Begin Do Loop")
    Do {   
        [System.Threading.Monitor]::Enter($PoshRS_Jobs.syncroot) 
        Foreach($job in $PoshRS_Jobs) {
            $job.state = $job.InnerJob.InvocationStateInfo.State
            If ($job.Handle.isCompleted -AND (-NOT $Job.Completed)) {   
                #$PoshRS_jobCleanup.Host.UI.WriteVerboseLine("$($Job.Id) completed")
                Try {           
                    $Data = $job.InnerJob.EndInvoke($job.Handle)
                } Catch {
                    $CaughtErrors = $Error
                    #$PoshRS_jobCleanup.Host.UI.WriteVerboseLine("$($Job.Id) Caught terminating Error in job: $_")
                }
                #$PoshRS_jobCleanup.Host.UI.WriteVerboseLine("$($Job.Id) Checking for errors")
                If ($job.InnerJob.Streams.Error -OR $CaughtErrors) {
                    #$PoshRS_jobCleanup.Host.UI.WriteVerboseLine("$($Job.Id) Errors Found!")
                    $ErrorList = New-Object System.Management.Automation.PSDataCollection[System.Management.Automation.ErrorRecord]
                    If ($job.InnerJob.Streams.Error) {
                        ForEach ($Err in $job.InnerJob.Streams.Error) {
                            #$PoshRS_jobCleanup.Host.UI.WriteVerboseLine("`t$($Job.Id) Adding Error")
                            [void]$ErrorList.Add($Err)
                        }
                    }
                    If ($CaughtErrors) {
                        ForEach ($Err in $CaughtErrors) {
                            #$PoshRS_jobCleanup.Host.UI.WriteVerboseLine("`t$($Job.Id) Adding Error")
                            [void]$ErrorList.Add($Err)
                        }                    
                    }
                    $job.Error = $ErrorList
                }
                #$PoshRS_jobCleanup.Host.UI.WriteVerboseLine("$($Job.Id) Disposing job")
                $job.InnerJob.dispose() 
                $job.Completed = $True  
                #Return type from Invoke() is a generic collection; need to verify the first index is not NULL
                If (($Data.Count -gt 0) -AND (-NOT ($Null -eq $Data[0]))) {   
                    $job.output = $Data
                    $job.HasMoreData = $True                            
                }              
                $Error.Clear()
            } 
        }        
        [System.Threading.Monitor]::Exit($PoshRS_Jobs.syncroot)
        Start-Sleep -Milliseconds 100     
    } while ($PoshRS_jobCleanup.Flag)
})
$PoshRS_jobCleanup.PowerShell.Runspace = $PoshRS_jobCleanup.Runspace
$PoshRS_jobCleanup.Handle = $PoshRS_jobCleanup.PowerShell.BeginInvoke()  

Write-Verbose "Creating routine to monitor Runspace Pools"
$PoshRS_RunspacePoolCleanup.Flag=$True
$PoshRS_RunspacePoolCleanup.Host=$Host
#5 minute timeout for unused runspace pools
$PoshRS_RunspacePoolCleanup.Timeout = [timespan]::FromMinutes(1).Ticks
$PoshRS_RunspacePoolCleanup.Runspace =[runspacefactory]::CreateRunspace()
 
#Create Type Collection so the object will work properly
$Types = Get-ChildItem "$($PSScriptRoot)\TypeData" -Filter *Types* | Select -ExpandProperty Fullname 
$Types | ForEach { 
    $TypeEntry = New-Object System.Management.Automation.Runspaces.TypeConfigurationEntry -ArgumentList $_ 
    $PoshRS_RunspacePoolCleanup.Runspace.RunspaceConfiguration.types.Append($TypeEntry) 
} 
  
$PoshRS_RunspacePoolCleanup.Runspace.Open()         
$PoshRS_RunspacePoolCleanup.Runspace.SessionStateProxy.SetVariable("PoshRS_RunspacePoolCleanup",$PoshRS_RunspacePoolCleanup)     
$PoshRS_RunspacePoolCleanup.Runspace.SessionStateProxy.SetVariable("PoshRS_RunspacePools",$PoshRS_RunspacePools) 
$PoshRS_RunspacePoolCleanup.Runspace.SessionStateProxy.SetVariable("ParentHost",$Host) 
$PoshRS_RunspacePoolCleanup.PowerShell = [PowerShell]::Create().AddScript({
    #Routine to handle completed runspaces
    Do { 
        $DisposePoshRS_RunspacePools=$False  
        If ($PoshRS_RunspacePools.Count -gt 0) { 
            #$ParentHost.ui.WriteVerboseLine("$($PoshRS_RunspacePools | Out-String)")
            [System.Threading.Monitor]::Enter($PoshRS_RunspacePools.syncroot) 
            Foreach($RunspacePool in $PoshRS_RunspacePools) {                
                #$ParentHost.ui.WriteVerboseLine("RunspacePool <$($RunspacePool.RunspaceID)> | MaxJobs: $($RunspacePool.MaxJobs) | AvailJobs: $($RunspacePool.AvailableJobs)")
                If (($RunspacePool.AvailableJobs -eq $RunspacePool.MaxJobs) -AND $PoshRS_RunspacePools.LastActivity.Ticks -ne 0) {
                    If ((Get-Date).Ticks - $RunspacePool.LastActivity.Ticks -gt $PoshRS_RunspacePoolCleanup.Timeout) {
                        #Dispose of runspace pool
                        $RunspacePool.RunspacePool.Close()
                        $RunspacePool.RunspacePool.Dispose()
                        $RunspacePool.CanDispose = $True
                        $DisposePoshRS_RunspacePools=$True
                    }
                } Else {
                    $RunspacePool.LastActivity = (Get-Date)
                }               
            }       
            #Remove runspace pools
            If ($DisposePoshRS_RunspacePools) {
                $TempCollection = $PoshRS_RunspacePools.Clone()
                $TempCollection | Where {
                    $_.CanDispose
                } | ForEach {
                    #$ParentHost.ui.WriteVerboseLine("Removing runspacepool <$($_.RunspaceID)>")
                    [void]$PoshRS_RunspacePools.Remove($_)
                }
            }
            Remove-Variable TempCollection
            [System.Threading.Monitor]::Exit($PoshRS_RunspacePools.syncroot)
        }
        Start-Sleep -Milliseconds 5000     
    } while ($PoshRS_RunspacePoolCleanup.Flag)
})
$PoshRS_RunspacePoolCleanup.PowerShell.Runspace = $PoshRS_RunspacePoolCleanup.Runspace
$PoshRS_RunspacePoolCleanup.Handle = $PoshRS_RunspacePoolCleanup.PowerShell.BeginInvoke() 
#endregion Cleanup Routine

#region Load Public Functions
Try {
    Get-ChildItem "$ScriptPath\Public" -Filter *.ps1 | Select -Expand FullName | ForEach {
        $Function = Split-Path $_ -Leaf
        . $_
    }
} Catch {
    Write-Warning ("{0}: {1}" -f $Function,$_.Exception.Message)
    Continue
}
#endregion Load Public Functions

#region Load Private Functions
Try {
    Get-ChildItem "$ScriptPath\Private" -Filter *.ps1 | Select -Expand FullName | ForEach {
        $Function = Split-Path $_ -Leaf
        . $_
    }
} Catch {
    Write-Warning ("{0}: {1}" -f $Function,$_.Exception.Message)
    Continue
}
#endregion Load Private Functions

#region Format and Type Data
Update-FormatData "$ScriptPath\TypeData\PoshRSJob.Format.ps1xml"
Update-TypeData "$ScriptPath\TypeData\PoshRSJob.Types.ps1xml"
#endregion Format and Type Data

#region Aliases
New-Alias -Name ssj -Value Start-RSJob -Force
New-Alias -Name gsj -Value Get-RSJob -Force
New-Alias -Name rsj -Value Receive-RSJob -Force
New-Alias -Name rmsj -Value Remove-RSJob -Force
New-Alias -Name spsj -Value Stop-RSJob -Force
New-Alias -Name wsj -Value Wait-RSJob -Force
#endregion Aliases

#region Handle Module Removal
$ExecutionContext.SessionState.Module.OnRemove ={
    $PoshRS_jobCleanup.Flag=$False
    $PoshRS_RunspacePoolCleanup.Flag=$False
    #Let sit for a second to make sure it has had time to stop
    Start-Sleep -Seconds 1
    $PoshRS_jobCleanup.PowerShell.EndInvoke($PoshRS_jobCleanup.Handle)
    $PoshRS_jobCleanup.PowerShell.Dispose()    
    $PoshRS_RunspacePoolCleanup.PowerShell.EndInvoke($PoshRS_RunspacePoolCleanup.Handle)
    $PoshRS_RunspacePoolCleanup.PowerShell.Dispose()
    Remove-Variable PoshRS_JobId -Scope Script -Force
    Remove-Variable PoshRS_Jobs -Scope Script -Force
    Remove-Variable PoshRS_jobCleanup -Scope Script -Force
    Remove-Variable PoshRS_RunspacePoolCleanup -Scope Script -Force
    Remove-Variable PoshRS_RunspacePools -Scope Script -Force
}
#endregion Handle Module Removal

#region Export Module Members
$ExportModule = @{
    Alias = @('gsj','rmsj','rsj','spsj','ssj','wsj')
    Function = @('Get-RSJob','Receive-RSJob','Remove-RSJob','Start-RSJob','Stop-RSJob','Wait-RSJob')
    Variable = @('PoshRS_JobId','PoshRS_Jobs','PoshRS_jobCleanup','PoshRS_RunspacePoolCleanup','PoshRS_RunspacePools')
}
Export-ModuleMember @ExportModule
#endregion Export Module Members