Jobtracker - GUI - updating datagridview

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 1 year 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
scavengerd2
Posts: 3
Last visit: Thu Feb 29, 2024 9:36 pm

Jobtracker - GUI - updating datagridview

Post by scavengerd2 »

So i have a GUI im trying to create jobs from that run as scheduled tasks on remote computers. The Scheduled tasks take some time to complete but i want once they are finished to update a diagrid view on the GUI from which they originated. This datagrid would be having multiple entries from multiple jobs. The environment lacks WinRM so the only way for access (I'm aware of) to these job status' is via schtasks.exe.
So the question is, i can get the job tracker items via $jobtrackerlist.job. But how can i make them announce they are complete? I would rather not be constantly be sending schtasks commands out at PC's and would rather them somehow report back that they are done.
Here's what I'm working with
I have a timer that will query the $jobtrackerlist and then update the datagridview with the info received. This will trigger once and run the duration of the GUI instance.
  1. function start-thisjob
  2. {
  3.     $namer = $source
  4.     Add-JobTracker -Name $namer `
  5.                    -JobScript {
  6.         Param ($namer)
  7.             $n = Get-Process $PID
  8.         while ($n.hasexited -ne $TRUE)
  9.             {
  10.                 $Jobs = $JobTrackerList.job
  11.             }
  12.     }`
  13.                    -CompletedScript {
  14.         Param ($Job)
  15.     }`
  16.    -UpdateScript {
  17.         Param ($Jobs)
  18.             $results = New-Object System.Collections.ArrayList
  19.             foreach ($item in $jobs)
  20.             {
  21.                 $thistracker = { } | select "Name", "Status", "Starttime"
  22.                 $thistracker.Name = $item.name
  23.                 $thistracker.status = $item.state
  24.                 $thistracker.starttime = $item.psbegintime
  25.                 $results += $thistracker
  26.                
  27.             }
  28.             Update-DataGridView -DataGridView $DGV_jobstatus -Item $results -AutoSizeColumns DisplayedCells
  29.         }
  30.        
  31.        
  32.        
  33.     }
  34.    
  35.     #This
Next I have this which is handling the tracking of each job that is created. simply attempting it to read a text file for when it contains "Complete" and then quit and have the jobtracker show the job as completed.
  1.     Add-JobTracker -Name $source `
  2.                    -JobScript {
  3.         Param ($source)
  4.         $PID
  5.         $n = $null
  6.         New-Item -ItemType file -Value $null -Path \\$using:source\c$\usmtfiles\status.txt
  7.     #   $statustext = Get-Content \\$using:source\c$\usmtfiles\status.txt
  8.        
  9.         while ($statustext -ne 'complete')
  10.         {
  11.             Start-Sleep 1000
  12.             $statustext = Get-Content \\$using:source\c$\usmtfiles\status.txt
  13.         }
  14.     }`
  15.                    -CompletedScript {
  16.         Param ($Job)
  17.         Write-Host $Job + 'Completed'
  18.     }`
  19.                    -UpdateScript {
  20.         Param ($Job)
  21.         #$jobs = $JobTrackerList.job | select State, Name, Progress, PSBeginTime
  22.         #Update-DataGridView -DataGridView $DGV_jobstatus -Item $jobs -AutoSizeColumns DisplayedCells
  23.     }
  24. }

Currently I'm getting just a single job showing up (the most recent job) and it just sits as running forever. Not looking for you to write this whole thing for me, just maybe a hint as to how the jobtracker Command interacts with the "Update-jobtracker". Like the update-jobtracker can interact with the form and can use information provided by the param $jobs?? Maybe this is not a use case for jobtracker?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Jobtracker - GUI - updating datagridview

Post by jvierra »

I recommend learning how to use the JobTracker by building and analyzing the example and reading the blog articles on how to use the JobTracker.

The code you posted doesn't make any sense based on your description of what you are trying to do.

When working with any new and complex technology it is always necessary do review all documentation and build and analyze all examples.

It also appears that you are adding an undefined variable for your jobs when the Update code hands you the current list of jobs. There is no need to create or redefine job objects, lists or arrays. That has all already been done for you. That is what the JobTracker custom control set does.

You will also be better off by not always recreating the grid contents, just add all of the jobs to the grid and then find them in the grid when a job changes its status.

If you are using remote jobs, then those jobs must be being created by some kind of remoting. How else could the jobs be remoted?
scavengerd2
Posts: 3
Last visit: Thu Feb 29, 2024 9:36 pm

Re: Jobtracker - GUI - updating datagridview

Post by scavengerd2 »

yes they are created by schtasks command
schtasks /create /s <pc> /tn task /xml s.xml

that's exactly what I'm looking for - to update an existing datagrid view with the new results. I'm sending a job to the remote machine via schtasks.exe and creating a job that monitors the status of that scheduled task.

I was not looking for the code segment to fix this. I just wanted maybe a slightly different explanation of how i can make the code from the job tracker script, continue to run as well as update the status of its datagridview row until the job is finished, and how to make that job report that it is finished.

I didn't just pick this up yesterday, I've read and analyzed for the better part of a year and was just seeking a bit of advice to a specific problem I've encountered that i cannot seem to grasp with all the code samples and things i have read.

Advice or the answer to any of the above would be beneficial. But ive been a member of this forum for years, this would be the first post ive made. It was not my first resort, more so, my last resort. But i'll keep trying, even a blind pig finds an acorn once in a while.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Jobtracker - GUI - updating datagridview

Post by jvierra »

Here is a simple demo of how to keep a DGV refreshed. Just run it and start the job then watch the DGV until the job completes.
Attachments
Demo-BindJobTracker.psf
(24.68 KiB) Downloaded 61 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Jobtracker - GUI - updating datagridview

Post by jvierra »

You are also mixing up scheduled jobs with scheduled tasks. PowerShell scheduled tasks use WMI/CIM and do not require remoting to be enabled. You also really do not need a job for this. You can loop through a dozen remote systems with "Get-ScheduledTaskInfo" and bind the results to a DGV.
scavengerd2
Posts: 3
Last visit: Thu Feb 29, 2024 9:36 pm

Re: Jobtracker - GUI - updating datagridview

Post by scavengerd2 »

Yes scheduled tasks - never worked with scheduled jobs.

forgive me if im mistaken, but CIM relies on WSMAN sessions and port 5985/5986 while WMI uses is using port 135 and DCOM. The latter is all that is available to be used. So CIM sessions are a nonstarter. So im thinking of using:

schtasks /query /tn mig /s pc /v /fo csv | convertfrom-csv

But ive found that it only takes one person to go offline and suddenly we have a hanging process.

This script is exactly what i needed though!! Was always thrown by

#Important: Do not access form controls from this script block.

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

Re: Jobtracker - GUI - updating datagridview

Post by jvierra »

Please take the time to review the documentation for the commands. CIM can use DCOM which is fundamentally the same as WMI uses. See the docs to learn how to use CIM. There are explicit examples.

Example 7 - https://docs.microsoft.com/en-us/powers ... rshell-7.2

You can do all systems in one command and all timeouts can be set and you will get correct statuses.

It is critical that you take to time to learn PowerShell beyond the simple things when you are attempting to do anything complex. Learning and understanding the Net Framework will make your scripting experience much less frustrating and it will help you to write only small amounts of code to accomplish very large effects.
This topic is 1 year 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