Speeding up powershell report via jobs?

Ask your PowerShell-related questions, including questions on cmdlet development!
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 7 years and 2 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Speeding up powershell report via jobs?

Post by jvierra »

"Problem Signature 04: Microsoft.Exchange.Diagnostics"

Why are you using Exchange?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Speeding up powershell report via jobs?

Post by jvierra »

Use this method as it will keep you from making mistakes. Your posted code has syntax errors'
  1. $jobscript = {
  2.     Param ($text)
  3.    
  4.     Import-Module ActiveDirectory
  5.     Get-ADGroup -properties name, Created, Modified, DistinGuishedName, Members -Filter "Name -like '*$text*'"
  6. }
  7. $completed = {
  8.     Param ($Job)
  9.     $Groups = Receive-Job -Job $Job
  10.     $progressbaroverlay1.Visible = $false
  11.     Load-DataGridView -DataGridView $datagridviewGroup -Item $Groups
  12.     Add-Logs -text "$($Groups.count) groups found which match $($textboxGroupSearch.Text)"
  13.     Add-logs -text "Done retreiving Groups"
  14. }
  15. $update = {
  16.     # occurs on timer tick so update the progress bar
  17. }
  18.  
  19. Add-JobTracker -Name 'JobGroupSearch'  -ArgumentList $textboxGroupSearch.Text -JobScript $jobscript -CompletedScript $completed -UpdateScript $update
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Speeding up powershell report via jobs?

Post by jvierra »

Note that only the $jobscript is run in the job. The other blocks run in the form.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Speeding up powershell report via jobs?

Post by stevens »

Guessing? It crashes so I can't even debug(?!)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Speeding up powershell report via jobs?

Post by jvierra »

stevens wrote:Guessing? It crashes so I can't even debug(?!)
Your script solutions are guesswork. The crash is due to trying to load and call Exchange CmdLets incorrectly. What does this have to do with the current question?
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Speeding up powershell report via jobs?

Post by stevens »

I have no clue why Exchange would make it crash and even suspect it isn't the correct root cause.

If I use for job:
"Get-ADGroup -properties name, Created, Modified, DistinGuishedName, Members -Filter {Name -like '*Appv*' }"
no crash: everything works fine

But if I use:
$NameContainsWildcard = ('*' + $($textboxGroupSearch.Text) + '*')
"Get-ADGroup -properties name, Created, Modified, DistinGuishedName, Members -Filter {Name -like $NameContainsWildcard }"
it crashed, with an Exchange error. Beats me.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Speeding up powershell report via jobs?

Post by stevens »

So I had to use $using: and now it works
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Speeding up powershell report via jobs?

Post by stevens »

Ok, so it works for me, these jobs.
Importing the functions via hard coded paths is something I really don't like. That means I'd have to copy over the ps1 next to the executable which looks ugly to me and is prone to errors.

Is there no way around it but that approach?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Speeding up powershell report via jobs?

Post by jvierra »

^The reason "using" works is because you have other errors in your code.

Attached is a complete example using a Param statement. Run it and you will see it works and loads the grid correctly and allows the grid to be sortable and searchable.

I found the "using" issue when I made a mistake i n building the form. Once I cleared my mistake the Param statement worked correctly.

As for loading functions ,they can be injected into the job with a little effort.

Large job scripts should be add to a project as external files and they will be deployed by the installer. Why jam all of that code in one file. No programmer would do that.
Attachments
Test-DGVADJob.psf
(28.81 KiB) Downloaded 132 times
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Speeding up powershell report via jobs?

Post by stevens »

Thanks. I'll have look at the form.
Can you clarify what you mean by "Large job scripts should be add to a project as external files and they will be deployed by the installer."
The only thing I would like to avoid is having ps1 files (only for my jobs) next to my exe (which contains my project), is that possible?
=> Ideallically, I'd put the shared functions into a ps1 which I call in the launch of the form, then read the ps1 in a job.
Last edited by stevens on Tue Jan 31, 2017 2:21 am, edited 1 time in total.
This topic is 7 years and 2 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