Skip to content

Instantly share code, notes, and snippets.

@HCRitter
Last active June 26, 2023 12:37
Show Gist options
  • Save HCRitter/35c543364da6bb9aeaf56df18f23e39a to your computer and use it in GitHub Desktop.
Save HCRitter/35c543364da6bb9aeaf56df18f23e39a to your computer and use it in GitHub Desktop.
Jobsandmore
function Write-MultiLineFeedback {
[CmdletBinding()]
param (
$Feedback
)
begin {
$CursorPosition = $Host.UI.RawUI.CursorPosition.Y
$Feedback.ForEach({
$PSItem.Line = $CursorPosition
$CursorPosition++
})
$JobList = $Feedback | Foreach-Object {
$SetItem = $PSItem
$job = Start-Job -ScriptBlock $SetItem.Job
$SetItem.Set = $Job.Id
$job
}
Start-Sleep -Seconds 1
}
process {
do{
$Feedback.ForEach({
if($PSItem.LastDisplayMessage -ne $(Get-Job -ID $PSItem.Set).ChildJobs.Information[-1]){
$PSItem.LastDisplayMessage = $(Get-Job -ID $PSItem.Set).ChildJobs.Information[-1]
$host.UI.RawUI.CursorPosition=@{X = 0;Y=$PSItem.Line}
$Host.UI.Write($PSItem.LastDisplayMessage)
}
Start-Sleep -Milliseconds 50
})
}until($JobList.ChildJobs.State -notcontains "running")
#make sure every information stream has been written
$Feedback.ForEach({
if($PSItem.LastDisplayMessage -ne $(Get-Job -ID $PSItem.Set).ChildJobs.Information[-1]){
$PSItem.LastDisplayMessage = $(Get-Job -ID $PSItem.Set).ChildJobs.Information[-1]
$host.UI.RawUI.CursorPosition=@{X = 0;Y=$PSItem.Line}
$Host.UI.Write($PSItem.LastDisplayMessage)
}
Start-Sleep -Milliseconds 50
})
}
end {
$host.UI.RawUI.CursorPosition=@{X = 0;Y=$CursorPosition}
}
}
$Temp = @([PSCustomObject]@{
Set = $Null
Job = {
Write-Host "Task Sequence 1.1"
Start-Sleep -Seconds (Get-Random -Maximum 7 -Minimum 1)
Write-Host "Task Sequence 1.2"
Start-Sleep -Seconds (Get-Random -Maximum 7 -Minimum 1)
Write-Host "Task Sequence 1.3"
Start-Sleep -Seconds (Get-Random -Maximum 7 -Minimum 1)
Write-Host "Task Sequence 1 finished"
}
Line = $Null
LastDisplayMessage = $null
},[PSCustomObject]@{
Set = $Null
Job = {
Write-Host "Task Sequence 2.1"
Start-Sleep -Seconds (Get-Random -Maximum 7 -Minimum 1)
Write-Host "Task Sequence 2.2"
Start-Sleep -Seconds (Get-Random -Maximum 7 -Minimum 1)
Write-Host "Task Sequence 2.3"
Start-Sleep -Seconds (Get-Random -Maximum 7 -Minimum 1)
Write-Host "Task Sequence 2 finished"
}
Line = $Null
LastDisplayMessage = $null
},[PSCustomObject]@{
Set = $Null
Job = {
Write-Host "Task Sequence 3.1"
Start-Sleep -Seconds (Get-Random -Maximum 7 -Minimum 1)
Write-Host "Task Sequence 3.2"
Start-Sleep -Seconds (Get-Random -Maximum 7 -Minimum 1)
Write-Host "Task Sequence 3.3"
Start-Sleep -Seconds (Get-Random -Maximum 7 -Minimum 1)
Write-Host "Task Sequence 3 finished"
}
Line = $Null
LastDisplayMessage = $null
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment