how to convert get-printer to Boolean

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 11 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
pls-sapien
Posts: 31
Last visit: Tue Dec 19, 2023 12:55 am

how to convert get-printer to Boolean

Post by pls-sapien »

hello all
im trying to make the get-printer to return me a Boolean result so i can verify if a printer exists
( btw is there a projects marketplace so i can share E.P.I.C (Enhanced Printer Infrastructure Creator)? :mrgreen: )
ive tried to use this function:
  1. function CheckIfPrinterExists ($PrinterName)
  2. {
  3.     trap [Exception] {
  4.         return $false
  5.     }
  6.     $checking2 = Get-Printer ($PrinterName)
  7.     return $true
  8. }
but even thought i get an error "ERROR: Get-Printer : No MSFT_Printer objects found with property 'Name' equal to "PRINTERNAMEHERE""
it returns true

thanks for all the help
Sapien Rox!
Sean.
User avatar
juneblender
Posts: 93
Last visit: Thu Mar 30, 2017 8:54 am

Re: how to convert get-printer to Boolean

Post by juneblender »

Hi, Sean,

PowerShell has two types of errors, non-terminating and terminating. Non-terminating errors, like the kind generated by Write-Error, produce an error, but don't stop the pipeline, which continues to process. Terminating errors stop the pipeline.

Trap and Try-Catch both catch only terminating errors. They have no effect on non-terminating errors.

To trap a non-terminating error, use an IF statement.

if (Get-Printer -Name $PrinterName) {return $True)
else (return $False)

-or-

$checking2 = Get-Printer -Name $PrinterName
if (-not $checking2) {return $false}
else {return $true}

If Get-Printer is an advanced function with common parameters (To find out, use Get-Command -Syntax), you can use the ErrorAction common parameter with a value of Stop to generate a terminating error when any error (terminating or non-terminating) occurs.

try { Get-Printer -Name $PrinterName -ErrorAction Stop} catch { throw "Can't find printer $PrinterName" }

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

Re: how to convert get-printer to Boolean

Post by jvierra »

THis ois actually very easy and does not require a trasp.

if(Get-Printer testprinter -Ea 0){'Printer found'}else{'Printer not found'}

Do not use parens when callin a CmdLet or function it can cause you many issues at times.
User avatar
pls-sapien
Posts: 31
Last visit: Tue Dec 19, 2023 12:55 am

Re: how to convert get-printer to Boolean

Post by pls-sapien »

thanks a lot guys your suggestions work as always :) hopefully ill be able to help and contribute as you guys :geek: :)

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

Re: how to convert get-printer to Boolean

Post by jvierra »

To use Tyr/Catch we must tell the CmdLet to throw the correct exception.
  1. Try{
  2.     Get-Printer testprinter -Ea Stop
  3. }
  4. Catch{
  5.      'Printer not found'
  6. }
"Trap" was used in V1 but is harder to manage and not generally used anymore.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: how to convert get-printer to Boolean

Post by jvierra »

As an example of how to use PowerShell to you advantage we can abandon program constructs used in older languages.
Your function can be simplified due to PowerShell's function.
  1. function Test-Printer{
  2.     Param(
  3.         [Parameter(Mandatory)]
  4.         $PrinterName
  5.     )
  6.     [bool](Get-Printer $PrinterName -Ea 0)
  7. }
User avatar
pls-sapien
Posts: 31
Last visit: Tue Dec 19, 2023 12:55 am

Re: how to convert get-printer to Boolean

Post by pls-sapien »

Hey again,
i was wondering if you encountered some issues regarding the usage of Add-printer command in sps2016.

im getting strange behavior when using it.

basically this is the line im running from a wizard form template:
  1. Add-Printer -Name "$($labelPrinterNameCustom.Text)" -DriverName "$($comboboxPrinterDriver.Text)" -PortName "$($labelPortNameCustom.Text)" -Comment "$($textboxPrinterComment.Text)" -Location "$($textboxPrinterLocation.Text)" -Shared -Published -RenderingMode SSR
