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 }
- $PrinterName_Click={
- # Install Printer above
- $PrinterName.Enabled = $false
- #Create a New Job using the Job Tracker
- Add-JobTracker -Name 'JobName' `
- -JobScript {
- #--------------------------------------------------
- #TODO: Set a script block
- #Input username / password
- $matnr = Read-Host -Prompt "Please type username"
- $password = Read-Host -Prompt "Please type password" -AsSecureString
- $pwcl = [System.Runtime.InteropServices.marshal]::PtrToStringAuto([System.Runtime.InteropServices.marshal]::SecureStringToBSTR($password))
- # Username/password is saved and when the pc is restarted the username/password combo for printer is still there.
- cmdkey /add:ServerAdress /user:Domain(FQDN)\$matnr /pass:$pwcl
- net use \\ServerAdress /persistent:yes
- Add-Printer -ConnectionName \\ServerAdress\Printer_Name
- #Important: Do not access form controls from this script block.
- Param($Argument1)#Pass any arguments using the ArgumentList parameter
- for($i = 0; $i -lt 50; $i++){ Start-Sleep -Milliseconds 100 }
- #--------------------------------------------------
- }`
- -CompletedScript {
- Param($Job)
- #$results = Receive-Job -Job $Job
- #Enable the Button
- $PrinterName.ImageIndex = -1
- $PrinterName.Enabled = $true
- }`
- -UpdateScript {
- Param($Job)
- #$results = Receive-Job -Job $Job -Keep
- #Animate the Button
- if($null -ne $PrinterName.ImageList)
- {
- if($PrinterName.ImageIndex -lt $PrinterName.ImageList.Images.Count - 1)
- {
- $PrinterName.ImageIndex += 1
- }
- else
- {
- $PrinterName.ImageIndex = 0
- }
- }
- }`
- -ArgumentList $null
- }