$null result on responsive form

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.
User avatar
dlaurora
Posts: 31
Joined: Wed Oct 26, 2016 10:43 am

$null result on responsive form

Post by dlaurora » Wed Mar 20, 2019 8:52 am

Hello Guys,

I have an issue when I work with a responsive form and I can't figure why. I have all the Jobs tracker functions loaded, the timer, etc.

When I try to find a specific user that I type on a textbox, the job get-aduser -identity $result apparently it is empty but if I run this outside the job script it works. Do you know if I have to import somehow the $result variable inside the Jobscript?

here is the part of the code that it is failing, let me know if it will be better to upload the entire form.
  1. [/Codebox
  2.  
  3. $textboxFind_TextChanged= {
  4.  
  5.     #TODO: Place custom script here
  6.  
  7.         $loadplane.Visible = $true
  8.  
  9.         Add-JobTracker -Name "ADuser"`
  10.  
  11.                        -JobScript {
  12.  
  13.                 $result = "$($textboxiFind.text)*"  #take the info that i type inside a textbox e.j: jhon.contoso
  14.  
  15.             $global:progresspreference = 'SilentlyContinue'
  16.  
  17.         Get-ADUser -Identity $result  ### Here appears the error, no mather what I use or put inside the $result variable it is always null but if I manually set the user it works E.j Get-ADUser -Identity jhon.contoso
  18.  
  19. }`
  20.  
  21.                    -CompletedScript {
  22.  
  23.         Param ($Job)
  24.  
  25. $Users = Receive-Job -Job $Job
  26.  
  27. Write-Host $Users ## i'm using this just to see if the jub is retreving something
  28.  
  29. $textboxFind.SelectionLength = 0
  30.  
  31. $textboxFind.SelectionStart = $result.Length
  32.  
  33. #Update-Combobox $textboxFind -Items $users
  34.  
  35. $loadplane.Visible = $false
  36.  
  37.                                                           }`
  38.  
  39.                        -UpdateScript {
  40.  
  41.             Param ($Job)
  42.  
  43.             $results = Receive-Job -Job $Job | Select-Object -Last 1
  44.  
  45.         }
  46.  
  47. }
  48.  
  49.  
  50.  
  51.  
  52.  
  53. ]
  54.  
  55.  
  56.  
  57.  
  58.  
  59. Thanks, best regards.

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

Re: $null result on responsive form

Post by jvierra » Wed Mar 20, 2019 9:13 am

You cannot reference the GUI from a job. You must use the ArgumentsList" to pass the information into the job script.

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

Re: $null result on responsive form

Post by dlaurora » Wed Mar 20, 2019 9:32 am

Thanks, it worked!!! forgot to check if Add-jobtracker had argument list.

Best regards.

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

Re: $null result on responsive form

Post by dlaurora » Wed Apr 10, 2019 6:07 am

Hello Again,

I'm trying now to add another argument into the same search, but the same is always null when I call it inside the add-jobtracker

I have a combobox ($dombox) that contains multiple domains names, need to select it when I try to search for specific users.

The second variable, $usr, is passing without issues.
  1. Add-JobTracker -Name "getuser" `
  2.  
  3.                        -JobScript {
  4.  
  5.             $global:progresspreference = 'SilentlyContinue'
  6.  
  7.            
  8.  
  9.             Get-ADUser -Server $using:dombox.Text  -Identity $using:usr -Properties PasswordLastSet, SamAccountName
  10.  
  11.            
  12.  
  13.         } -ArgumentList $usr, $dombox.Text
  14.  
  15.  

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

Re: $null result on responsive form

Post by jvierra » Wed Apr 10, 2019 6:22 am

This is how to pass arguments:

Code: Select all

-JobScript {
    Param(
        $USR,$DomText
    )
    Get-ADUser -Server $DomText -Identity $USR -Properties PasswordLastSet, SamAccountName
} -ArgumentList $usr, $dombox.Text

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

Re: $null result on responsive form

Post by dlaurora » Wed Apr 10, 2019 7:11 am

Still passing the argument that way the result is $null

ERROR: Cannot validate argument on parameter 'Server'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
ERROR: + CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
ERROR: + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser
ERROR: + PSComputerName : localhost
ERROR:

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

Re: $null result on responsive form

Post by dlaurora » Wed Apr 10, 2019 7:54 am

Never mind, I manage to solve it, I've saved the result of the combo box into a variable, and then add the same into the argument list.

Thanks for the assistance.

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

Re: $null result on responsive form

Post by jvierra » Wed Apr 10, 2019 8:03 am

One of your arguments is null. Try tying in a specific string to prove the code works.

You have $user which is a local variable. Where are you setting $usr? Is the textbox name correct.

This will help:

Code: Select all

# before Start-Job place these lines
[System.Windows.Forms.MessageBox]::Show($usr)
[System.Windows.Forms.MessageBox]::Show($dombox.Text)

#...  other code

-JobScript {
    Param(
        $USR,$DomText
    )
    Get-ADUser -Server $DomText -Identity $USR -Properties PasswordLastSet, SamAccountName
} -ArgumentList $usr, $dombox.Text
Learning how to debug is critical when using advanced programming methods. Consider this a chance to improve your debugging skills.

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

Re: $null result on responsive form

Post by jvierra » Wed Apr 10, 2019 8:16 am

Ok. You can't use the $using scope modifier with an object property. It only works with value types or objects.

Using a param statement is best and eliminates many dependencies. It is also cleaner and easier to understand when viewed.

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

Re: $null result on responsive form

Post by dlaurora » Thu May 09, 2019 5:15 am

Hello again,

I have a new question regarding Arguments, i'm trying to pass credentials, in the main load form it asks for credentials and I save it into a variable, just a simple line $creds = get-credentials, then I try to add the same into the job passing through but during the process, it prompts again for credentials and I receive this error

ERROR: Remove-Job: The command cannot remove the job with the job ID 3 because the job is not finished. To remove the job, first, stop the job, or use the Force
ERROR: parameter.
ERROR: Parameter name: Job
AD Tool V 1.2.psf (1008, 6): ERROR: At Line: 1008 char: 6
ERROR: + Remove-Job -Job $psObject.Job
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : InvalidOperation: (System.Manageme...n.PSRemotingJob:PSRemotingJob) [Remove-Job], ArgumentException
ERROR: + FullyQualifiedErrorId : JobObjectNotFinishedCannotBeRemoved,Microsoft.PowerShell.Commands.RemoveJobCommand
ERROR:

Here is the code were i try to pass the credentials through Argument List

Code: Select all

Add-JobTracker -Name 'ADuser' -ArgumentList $dc,$Creds,$textboxFind.Text `
					   -JobScript {
			Param(
				$dc,
				$Creds,
				$username
			)
			$global:progresspreference = 'SilentlyContinue'
			Get-ADUser -Server $dc -Credential $Creds -Identity $username -Properties PasswordLastSet, SamAccountName
}
If I remove Server and Credentials it works without issues, Before the Job, I display the content of each variable and they are not $null

Thanks, best regards.

Locked