Created
August 10, 2023 20:16
-
-
Save romero126/be03f7d1f4c9c9ee93da78738f13dcd7 to your computer and use it in GitHub Desktop.
Monitor-Object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Monitor-Object { | |
param( | |
[Parameter()] | |
[Alias('Begin')] | |
[ScriptBlock]$BeginBlock = { | |
param( | |
[Parameter(Mandatory)] | |
[object] $InputObject | |
) | |
return $InputObject | |
}, | |
[Parameter(Mandatory)] | |
[Alias('ScriptBlock', 'SB')] | |
[ScriptBlock]$ProcessBlock, | |
[Parameter(ValueFromPipeline)] | |
[Alias('i')] | |
[object] $InputObject, | |
[Parameter()] | |
[Alias('ft')] | |
[switch] $FormatTable, | |
[Parameter()] | |
[Alias('ri')] | |
[int] $RefreshInterval = 30 | |
) | |
begin { | |
# Get Cursor Position | |
$esc = $([char]27) | |
# Save Cursor Position | |
Write-Host "$ESC[s" # (SCO) | |
#Write-Host "$($ESC)7" # (DEC) | |
Write-Host 'Please wait while gathering data' -NoNewLine | |
$inputObjectList = New-Object System.Collections.ArrayList | |
} | |
process { | |
# Unroll the objects | |
foreach ($i in $InputObject) { | |
$_result = . $BeginBlock -InputObject $i | |
$null = $inputObjectList.Add( $_result ) | |
} | |
} | |
end { | |
try { | |
do { | |
$newObjectList = New-Object System.Collections.ArrayList | |
foreach ($obj in $InputObjectList) { | |
Write-Host "$esc[2K$esc[0GProcessing $($inputObjectList.IndexOf($obj)) of $( $inputObjectList.Count)" -NoNewline -ForegroundColor Cyan | |
$_result = . $ProcessBlock -InputObject $obj | |
$null = $newObjectList.Add( $_result ) | |
} | |
$inputObjectList = $newObjectList | |
# | |
Write-Host "Position", $host.UI.RawUI.CursorPosition.Y | |
Write-Host "Current Position $esc[6n" | |
Write-Host "$esc[2K$esc[0GCompleted Processing $( $inputObjectList.Count) Records`n" -ForegroundColor Cyan | |
# Clear to end console (This should only happen after i push objects to screen) | |
# Restore Cursor Position | |
Write-Host "$ESC[u" -NoNewline # (SCO) | |
#Write-Host "$($ESC)8" -NoNewline # (DEC) | |
Write-Host "$ESC[2K$ESC[0G$ESC[0J" -NoNewline | |
if ($FormatTable) { | |
$inputObjectList | ft -a | |
} else { | |
$inputObjectList | |
} | |
Write-Host "" | |
Write-Host "Ctrl+X to Exit" | |
Write-Host "Ctrl+A To Add Clipboard to Pipeline" | |
# Refreshing in x Seconds | |
if ($RefreshInterval) { | |
for ($i = $RefreshInterval; $i -gt 0; $i--) | |
{ | |
if ([console]::KeyAvailable) | |
{ | |
$key = [console]::ReadKey($true) | |
if ($key.Modifiers -eq 'Control') { | |
if ($key.Key -eq 'X') { | |
return | |
} | |
elseif ($key.Key -eq 'A') { | |
Write-Host 'Todo Add some logic here.' | |
Start-Sleep 3 | |
} | |
} | |
} | |
Start-Sleep 1 | |
} | |
Write-Host "$esc[2K$esc[0G" -NoNewline | |
} | |
} until ($false) | |
} | |
catch { | |
} | |
finally { | |
Write-Host "" | |
} | |
Write-Host "Completed" | |
return | |
} | |
} | |
1..10 | Monitor-Object -BeginBlock { | |
param( | |
[object] $InputObject | |
) | |
return [PSCustomObject][Ordered]@{ | |
Name = "Object $InputObject" | |
Value = $InputObject | |
Iteration = 0 | |
} | |
} -ProcessBlock { | |
param( | |
[object] $InputObject | |
) | |
$InputObject.Iteration++ | |
$InputObject | |
Start-Sleep -MilliSeconds 250 | |
} -RefreshInterval 3 -FormatTable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment