Update-Jobtracker help

Ask your PowerShell-related questions, including questions on cmdlet development!
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
dlaurora
Posts: 31
Joined: Wed Oct 26, 2016 10:43 am

Update-Jobtracker help

Post by dlaurora »

Hello everyone,

I trying to learn how to use the Job tracker function, till now what I did is to create the job, also was able to add arguments into it, but i'm not sure how to update it.

The below script it suppose to do an AD search, and update a combobox with users name, it partially works, sometimes it update it and sometimes it gets stuck, and I think it is case I don't stop the job or update it, so i went to the example that it is posted in responsive form page, tried to replicate it but did not work, could you please help me with it?

Thanks, best regards.

  1. [$textboxFind_TextChanged= {
  2.  
  3.     #TODO: Place custom script here
  4.  
  5.     $JobTrackerList = New-Object System.Collections.ArrayList
  6.  
  7.        
  8.  
  9.     if ($textboxFind.Text.Length -gt 2)
  10.  
  11.     {
  12.  
  13.         Update-Combobox -ComboBox $textboxFind -Items ""
  14.  
  15.         $textboxFind.SelectionLength = 0
  16.  
  17.         $textboxFind.SelectionStart = $textboxFind.Text.Length
  18.  
  19.         $ArgumentList = $textboxFind.Text
  20.  
  21.         $loadplane.Visible = $true
  22.  
  23.        
  24.  
  25.         Add-JobTracker -Name "ADuser" `
  26.  
  27.                        -JobScript {
  28.  
  29.            
  30.  
  31.             Param ($ArgumentList)
  32.  
  33.             $global:progresspreference = 'SilentlyContinue'
  34.  
  35.             Get-ADUser -ResultSetSize 6 -Filter { (GivenName -like $ArgumentList) -or (sn -like $ArgumentList) } -Properties PasswordLastSet, SamAccountName
  36.  
  37.            
  38.  
  39.         } -ArgumentList $ArgumentList `
  40.  
  41.                        -CompletedScript {
  42.  
  43.            
  44.  
  45.             Param ($Job)
  46.  
  47.             $Global:Users = Receive-Job -Job $Job
  48.  
  49.            
  50.  
  51.             If($global:users -eq $null) {$textbox.Text = "no funciono"} Else { $textbox.Text = $Global:Users}
  52.  
  53.            
  54.  
  55.             Update-Combobox -Items $Global:Users.SamAccountName -ComboBox $textboxFind -Append
  56.  
  57.             $loadplane.Visible = $false
  58.  
  59.             } `
  60.  
  61.                        -UpdateScript {
  62.  
  63.             Param ($Job)
  64.  
  65.             $results = Receive-Job -Job $Job | Select-Object -Last 1
  66.  
  67.            
  68.  
  69.         }
  70.  
  71.        
  72.  
  73.     }
  74.  
  75.     Else { $loadplane.Visible = $false }]

jvierra
Posts: 14453
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Update-Jobtracker help

Post by jvierra »

This would be closer to what you want:

Code: Select all

$textboxFind_TextChanged = {
    
    if ($textboxFind.Text.Length -gt 2) {
        
        $textboxFind.SelectionLength = 0
        $textboxFind.SelectionStart = $textboxFind.Text.Length
        $ArgumentList = $textboxFind.Text
        $loadplane.Visible = $true
        
        Add-JobTracker -Name ADuser -ArgumentList $ArgumentList `
            -JobScript {
                Param ($ArgumentList)
                $global:progresspreference = 'SilentlyContinue'
                Get-ADUser -ResultSetSize 6 -Filter "GivenName -like '$ArgumentList*' -or sn -like '$ArgumentList*'" -Properties PasswordLastSet, SamAccountName
                
            } `
            -CompletedScript {
                Param ($Job)
                $users = Receive-Job -Job $Job
                
                if ($users) {
                   $textbox.Text = $Global:Users 
                } else {
                   $textbox.Text = "no funciono"
                }
                
                Update-Combobox -Items $Global:Users.SamAccountName -ComboBox $textboxFind -Append
                $loadplane.Visible = $false
            } `
        -UpdateScript {
            Param ($Job)
            $results = Receive-Job -Job $Job | Select-Object -Last 1
        }
    }
    
}

User avatar
dlaurora
Posts: 31
Joined: Wed Oct 26, 2016 10:43 am

Re: Update-Jobtracker help

Post by dlaurora »

Thanks for the reply,

It is working but with some error that i have to check.

One last question, if you see I only do the task if the word written on the text box is greater than 2, do this, do I have to create a stop job each time that we write a letter? For example:

$textbox = "jho" , here it supposes to bring me the first 6 users that contain "jho" on it, but when I add the next letter, "jhon", the result is $null and the combobox is not updated

#
ERROR: Update-ComboBox : Cannot validate argument on parameter 'Items'. The argument is null. Provide a valid value for the argument, and then try running the
ERROR: command again.
AD Tool V 1.0.psf (203, 27): ERROR: At Line: 203 char: 27
ERROR: + Update-Combobox -Items $Users.SamAccountName -ComboBo ...
#

jvierra
Posts: 14453
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Update-Jobtracker help

Post by jvierra »

You must always start a new job. Using a job here is not a good idea.

User avatar
dlaurora
Posts: 31
Joined: Wed Oct 26, 2016 10:43 am

Re: Update-Jobtracker help

Post by dlaurora »

thanks, I'll try to find another way to make it responsive, I was trying to populate a variable at the start of the form with users but get-aduser -filter * -properties SamAccountName takes to long and the variable is always empty till it finishes.

Locked