$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.
This topic is 4 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
dlaurora
Posts: 31
Last visit: Wed Oct 02, 2019 12:51 pm

$null result on responsive form

Post by dlaurora »

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.

Code: Select all

[/Codebox
$textboxFind_TextChanged= {
#TODO: Place custom script here
$loadplane.Visible = $true
Add-JobTracker -Name "ADuser"`
-JobScript {
$result = "$($textboxiFind.text)*" #take the info that i type inside a textbox e.j: jhon.contoso
$global:progresspreference = 'SilentlyContinue'
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
}`
-CompletedScript {
Param ($Job)
$Users = Receive-Job -Job $Job
Write-Host $Users ## i'm using this just to see if the jub is retreving something
$textboxFind.SelectionLength = 0
$textboxFind.SelectionStart = $result.Length
#Update-Combobox $textboxFind -Items $users
$loadplane.Visible = $false
}`
-UpdateScript {
Param ($Job)
$results = Receive-Job -Job $Job | Select-Object -Last 1
}
}


]


Thanks, best regards.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: $null result on responsive form

Post by jvierra »

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
Last visit: Wed Oct 02, 2019 12:51 pm

Re: $null result on responsive form

Post by dlaurora »

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

Best regards.
User avatar
dlaurora
Posts: 31
Last visit: Wed Oct 02, 2019 12:51 pm

Re: $null result on responsive form

Post by dlaurora »

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.                        -JobScript {
  3.             $global:progresspreference = 'SilentlyContinue'
  4.            
  5.             Get-ADUser -Server $using:dombox.Text  -Identity $using:usr -Properties PasswordLastSet, SamAccountName
  6.            
  7.         } -ArgumentList $usr, $dombox.Text
  8.  
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: $null result on responsive form

Post by jvierra »

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
Last visit: Wed Oct 02, 2019 12:51 pm

Re: $null result on responsive form

Post by dlaurora »

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
Last visit: Wed Oct 02, 2019 12:51 pm

Re: $null result on responsive form

Post by dlaurora »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: $null result on responsive form

Post by jvierra »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: $null result on responsive form

Post by jvierra »

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
Last visit: Wed Oct 02, 2019 12:51 pm

Re: $null result on responsive form

Post by dlaurora »

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.
This topic is 4 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked