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.
james.bernard
Posts: 11
Joined: Wed Apr 29, 2020 8:00 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: 320
Joined: Wed Sep 09, 2015 12:53 pm

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: 11
Joined: Wed Apr 29, 2020 8:00 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: 320
Joined: Wed Sep 09, 2015 12:53 pm

Re: Need Help with Job Tracker

Post by localpct »

start here
  1. $buttonStartJob_Click={
  2.  
  3.    
  4.  
  5.     $buttonStartJob.Enabled = $false
  6.  
  7.     $computername = $textbox1.Text
  8.  
  9.     #Create a New Job using the Job Tracker
  10.  
  11.     $paramAddJobTracker = @{
  12.  
  13.         Name      = 'JobName'
  14.  
  15.         JobScript = {
  16.  
  17.             #--------------------------------------------------
  18.  
  19.             #TODO: Set a script block
  20.  
  21.             #Important: Do not access form controls from this script block.
  22.  
  23.            
  24.  
  25.             Param ($computername) #Pass any arguments using the ArgumentList parameter
  26.  
  27.            
  28.  
  29.             test-connection -computername $ComputerName -count 1 -quiet
  30.  
  31.            
  32.  
  33.             #--------------------------------------------------
  34.  
  35.         }
  36.  
  37.         ArgumentList = $computername
  38.  
  39.         CompletedScript = {
  40.  
  41.             Param ([System.Management.Automation.Job]$Job)
  42.  
  43.             $textbox2.Text = $results = Receive-Job -Job $Job
  44.  
  45.             #Enable the Button
  46.  
  47.             $buttonStartJob.ImageIndex = -1
  48.  
  49.             $buttonStartJob.Enabled = $true
  50.  
  51.         }
  52.  
  53.         UpdateScript = {
  54.  
  55.             Param ([System.Management.Automation.Job]$Job)
  56.  
  57.             #$results = Receive-Job -Job $Job -Keep
  58.  
  59.             #Animate the Button
  60.  
  61.             if ($null -ne $buttonStartJob.ImageList)
  62.  
  63.             {
  64.  
  65.                 if ($buttonStartJob.ImageIndex -lt $buttonStartJob.ImageList.Images.Count - 1)
  66.  
  67.                 {
  68.  
  69.                     $buttonStartJob.ImageIndex += 1
  70.  
  71.                 }
  72.  
  73.                 else
  74.  
  75.                 {
  76.  
  77.                     $buttonStartJob.ImageIndex = 0
  78.  
  79.                 }
  80.  
  81.             }
  82.  
  83.         }
  84.  
  85.     }
  86.  
  87.    
  88.  
  89.     Add-JobTracker @paramAddJobTracker
  90.  
  91. }

james.bernard
Posts: 11
Joined: Wed Apr 29, 2020 8:00 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: 14543
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 11
Joined: Wed Apr 29, 2020 8:00 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: 7458
Joined: Tue May 29, 2007 4:43 pm

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: 11
Joined: Wed Apr 29, 2020 8:00 am

Re: Need Help with Job Tracker

Post by james.bernard »

I am using 5.1

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

Re: Need Help with Job Tracker

Post by jvierra »

Can you pleas post your PSF file.

Locked