Update-Jobtracker help

Ask your Windows 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 » Thu Mar 28, 2019 4:18 am

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.

Code: Select all

[$textboxFind_TextChanged= {
#TODO: Place custom script here
$JobTrackerList = New-Object System.Collections.ArrayList

if ($textboxFind.Text.Length -gt 2)
{
Update-Combobox -ComboBox $textboxFind -Items ""
$textboxFind.SelectionLength = 0
$textboxFind.SelectionStart = $textboxFind.Text.Length
$ArgumentList = $textboxFind.Text
$loadplane.Visible = $true

Add-JobTracker -Name "ADuser" `
-JobScript {

Param ($ArgumentList)
$global:progresspreference = 'SilentlyContinue'
Get-ADUser -ResultSetSize 6 -Filter { (GivenName -like $ArgumentList) -or (sn -like $ArgumentList) } -Properties PasswordLastSet, SamAccountName

} -ArgumentList $ArgumentList `
-CompletedScript {

Param ($Job)
$Global:Users = Receive-Job -Job $Job

If($global:users -eq $null) {$textbox.Text = "no funciono"} Else { $textbox.Text = $Global:Users}

Update-Combobox -Items $Global:Users.SamAccountName -ComboBox $textboxFind -Append
$loadplane.Visible = $false
} `
-UpdateScript {
Param ($Job)
$results = Receive-Job -Job $Job | Select-Object -Last 1

}

}
Else { $loadplane.Visible = $false }]

User avatar
jvierra
Posts: 13803
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Update-Jobtracker help

Post by jvierra » Thu Mar 28, 2019 4:49 am

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 » Thu Mar 28, 2019 5:09 am

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 ...
#

User avatar
jvierra
Posts: 13803
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Update-Jobtracker help

Post by jvierra » Thu Mar 28, 2019 5:45 am

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 » Thu Mar 28, 2019 5:55 am

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