Skip to content

Instantly share code, notes, and snippets.

@LawrenceHwang
Last active March 9, 2016 19:50
Show Gist options
  • Save LawrenceHwang/722122a5c1e1d8d74a96 to your computer and use it in GitHub Desktop.
Save LawrenceHwang/722122a5c1e1d8d74a96 to your computer and use it in GitHub Desktop.
<#
This is not a complete code and inteneded to be used with other codes.
The goal is to grab a collection of names, vms..etc and then:
- Do some work to the collection in smaller batches
- The batch work should be sent to background for "parallel" execution
- Have a way to check the status of each object in the batch work
- If completed work(s) is found, pull next object(s) from the collection
The status of last batch is not tracked. Improvements can be made in future version.
#>
# The list is a collection of names/ vms... etc
$list = Get-Content -Path "C:\temp\list.txt"
# Next is used for setting the max number of object in each bath of execution
[int]$Next = 5
# The time (sec) before next status check
[int]$WaitTime = 5
# log location
[string] $log = 'C:\Temp\LoopError.log'
# log error or not. Default $false
[bool]$LogError = $false
# Creating the variables used in the script
# Using [System.Collections.ArrayList] object so the members can be added/ removed
[System.Collections.ArrayList]$CurrentWorkingSet = @()
[System.Collections.ArrayList]$Tracking = @()
[int]$i = 0
[int]$j = 0
# Iterate through all objects in the $list
for ($i ; $i -lt $list.count){
# The $j loop here has the actual work inside.
# It also controls the number of object to advance by $Next
for ($j=0 ; $j -lt $Next ; $j++){
if (($i+$j -ge $list.Count)){
# Stop when the the loop exceeds the number of the objects
# Might need to add additional monitoring here
break
}
# Add the object to the $CurrentWorkingSet array for monitoring
$CurrentWorkingSet.Add($list[($i+$j)]) | Out-Null
### Do the actual work here. ####
### Should send the work to the background job.
### Should support error handling and error log here
### write-verbose "<$i,$j,$next>: Working on $($list[$i+$j])" -BackgroundColor Magenta
<#
try{
Test-Connection -Count 1 -ComputerName $list[($i+$j)] -Quiet -ErrorAction stop
}
catch{
if ($LogError){
$list[($i+$j)] | Out-File -FilePath $log -Append
$Error[-1].Exception | Out-File -FilePath $log -Append
}
}
#>
} # End of for loop for $j
# Here we wait some time for the above actions to have a chance to complete
Start-Sleep -Seconds $WaitTime
# Perform tests against the objects in $CurrentWorkingSet and change the $Next value based on the result.
# For example, 2 out of 5 computers completed then $Next = 2
# Also remove the 'COMPLETED' object from the $CurrentWorkingSet
# Test against $c in the $currentworkingset
$Tracking = @()
$Next = 0
foreach ($c in $CurrentWorkingSet){
# This is to find out the objects that still in the working state
try{
### Put the status test here with the if statement ###
if (<###########>){
$state = $true
}
else{
$state = $false
$Next++
}
} # End of try
catch{
Write-Warning "Something went wrong with $c"
} # End of catch
if ($state){
# When the $state is $true, meaning the work is still happening, adding it to $Tracking
$Tracking += $c
}
else{
# This is the object in the completed (maybe failed?) state
}
} # End of foreach
$CurrentWorkingSet = $Tracking
$i = $i + $j
} # End of for loop of $i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment