More PowerShell Runspace Fun
In the last article, we began to explore how to use custom runspaces in our PowerShell scripting. Is still believe that using cmdlets like Start-Job
and Invoke-Command
are preferred. Let PowerShell do the work. But, there may be situations where advanced scripters want to have more granular control over the process.
Last time, we looked at using runspaces sequentially. Like a background job, the results remain in the runspace, and you may want to keep things separate. You can also easily re-run the code in the runspace.
Here's a simple script.
#MeasureFolder.ps1
Param([string]$Path = '.')
$stat = Get-ChildItem -Path $Path -Recurse -File | Measure-Object -Property Length -Sum -Average
[PSCustomObject]@{
PSTypeName = 'folderInfo'
Path = Convert-Path $Path
Files = $stat.Count
Size = $stat.Sum
Average = $stat.Average
Computername = [System.Environment]::MachineName
Date = Get-Date
}
Want to read the full issue?