Job-Tracker using textbox as argument list

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 10 years and 8 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
spostema
Posts: 7
Last visit: Wed Jun 26, 2019 8:35 am

Job-Tracker using textbox as argument list

Post by spostema »

Good morning,

I'm loving the Job-Tracker control set, however I'm struggling to pass a textbox.text value as the argument. I'm working on an Exchange 2010 mailbox export as a background job and in order to monitor the job progress I must monitor specific mailbox exports and receive the job to grab percent complete to a progress bar.

I've tried many different ways using $arg, $arg[0] in the body of the jobscript...

Here's the code I'm having issues with, any suggestions would be greatly appreciated!

PowerShell Code
Double-click the code block to select all.
-JobScript {
   		#--------------------------------------------------
		#TODO: Set a script block
		#Important: Do not access form controls from this script block.
    
		Param($Arg)#Pass any arguments using the ArgumentList parameter
    	. 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'
		connect-exchangeserver -auto
		Start-Sleep -Seconds 10
		do {
			$Status = Get-MailboxExportRequest -Mailbox $Arg[0] | Get-MailboxExportRequestStatistics | Select-Object -ExpandProperty $_.percentcomplete
			$Status
		} until(Get-MailboxExportRequest -Mailbox $arg[0] -Status Completed)
		
		
		#--------------------------------------------------
	}`
	-ArgumentList {$textboxUsername.Text } `
Thanks,

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

Re: Job-Tracker using textbox as argument list

Post by jvierra »

Steve - you are using $arg. The global arguments variable is called $args with an s on the end.

I recommend using a Param() instead as it will allow targeted named arguments.
User avatar
spostema
Posts: 7
Last visit: Wed Jun 26, 2019 8:35 am

Re: Job-Tracker using textbox as argument list

Post by spostema »

So I could use Param($textbox.text) in the Jobscript section?
User avatar
spostema
Posts: 7
Last visit: Wed Jun 26, 2019 8:35 am

Re: Job-Tracker using textbox as argument list

Post by spostema »

apologies for my ignorance, I've been staring at this for 3 days now...I tried Param($textbox.text) and Param($username = $textbox.text) and it comes back with the argument is null..
PowerShell Code
Double-click the code block to select all.
-JobScript {
   		#--------------------------------------------------
		#TODO: Set a script block
		#Important: Do not access form controls from this script block.
    
		Param($Username= $textboxUsername.Text) #Pass any arguments using the ArgumentList parameter
    	. 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'
		connect-exchangeserver -auto
		Start-Sleep -Seconds 10
		do {
			$Status = Get-MailboxExportRequest -Mailbox $Username | Get-MailboxExportRequestStatistics | Select-Object -ExpandProperty $_.percentcomplete
			$Status
		} until(Get-MailboxExportRequest -Mailbox $Username -Status Completed)
		
		
		#--------------------------------------------------
	}`
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Job-Tracker using textbox as argument list

Post by jvierra »

Start by reading up on how PowerShell uses arguments.

help about_Command_Syntax
help about_Parameters
help about_Functions
help about_Script_Blocks

to attempt to write a complex forms based script with no prior training in PowerShell is going to be very slow and painful. I highly recommend a book or video course to get you up to speed on the basics such as how to pass arguments.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Job-Tracker using textbox as argument list

Post by jvierra »

A quick answer would be to look at the help for the -arguments parameter. It will describe how arguments work.
User avatar
spostema
Posts: 7
Last visit: Wed Jun 26, 2019 8:35 am

Re: Job-Tracker using textbox as argument list

Post by spostema »

I appreciate you telling me to rtfm ;) I think I understand how arguments are passed. my problem is if I do Param($username = "username") the do loop works properly, updating the progress bar and exiting out clean....if I do Param($username = $textbox.text) it does not work. the comments in the scriptblock specifically say not to access form controls, so how the heck do I pass that textbox value to the do loop?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Job-Tracker using textbox as argument list

Post by jvierra »

I am afraid you do not quite understand how argumenhts work and you have not read the parts of the job which tell yyou how aruments with jobs are different.

#1 - you cannot reference an object in the form as a Parma value.

This construct:
Param{
$computer=$env:computername
}

... does not pass any value. It is just a default value that will be used if you do not pass a value. In you case $textbox1.Text is a value in the form. The rules are explicit for the Forms and the Job. The default job script explicitly states:
#Important: Do not access form controls from this script block

That is what you have done and it cannot be made to work. The job script has not ability to see anything in the callers environment. You must explicitly pass all arguments using the -arguments parameter. Please look at the examples. They describe all of the issues and caveats of doing this.

Passing arguments into any script block is a fundamental issue of PowerShell. There have been many blogs written to try and explain why this is. It is sufficient to say here that the script block is run in a separate namespace and all values and module must be explicitly passed into the scriptblock.

Example:
PowerShell Code
Double-click the code block to select all.
# scriptblock definition
$sb={
     Param(
      $myPath='c:\'
     )

     dir -path $myPath
}

# execute with default
Invoke-command -ScriptBlock $sb

# execute with argumentlist
Invoke-command -ScriptBlock $sb -ArgumentList c:\windows
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Job-Tracker using textbox as argument list

Post by jvierra »

Here is the less flexible form as I posted in my first answer. It uses the default script $args variable which is a leftover from PosH V1 which did not have a Param() statement.
PowerShell Code
Double-click the code block to select all.
# scriptblock definition
$sb={
     dir -path $args[0]
}


# execute with argumentlist
Invoke-command -ScriptBlock $sb -ArgumentList c:\windows
As I posted it is an array and is $args plural and must be referenced as an array. It is mostly used for bckwards compatibility but still works.

"ArgumentList" is not hash either It is an array.

-ArgumentList 'one',2,3,'other'
OR

-ArgumentList @('one',2,3,'other')


If you wrap in an array @() then you must add quotes to strings.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Job-Tracker using textbox as argument list

Post by jvierra »

spostema wrote:I appreciate you telling me to rtfm ;) I think I understand how arguments are passed. my problem is if I do Param($username = "username") the do loop works properly, updating the progress bar and exiting out clean....if I do Param($username = $textbox.text) it does not work. the comments in the scriptblock specifically say not to access form controls, so how the heck do I pass that textbox value to the do loop?
In your case you would not use a default argument unless it has meaning.

Param( $username )

Since this is automation you would have to let the code throw an exception and catch it after the execution since you cannot backup in a work or job stream. You cannot re-prompt for the value. This is one draw back of forms and requires that you do all validation in advance.

To send the argument just reference the control

-ScriptBlock ={Param($username)...} -ArgumentList $textboxUsername.Text

You were very close in the beginning but, since you didn't know how to use the arguments I noted that reading the docs would help you to get it straightened out. I was hoping to not have to rewrite the manual to explain what was wrong.

Can you see what you did wrong now? I think you will not forget how in the future. It gets many people because it is not as intuitive as they migh believe.

"C" programmers get it because they are very familiar with 'argc' and 'argv' and how argument arrays are passed. For non-programmers it can be very confusing. Add to that the fact that the script block cannot see outside of itself and you have a mystery - "How do I send information to a script block?"

Note that a simple script block does not have this issue.
PowerShell Code
Double-click the code block to select all.
$sb={dir $p}
$p='c:\'
& $sb
That is because it is not being executed in a different name space. Jobs are executed in a separate namespace per job.
This topic is 10 years and 8 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