Need Help with Job Tracker

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 3 years and 7 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
james.bernard
Posts: 16
Last visit: Wed Oct 26, 2022 8:29 am

Need Help with Job Tracker

Post by james.bernard »

Product, version and build: 5.7.174.0
32 or 64 bit version of product: 64
Operating system: Win 10
32 or 64 bit OS: 64

Hey all
I am still having some trouble getting the Start-Job button to work. I was curious about the parameters that need to be passed on and the argument list. Does this need to be any variables you have in the script? Below is what I am trying to run. It will start to run and then give me errors the the $ComputerName variable is null, but none of the text that i have to be displayed in the rick text box will show, even if it fails, i.e. "Please Wait". Below is the code I am using, can someone show me what I am doing wrong with this?
Thanks

Code: Select all

$buttonStartJob_Click={
	
	$buttonStartJob.Enabled = $false
	
	#Create a New Job using the Job Tracker
	$paramAddJobTracker = @{
		Name	  = 'InfoCheckTest'
		JobScript = {
			#--------------------------------------------------
			#TODO: Set a script block
			#Important: Do not access form controls from this script block.
			
			Param ($ComputerName) #Pass any arguments using the ArgumentList parameter
			$ComputerName = $textbox1.Text
			$outputbox2.Text = ""
			if (test-connection -computername $ComputerName -count 1 -quiet)
			{
				$outputbox2.Text = "Please Wait `r `n"
				
				$computerSystem = get-wmiobject Win32_ComputerSystem -ComputerName $ComputerName
				$computerUser = Get-WmiObject -ComputerName $ComputerName -Class Win32_ComputerSystem | Select-Object UserName
				$computerBIOS = get-wmiobject Win32_BIOS -ComputerName $ComputerName
				$computerOS = get-wmiobject Win32_OperatingSystem -ComputerName $ComputerName
				$computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $ComputerName -Filter "DeviceID='C:'"
				$wmi = Get-WmiObject -class Win32_OperatingSystem -computer $ComputerName
				
				
				$Mod = $computerSystem.Model -replace "32373A0", "M92p" -replace "3237EF9", "M92p" -replace "3167BC8", "M71e" -replace "10A4S00R0D", "M93" -replace "3664AK9", "M72e" -replace "3267B69", "M72e Tiny" -replace "0833AL2", "M70e"
				$Uptime = (Get-Date) - ($wmi.ConvertToDateTime($wmi.lastbootuptime))
				$Display = "Uptime: " + $Uptime.Days + " days, " + $Uptime.Hours + " hours, " + $Uptime.Minutes + " minutes"
				$Man = $computerSystem.Manufacturer
				$HDD = "{0:N2}" -f ($computerHDD.FreeSpace/1GB) + "GB"
				$User = $computerUser.UserName
				
				
				$outputBox2.AppendText("Manufacturer: $Man `r`n")
				$outputBox2.AppendText("Model: $Mod `r`n")
				$outputBox2.AppendText("$Display `r`n")
				$outputBox2.AppendText("Free Drive Space: $HDD `r`n")
				$outputBox2.AppendText("Current User: $User `r`n")
			}
			else
			{
				$outputBox2.text = "Computer is offline."
			}
			
			for ($i = 0; $i -lt 50; $i++) { Start-Sleep -Milliseconds 100 }
			
			#--------------------------------------------------
		}
		ArgumentList = $null
		CompletedScript = {
			Param ([System.Management.Automation.Job]$Job)
			#$results = Receive-Job -Job $Job 
			#Enable the Button
			$buttonStartJob.ImageIndex = -1
			$buttonStartJob.Enabled = $true
		}
		UpdateScript = {
			Param ([System.Management.Automation.Job]$Job)
			$results = Receive-Job -Job $Job -Keep
			#Animate the Button
			if ($null -ne $buttonStartJob.ImageList)
			{
				if ($buttonStartJob.ImageIndex -lt $buttonStartJob.ImageList.Images.Count - 1)
				{
					$buttonStartJob.ImageIndex += 1
				}
				else
				{
					$buttonStartJob.ImageIndex = 0
				}
			}
		}
	}
	
	Add-JobTracker @paramAddJobTracker
}
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Need Help with Job Tracker

