Skip to content

Instantly share code, notes, and snippets.

@PrateekKumarSingh
Created August 8, 2019 01:29
Show Gist options
  • Save PrateekKumarSingh/6325a96e167157304700f04be00c5ee7 to your computer and use it in GitHub Desktop.
Save PrateekKumarSingh/6325a96e167157304700f04be00c5ee7 to your computer and use it in GitHub Desktop.
$Timer = [System.Diagnostics.Stopwatch]::StartNew()
$maxThreads = 20
$RunSpacePool = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspacePool(1, $maxThreads )
$Jobs = New-Object System.Collections.ArrayList
$RunSpacePool.Open()
$Worker = {
param($Item)
if (-not(Get-Item $Item | Where-Object PSIsContainer)) {
Get-Item $Item -Stream *
# "{0} no" -f $item
}
elseif (Get-Item $Item | Where-Object PSIsContainer) {
# "{0} yes" -f $item
# Get-ChildItem 'C:\Program Files\Common Files\System\ado\' -Recurse | ForEach-Object { Get-Item -Path "$($_.fullname)" -Stream * }
Get-ChildItem $Item -Recurse | ForEach-Object { Get-Item -Path "$($_.fullname)" -Stream * }
}
}
Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '3'" | ForEach-Object {
Get-ChildItem "$($_.DeviceID)\" -Depth 1 | foreach {
$PowerShell = [System.Management.Automation.PowerShell]::Create()
$PowerShell.RunspacePool = $RunSpacePool
[void]$PowerShell.AddScript($Worker)
[void]$PowerShell.AddArgument($_.FullName)
$Handle = $PowerShell.BeginInvoke()
$tempObj = '' | Select-Object PowerShell, Handle
$tempObj.PowerShell = $PowerShell
$tempObj.Handle = $Handle
[void]$Jobs.Add($tempObj)
"$_ in runspace"
}
}
# $Result = 1..6 | ForEach {
# $PS = [powershell]::Create()
# $PS.RunspacePool = $RSP
# [void]$PS.AddScript({
# Param($i)
# While ($True) {
# Start-Sleep -Milliseconds 1000
# If ($i%2) {BREAK}
# }
# }).AddArgument($_)
# $List.Add(([pscustomobject]@{
# Id = $_
# PowerShell = $PS
# Handle = $PS.BeginInvoke()
# }))
# }
# Start-Sleep -Seconds 2
$Flag = 'static','nonpublic','instance'
while($true){
0..19 | ForEach {
$_Worker = $Jobs[0].PowerShell.GetType().GetField('worker',$Flag)
$Worker = $_Worker.GetValue($Jobs[$_].PowerShell)
$_CRP = $worker.GetType().GetProperty('CurrentlyRunningPipeline',$Flag)
$CRP = $_CRP.GetValue($Worker)
$State = If ($Jobs[$_].handle.IsCompleted -AND -NOT [bool]$CRP) {
'Completed'
}
ElseIf (-NOT $Jobs[$_].handle.IsCompleted -AND [bool]$CRP) {
'Running'
}
ElseIf (-NOT $Jobs[$_].handle.IsCompleted -AND -NOT [bool]$CRP) {
'NotStarted'
}
[pscustomobject]@{
Id = (([int]$_)+1)
HandleComplete = $Jobs[$_].handle.IsCompleted
PipelineRunning = [bool]$CRP
State = $State
}
}
sleep -Milliseconds 1000; cls
}
#Write-Host "waiting for the jobs to complete."
$res = $Jobs | Foreach-Object {
$_.PowerShell.EndInvoke($_.Handle);
$_.PowerShell.Dispose()
}
$Timer.Stop()
Write-Output "Time Elapsed: $($Timer.Elapsed.Minutes) Minutes $($Timer.Elapsed.Seconds) Seconds."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment