Jobtracker if error occours show messagebox

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
User avatar
fluffydinosaurs
Posts: 3
Last visit: Thu Aug 17, 2023 1:42 am
Has voted: 1 time

Jobtracker if error occours show messagebox

Post by fluffydinosaurs »

Hey i hope someone can help im trying to get a messagebox to show if an error happens in a "Form - Jobtracker" with a "Button - Start job".
Im installing a printer with a mix of cmd and then powershell to do the installation of the printer.
The user is asked for there username and password and then it connect to the print server and then it runs the powershell commands to install the printer.

Most of the time this works without any errors, but sometimes Spool services is causing a problem, they typed the wrong username/password combo or something else.

If this happens it would be great to have a messagebox show up and tell them the error code in raw format.
I do understand that its not nice or bad practis to do it that way but it sometimes makes it easier to error search the problem.

I tried using this website and the step about "Display errors in a message box" but i cant figure out how to make it work with "Add-JobTracker".
sapien.com/blog/2015/01/15/manage-errors-in-a-gui-application/

I tried using -ErrorAction and -ErrorVariable but that didnt work either since i probably used it wrong. devblogs.microsoft.com/powershell/erroraction-and-errorvariable/

I also have tried catch with

catch {
[System.Windows.Forms.MessageBox]::Show("$Error[0]");}

I added the [System.Windows.Forms.MessageBox]::Show("$Error[0]"); in these places without it working as hoped:
-CompletedScript
-UpdateScript
After "Add-Printer -ConnectionName"
And below this
:Param($Argument1)#Pass any arguments using the ArgumentList parameter

for($i = 0; $i -lt 50; $i++){ Start-Sleep -Milliseconds 100 }


  1. $PrinterName_Click={
  2.     # Install Printer above
  3.     $PrinterName.Enabled = $false
  4.     #Create a New Job using the Job Tracker
  5.     Add-JobTracker -Name 'JobName' `
  6.     -JobScript {
  7.         #--------------------------------------------------
  8.         #TODO: Set a script block
  9.  
  10.     #Input username / password
  11.         $matnr = Read-Host -Prompt "Please type username"
  12.         $password = Read-Host -Prompt "Please type password" -AsSecureString
  13.         $pwcl = [System.Runtime.InteropServices.marshal]::PtrToStringAuto([System.Runtime.InteropServices.marshal]::SecureStringToBSTR($password))
  14.  
  15.         # Username/password is saved and when the pc is restarted the username/password combo for printer is still there.
  16.         cmdkey /add:ServerAdress /user:Domain(FQDN)\$matnr /pass:$pwcl
  17.         net use \\ServerAdress /persistent:yes
  18.         Add-Printer -ConnectionName \\ServerAdress\Printer_Name
  19.  
  20.         #Important: Do not access form controls from this script block.
  21.  
  22.         Param($Argument1)#Pass any arguments using the ArgumentList parameter
  23.  
  24.         for($i = 0; $i -lt 50; $i++){ Start-Sleep -Milliseconds 100 }
  25.  
  26.         #--------------------------------------------------
  27.     }`
  28.     -CompletedScript {
  29.         Param($Job)
  30.         #$results = Receive-Job -Job $Job
  31.         #Enable the Button
  32.         $PrinterName.ImageIndex = -1
  33.         $PrinterName.Enabled = $true
  34.     }`
  35.     -UpdateScript {
  36.         Param($Job)
  37.         #$results = Receive-Job -Job $Job -Keep
  38.         #Animate the Button
  39.         if($null -ne $PrinterName.ImageList)
  40.         {
  41.             if($PrinterName.ImageIndex -lt $PrinterName.ImageList.Images.Count - 1)
  42.             {
  43.                 $PrinterName.ImageIndex += 1
  44.             }
  45.             else
  46.             {
  47.                 $PrinterName.ImageIndex = 0
  48.             }
  49.         }
  50.     }`
  51.     -ArgumentList $null
  52. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Jobtracker if error occours show messagebox

Post by jvierra »

Print servers install the print driver automatically. You do not have to do individual installations. This has been true since Windows Server 2000.

Why are you trying to do something that has been unnecessary for almost 20 years?
User avatar
fluffydinosaurs
Posts: 3
Last visit: Thu Aug 17, 2023 1:42 am
Has voted: 1 time

Re: Jobtracker if error occours show messagebox

Post by fluffydinosaurs »

These are for students private computers so we can not adminstrate them with sccm, intune or other solutions like this so they need to manually install it. For the company owned computers we do of course auto install the printers.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Jobtracker if error occours show messagebox

Post by jvierra »

The Windows Print Spooler install a simple driver if no driver has been installed. YOU do not need to do anything locally just be sure the driver is installed on the server, and it will install automatically when you connect to the printer.

What you are trying to do is totally unnecessary. In corporate systems we have not installed print drivers for decades. You are trying to use an obsolete method.

I recommend talking to your IT department or if you are the only tech then here is a place to start to learn how to use Windows printers.

Here is a simple video that will teach you how to work with the print server to make all print drivers available without needing to install drivers on workstations.

https://www.youtube.com/watch?v=4yiSQaOyRus

Trying to script driver installation without a fairly deep amount of technical skill with the NT operating system is going to be a difficult thing to manage. We never write code to do things the OS does for us. Of course, home users wouldn't ever know this as standalone PCs have to have a full installation of printer software.
User avatar
fluffydinosaurs
Posts: 3
Last visit: Thu Aug 17, 2023 1:42 am
Has voted: 1 time

Re: Jobtracker if error occours show messagebox

Post by fluffydinosaurs »

I did find the answer after a bit of searching on the forum with "Jobtracker messagebox" i did stumble upon this post and on page 3 i found my solution.
viewtopic.php?p=61956#p61956
The only change i did was $res to $ErrorCodes so i would later understand why i have it there.
This gives me just what i wanted the error code for both cmd and powershell. By pure luck the text looks usefull too for the user if they type wrong username/password.
Just gonna do some simple tweaks to add some more info for them but problem got solved :)
  1. $Competed = {
  2.     Param($job)
  3.    $res =  $job | Receive-Job
  4.    [System.Windows.Forms.MessageBox]::Show( $res.Count)
  5. }
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