Post by localpct »

You're not passing arguments to the script block

ArgumentList = $null

I would honestly start out with an easier job then build up
it took me a good month to figure it out the way it should be, if not longer

and

Param ($ComputerName) #Pass any arguments using the ArgumentList parameter
$ComputerName = $textbox1.Text

The $computername is being passed , before it's being defined
james.bernard
Posts: 16
Last visit: Wed Oct 26, 2022 8:29 am

Re: Need Help with Job Tracker

Post by james.bernard »

Appreciate the reply. So I guess thats where I am getting lost. What exactly needs to be in the ArgumentList? Is it any variables I have in the scriptblock? I am not running any arguments on this, currently, unless I am misunderstanding what is being asked.
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Need Help with Job Tracker

Post by localpct »

start here
  1. $buttonStartJob_Click={
  2.    
  3.     $buttonStartJob.Enabled = $false
  4.     $computername = $textbox1.Text
  5.     #Create a New Job using the Job Tracker
  6.     $paramAddJobTracker = @{
  7.         Name      = 'JobName'
  8.         JobScript = {
  9.             #--------------------------------------------------
  10.             #TODO: Set a script block
  11.             #Important: Do not access form controls from this script block.
  12.            
  13.             Param ($computername) #Pass any arguments using the ArgumentList parameter
  14.            
  15.             test-connection -computername $ComputerName -count 1 -quiet
  16.            
  17.             #--------------------------------------------------
  18.         }
  19.         ArgumentList = $computername
  20.         CompletedScript = {
  21.             Param ([System.Management.Automation.Job]$Job)
  22.             $textbox2.Text = $results = Receive-Job -Job $Job
  23.             #Enable the Button
  24.             $buttonStartJob.ImageIndex = -1
  25.             $buttonStartJob.Enabled = $true
  26.         }
  27.         UpdateScript = {
  28.             Param ([System.Management.Automation.Job]$Job)
  29.             #$results = Receive-Job -Job $Job -Keep
  30.             #Animate the Button
  31.             if ($null -ne $buttonStartJob.ImageList)
  32.             {
  33.                 if ($buttonStartJob.ImageIndex -lt $buttonStartJob.ImageList.Images.Count - 1)
  34.                 {
  35.                     $buttonStartJob.ImageIndex += 1
  36.                 }
  37.                 else
  38.                 {
  39.                     $buttonStartJob.ImageIndex = 0
  40.                 }
  41.             }
  42.         }
  43.     }
  44.    
  45.     Add-JobTracker @paramAddJobTracker
  46. }
james.bernard
Posts: 16
Last visit: Wed Oct 26, 2022 8:29 am

Re: Need Help with Job Tracker

Post by james.bernard »

So I did move the computername variable and added in a message so I can see that it is pulling the PC name (I created a small form to test all this in) It runs and outputs the "Trying to ping $computername" into $outputBox2 but then just hangs and doesn't ever output the results and the Start Job button never goes back to Enabled, but stays grayed out. I am adding in the revised script for the Start Job button. Not sure what I am doing incorrectly. I have seen examples online and I do not see any major differences between what I am running and the examples of working scripts.

Code: Select all

