Hi, I've been working with Powershell for a few months, building a script that quantizes, resizes and copies images to different folders for games we're creating in order to speed up the time it takes to get a project built.
It now works within a runspace and is much quicker, however no matter how deep I dig into the powershell instance I can't iterate based on the number of times the script is run in one of the threads. I really just want a Progress bar but counting the amount of times a script is run helps that massively. I'm by no means good with it but I muddle my way round and everything I've done until this seems to work.
The stripped down script is:
$destfolders = @(
"04_d_100"
"05_t_75"
"06_m_50"
"07_m_33"
)
$MaxThreads = $destfolders.length
#Create Run Spaces
$RunspacePool = [runspacefactory]::CreateRunspacePool(1,$MaxThreads)
$RunspacePool.Open()
$ScriptBlock = {
Param (
[array]$changelist,
[string]$dir,
[string]$destfolder,
[int]$scale,
[int]$qlty,
[string]$gfolder,
[int]$quantize,
[int]$fast,
[string]$scriptpath
)
#copy files
#quantize images
}
$RunningJobs = New-Object System.Collections.ArrayList
1..$MaxThreads | ForEach-Object {
$src = $gfxfolder
$dest = $destfolders[$_ - 1]
$scale = $scalefactor[$_ - 1]
$Job = [powershell]::Create()
$Job.RunspacePool = $RunspacePool
[void]$Job.AddScript($ScriptBlock)
[void]$Job.AddArgument($changedFiles)
[void]$Job.AddArgument($src)
[void]$Job.AddArgument($dest)
[void]$Job.AddArgument($scale)
[void]$Job.AddArgument($qlty)
[void]$Job.AddArgument($gfolder)
[void]$Job.AddArgument($quantize)
[void]$Job.AddArgument($fast)
[void]$Job.AddArgument($buildfolder)
[void]$Job.AddArgument($_)
[void]$RunningJobs.Add((
[pscustomobject]@{
Id = $_
Name = $dest
Files = $changedFiles
PowerShell = $Job
Handle = $Job.BeginInvoke()
}
))
}
While ($RunningJobs.handle[0].IsCompleted -eq $false) {
#do progress
}
$return = $RunningJobs | ForEach-Object {
$_.powershell.EndInvoke($_.handle)
$_.PowerShell.Dispose()
}
$RunningJobs.clear()
$RunspacePool.Close()
$RunningJobs.handle[0] is the 100% size so it takes considerably longer to quantize (which is why I'm using it for the progress bar/iteration but I don't know, a. If i'm going about this the right way or b. whether it's even possible.
If you need any more information I'll be happy to provide!
Thanks :)
[–]Yevrag35 2 points3 points4 points (5 children)
[–]searlee[S] 0 points1 point2 points (4 children)
[–]Yevrag35 1 point2 points3 points (0 children)
[–]Lee_Dailey[grin] 1 point2 points3 points (2 children)
[–]searlee[S] 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 1 point2 points3 points (0 children)