Background Jobs (Job Tracking Control Set)

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
clhumphr
Posts: 19
Last visit: Wed Jun 16, 2021 5:23 am

Background Jobs (Job Tracking Control Set)

Post by clhumphr »

I have read and re-read the post: https://www.sapien.com/blog/2012/05/16/ ... sive-forms however, I am still having an issue of getting the results of my -jobScipt to return, I have resorted to use write-host to see if I can get any output but all I am left with are blank lines. I have tried to use brakepoints along with Debug which just skips right over the -jobscript block, I assume this is because it's already in a separate thread. if anyone could take a look at this and tell me where I have hosed it up I would be very grateful.
Below is my Button Click Event for the job.

Thanks,
  1. $buttonStartJob_Click={
  2.  
  3.     $global:OutputFileName = "C:\test\EMail_Results33.txt"
  4.     $global:InputFilePath = "C:\test\EmailList.txt"
  5.    
  6.     $buttonStartJob.Enabled = $false
  7.    
  8.     $EmailList = Get-Content $InputFilePath
  9.    
  10.     $Count = $EmailList.count
  11.  
  12.     Add-JobTracker -Name "GetADInfo" `
  13.     -JobScript {
  14.         for ($a = 0; $a -le $Count - 1; $a++)
  15.             {
  16.                 $Email = $EmailList[$a]
  17.                
  18.                 $Filter = "EmailAddress -like ""*$Email*"" "
  19.                 $UserInfo = Get-AdUser -Filter $filter
  20.    
  21.                 $UserResults =+ $UserInfo
  22.                
  23.                 $NTID = $UserInfo.SamAccountName
  24.                
  25.                 $UserData = Get-AdUser -Filter {SamAccountName -eq $NTID} -Property SamAccountName , EmailAddress, Title   
  26.                
  27.                 Write-Host UserData = $UserData
  28.                
  29.                 $NTID = $UserData.samaccountname
  30.                 $EAddress = $UserData.EmailAddress
  31.                 $JobTitle = $UserData.Title                
  32.                
  33.                 $Results1 = $Results1 + $NTID + "," + $EAddress + "," + $JobTitle + "`n"
  34.                 $Results1  
  35.                
  36.                 Start-Sleep -Milliseconds 100
  37.                 Write-Host UserData = $Results1
  38.                 $a + 1
  39.             }
  40.  
  41.     }`
  42.     -CompletedScript {
  43.             Param($Job)
  44.  
  45.                 $buttonStartJob.Enabled = $true
  46.                 $Results = Receive-Job -Job $Job -Keep
  47.                
  48.                 Write-Host Results $Results
  49.                
  50.                 $buttonStartJob.ImageIndex = -1
  51.                 $buttonStartJob.Enabled = $true
  52.                
  53.                 Write-Host is complete
  54.     }`
  55.     -UpdateScript {
  56.             Param($Job)
  57.  
  58.             if($null -ne $buttonStartJob.ImageList)
  59.                 {
  60.                     if($buttonStartJob.ImageIndex -lt $buttonStartJob.ImageList.Images.Count - 1)
  61.                         {
  62.                             $buttonStartJob.ImageIndex += 1
  63.                         }
  64.                     else
  65.                         {
  66.                             $buttonStartJob.ImageIndex = 0     
  67.                         }
  68.                 }
  69.     }`
  70.     -ArgumentList $null
  71.    
  72. }

jvierra
Posts: 14841
Last visit: Wed Jun 23, 2021 11:17 am
Answers: 11
Has voted: 3 times
Been upvoted: 10 times

Re: Background Jobs (Job Tracking Control Set)

Post by jvierra »

You are never asking for the job data anywhere. Job only return information when asked.

See: "help about_jobs" for instructions on how to use a job.

User avatar
clhumphr
Posts: 19
Last visit: Wed Jun 16, 2021 5:23 am

Re: Background Jobs (Job Tracking Control Set)

Post by clhumphr »

  1. -CompletedScript {
  2. Param($Job)
  3. $buttonStartJob.Enabled = $true
  4. $Results = Receive-Job -Job $Job -Keep
  5. Write-Host Results $Results
I thought that this was asking for the Job data, is that malformed or not correct?
Reviewing the Get-help about_Jobs just in case I did not fully understand about doing the receive-job :)

Thanks,

jvierra
Posts: 14841
Last visit: Wed Jun 23, 2021 11:17 am
Answers: 11
Has voted: 3 times
Been upvoted: 10 times

Re: Background Jobs (Job Tracking Control Set)

Post by jvierra »

$results will be a collection of objects. What tis it you are expecting.

Stat by testing your job script at a CLI prompt until you get it t return what you need.

The script you posted cannot ever return anything. $count and $emailist are never defined.

jvierra
Posts: 14841
Last visit: Wed Jun 23, 2021 11:17 am
Answers: 11
Has voted: 3 times
Been upvoted: 10 times

Re: Background Jobs (Job Tracking Control Set)

Post by jvierra »

Here is a better format that cn help you keep it sorted out.
  1. $jobScript = {
  2.     $emaillist = 'user@deomain.com', 'user@deomain.com', 'user@deomain.com'
  3.     foreach($email in $emaillist){
  4.         Get-AdUser -Filter "EmailAddress -like '*$email*'" -Properties SamAccountName, EmailAddress, Title
  5.     }
  6. }
  7. $completedScript = { ... }
  8. Add-JobTracker -Name GetADInfo -JobScript $jobScript -COmpletedScript $completedScript ...
Write-Host is useless in a job.

You must either include the list in the job or pass it as an argument.

User avatar
clhumphr
Posts: 19
Last visit: Wed Jun 16, 2021 5:23 am

Re: Background Jobs (Job Tracking Control Set)

Post by clhumphr »

Results that are expected:
First: an object with the SamAccountName
Second: The First Object is used with Get-ADuser returning an object with these properties (SamAccountName, EmailAddress, Title)

The For loop works fine outside the job; it returns what I need. However the script window is non-responsive which is why I am trying to drop this into a job.

$count and $emaillist are defined at the top of the Click event :)
  1. $global:OutputFileName = "C:\test\EMail_Results33.txt"
  2. $global:InputFilePath = "C:\test\EmailList.txt"
  3.  
  4. $buttonStartJob.Enabled = $false
  5.  
  6. $EmailList = Get-Content $InputFilePath
  7.  
  8. $Count = $EmailList.count
thanks.

User avatar
clhumphr
Posts: 19
Last visit: Wed Jun 16, 2021 5:23 am

Re: Background Jobs (Job Tracking Control Set)

Post by clhumphr »

Thanks a ton for the help

this did the trick
You must either include the list in the job or pass it as an argument.
this is outside the job so how in the h@## could the job know about the list...it can't...duh thanks again for the help I could not see the trees for the forest...... :oops:

Locked