even if i replace all the vars with text i get the same error.

Here is the error:

ERROR: Add-Printer : An error occurred while performing the specified operation. See the error details for more information.
NewPrinterWizard_v0.psf (751, 3): ERROR: At Line: 751 char: 3
ERROR: + Add-Printer -Name "$($labelPrinterNameCustom.Text)" -Driv ...
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : NotSpecified: (MSFT_Printer:ROOT/StandardCimv2/MSFT_Printer) [Add-Printer], CimException
ERROR: + FullyQualifiedErrorId : HRESULT 0x80070bce,Add-Printer

but it doesnt happen every time - sometimes it does create the printer (???). i tried running the SPS2016 as admin and marked the enabled elevation (even though microsoft states there is no need for that when using add-printer)

maybe im using wrong call for variables? you guys had some issues with add-printer?

thanks again for all the time and help!

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

Re: how to convert get-printer to Boolean

Post by jvierra »

Here is a safer way to do this. Over use of quotes can have unintended contents. You should not use them where they are not needed.\
  1. $valid=$labelPrinterNameCustom.Text -and
  2.         $comboboxPrinterDriver.Text -and
  3.         $labelPortNameCustom.Text -and
  4.         $textboxPrinterComment.Text -and
  5.         $textboxPrinterLocation.Text
  6. if ($valid) {
  7.     $props = @{
  8.         Name = $labelPrinterNameCustom.Text
  9.         DriverName = $comboboxPrinterDriver.Text
  10.         PortName = $labelPortNameCustom.Text
  11.         Comment = $textboxPrinterComment.Text
  12.         Location = $textboxPrinterLocation.Text
  13.         Shared = $true
  14.                 Published = $true
  15.         RenderingMode = 'SSR'
  16.     }
  17.     Add-Printer @props
  18. } else {
  19.     Throw 'Missing variable'
  20. }
  21.  
  22. $valid=$labelPrinterNameCustom.Text -and
  23.         $comboboxPrinterDriver.Text -and
  24.         $labelPortNameCustom.Text -and
  25.         $textboxPrinterComment.Text -and
  26.         $textboxPrinterLocation.Text
  27. if ($valid) {
  28.     $props = @{
  29.         Name = $labelPrinterNameCustom.Text
  30.         DriverName = $comboboxPrinterDriver.Text
  31.         PortName = $labelPortNameCustom.Text
  32.         Comment = $textboxPrinterComment.Text
  33.         Location = $textboxPrinterLocation.Text
  34.         Shared = 'Published'
  35.         RenderingMode = 'SSR'
  36.     }
  37.     Add-Printer @props
  38. } else {
  39.     Throw 'Missing variable'
  40. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: how to convert get-printer to Boolean

Post by jvierra »

If you continue to get this error you should contact the printer vendor as they may have a driver issue or a DLL that is misbehaving. IF you search for the error you will find that it is well known and plagues many printers.

It would be better if you managed printers with PrinterManager which is available on every version of Windows Server since Windows Server 2003R2.

If you are only adding local printers then you will find that many low cost printers are not shareable in a domain environment without issues.

Also beware of bad characters in any of the parameters which can cause errors when creating a printer.
User avatar
pls-sapien
Posts: 31
Last visit: Tue Dec 19, 2023 12:55 am

Re: how to convert get-printer to Boolean

Post by pls-sapien »

i found out that changing the SPS powershell version from V5 64bit to V5 32bit actually solved the problem :D

but ill definitely will take into consideration your recommendations.

my goal is to create a tool that will automate the process because we need to create an AD group,Print Port,Printer,Fix Premissions,Tick all kinds of checkboxes in the printer itself and of course locate an available IP address and reserver it in the DHCP.
now multiply that by 5 companies each with its naming conventions,IP ranges etc

im very very close to completion and both of you guys are my VIPs with many thanks in the script comments :)

when its ready ill post my Frankenstein-of-a-project (yeah its crude and ugly but alive :P) so anyone can simplify his/her life :)

cheers

Sean
This topic is 7 years and 11 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