Author Posts

October 12, 2015 at 9:44 am

I am a user of Powershell Studio and I am attempting to build a progress bar using examples found in their article found here .

Basically they are using the background jobs part of powershell and building a framework around it. The framework calls on three scriptblocks. Jobscript (which constitutes the job function), updatescript which tells the framework what to do on update, and lastly completescript tells what to do when the job completes.

In their examples they use increments (as a percentage) in the form of:

for($i = 0; $i -lt 100; $i++)
{
#Do some work
Start-Sleep -Milliseconds 100
#Output Progress
$i + 1
}
}

for their jobscript.

For their updatescript, it follows on from that.

-UpdateScript {
Param($Job)
$results = Receive-Job -Job $Job | Select-Object -Last 1

if($results -is [int])
{
$progressbar1.Value = $results
}
}


and it adds to the progressbar gui control value.

However in my jobscript the function is slightly different.

Param (
$TxtCSVjob,
$Txtoudetailsjob)

$user_list = Get-Content $txtCSVjob
foreach ($user_id in $user_list)
{
$user_details = Get-ADUser -Identity $user_id
$user_details.DistinguishedName | Move-ADObject -TargetPath $Txtoudetailsjob
}

}

Basically its moving a user listed in a csv file from one OU to another.

I know that in my update script I will have an instruction to increment the progressbar control in my winform, but I am struggling to find an efficient way to carry out the primary task of moving the user as well as updating the progress bar to show status..

I know that this is a custom framework, But the real matter that I need to wrap my head around is how to increment in powershell. I am hoping that I can get some guidance...I have looked around the interwebs, but I am don't think I am using the right terms to search. Any help is most appreciated.

Kind regards,

WY

October 12, 2015 at 9:47 am

We'll see who replies here, but as an intermediate suggestion, have you considered asking this question in the forums on http://sapien.com? Since they wrote the product, and your example, they might be a quicker source of information for you.

October 12, 2015 at 12:09 pm

Thank you very much Don for replying.

In answer to your question I did post to the Sapien forum and they gave a lot of help when it came to the actual framework but when it came to explaining powershell concepts, they were not so receptive, saying that they do not have the manpower to help out with explaining powershell concepts. I realise after looking at the framework I would like some ideas to increment a step.

I am hoping that I have some better luck in getting some guidance in the concepts (even if its just a pointer to a URL.

Thanks

Wei-Yen Tan

October 12, 2015 at 2:27 pm

The way I'm reading this, the -JobScript is just returning a number. When you run Receive-Job in the -UpdateScript, it reads that number returned by the -JobScript

For example:

    -JobScript {
        for($i = 0; $i -lt 100; $i++)
        { 
            #Do some work
            Start-Sleep -Milliseconds 100 
            #Output Progress
            $i + 1 #This is where they are returning the incremental number
        } 
    }

I would suspect you would need to do something like this in your process

Param (
    $TxtCSVjob,
    $Txtoudetailsjob
)

$user_list = Get-Content $txtCSVjob

# Count the number of lines so we know how many users we are working with
$inputcount = $user_list.count

# Divide the 100 by the number of users so we know how much to increment for each
# user we have processed, in case we are working on a number that is not exactly 100
# users
$incrementvalue = 100/$inputcount

foreach ($user_id in $user_list)
{
    $user_details = Get-ADUser -Identity $user_id
    $user_details.DistinguishedName | Move-ADObject -TargetPath $Txtoudetailsjob

    # Increment our $status variable by our incremental value for the user just completed
    $status = $status + $incrementvalue

    # Return our status
    $status
}

Of course the above is all theoretical. I have no example to work with.

October 12, 2015 at 7:03 pm

I will start with a simple example

 
1..10|%{Write-progress -Activity "Doing Stuff" -percentcomplete ($_/10*100)
 Start-Sleep 1}

The code does 10 things (count from 1 -10). each time it counts it updates a progress bar with the percent it is complete and sleeps for 1 second.

The example from SAPIEN is doing something similar but the example framework is reading the status from the output from the -Jobscript. If you do something like this

-jobscript{
     Param (
    $TxtCSVjob,
    $Txtoudetailsjob)

    $user_list = Get-Content $txtCSVjob

    foreach ($user_id in $user_list){
        $i = $i+1
        $user_details = Get-ADUser -Identity $user_id
        $user_details.DistinguishedName | Move-ADObject -TargetPath $Txtoudetailsjob
        $i/$user_list.count*100
    }
}

You would move each user and then update the progress bar