$buttonStartJob_Click = {
	
	$buttonStartJob.Enabled = $false
	$computername = $textbox1.Text
	$outputBox2.Text = "Trying to ping $computername"
	#Create a New Job using the Job Tracker
	Add-JobTracker -Name "JobName" `
				   -JobScript {
		#--------------------------------------------------
		#TODO: Set a script block
		#Important: Do not access form controls from this script block.
		
		Param ($computername) #Pass any arguments using the ArgumentList parameter
		
		#for($i = 0; $i -lt 50; $i++){ Start-Sleep -Milliseconds 100 } 
		test-connection -computername $computername -count 4
		#--------------------------------------------------
	}`
				   -CompletedScript {
		Param ($Job)
		$results = Receive-Job -Job $Job
		$outputBox2.Text = $results
		
		#Enable the Button
		$buttonStartJob.ImageIndex = -1
		$buttonStartJob.Enabled = $true
	}`
				   -UpdateScript {
		Param ($Job)
		#$results = Receive-Job -Job $Job -Keep
		#Animate the Button
		if ($buttonStartJob.ImageList -ne $null)
		{
			if ($buttonStartJob.ImageIndex -lt $buttonStartJob.ImageList.Images.Count - 1)
			{
				$buttonStartJob.ImageIndex += 1
			}
			else
			{
				$buttonStartJob.ImageIndex = 0
			}
		}
	}
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Need Help with Job Tracker

Post by jvierra »

You have to provide the arguments to teh function.

Code: Select all

$buttonStartJob_Click = {
	
	$buttonStartJob.Enabled = $false
	$outputBox2.Text = "Trying to ping $computername"

    Add-JobTracker -Name JobName `
        -ArgumentList $textbox1.Text `
    	-JobScript {
    		Param ($computername)
    		test-connection -computername $computername -count 4
    		#--------------------------------------------------
    	}`
    	-CompletedScript {
    		Param ($Job)

            $results = Receive-Job -Job $Job
    		$outputBox2.Text = $results
    		$buttonStartJob.ImageIndex = -1
    		$buttonStartJob.Enabled = $true
    	}`
    	-UpdateScript {
    		Param ($Job)
    		if ($buttonStartJob.ImageList -ne $null){
    			if ($buttonStartJob.ImageIndex -lt $buttonStartJob.ImageList.Images.Count - 1){
    				$buttonStartJob.ImageIndex += 1
    			}else{
    				$buttonStartJob.ImageIndex = 0
    			}
    		}
    	}
}
james.bernard
Posts: 16
Last visit: Wed Oct 26, 2022 8:29 am

Re: Need Help with Job Tracker

Post by james.bernard »

Thanks for the reply. I added that in and the results are the same. I added in a line to clear the outputbox (outside of the scriptblock) before the results are added in, but even that does not happen, and the button never goes back to enabled, signaling that the completed script portion has been reached, so it does not look like even that simple scriptblock is completing. I am not sure what I am missing, as I am using the edits to the code that you have provided

Code: Select all

$buttonStartJob2_Click = {
	
	$buttonStartJob2.Enabled = $false
	$computername = $textbox1.Text
	$outputBox2.Text = "Trying to ping $computername"
	
	Add-JobTracker -Name JobName `
				   -ArgumentList $textbox1.Text `
				   -JobScript {
		Param ($computername)
		test-connection -computername $computername -count 4
		#--------------------------------------------------
	}`
				   -CompletedScript {
		Param ($Job)
		
		$results = Receive-Job -Job $Job
		$outputBox2.Text = ""
		$outputBox2.Text = $results
		$buttonStartJob.ImageIndex = -1
		$buttonStartJob.Enabled = $true 
	}`
				   -UpdateScript {
		Param ($Job)
		if ($buttonStartJob2.ImageList -ne $null)
		{
			if ($buttonStartJob2.ImageIndex -lt $buttonStartJob2.ImageList.Images.Count - 1)
			{
				$buttonStartJob2.ImageIndex += 1
			}
			else
			{
				$buttonStartJob2.ImageIndex = 0
			}
		}
	}
}
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Need Help with Job Tracker

Post by Alexander Riedel »

Which powershell version are you actually using? Windows PowerShell 5.1 or PowerShell 7?
Alexander Riedel
SAPIEN Technologies, Inc.
james.bernard
Posts: 16
Last visit: Wed Oct 26, 2022 8:29 am

Re: Need Help with Job Tracker

Post by james.bernard »

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

Re: Need Help with Job Tracker

Post by jvierra »

Can you pleas post your PSF file.
This topic is 3 years and 7 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