Check windows printer before print

Is there a way to check if a “Windows Printer” is pressent with a javascript ot other script type before print ?

Printing PDF’s through the Workflow with the plugin " Print using a Windows driver"
Printernane is a variable.

Script should just return true or false.
If false then we have the option to send the print to a default printer, instead of creating an error in the workflow.

Thanks ind advance
Klaus

Hi,

Maybe using the SNMP condition plugin!

Hi,

Or use the error to send it to a default printer (error process)

There must be a script that can check for locally installed windows printers.
I don’t want to check if the “physical” printer is alive.
Just want to check i a named printer is installed under windows printers like a printer named “Administration”

I think Powershell is the script language to use.
Please google “check printer status Powershell” and you get lots of solutions.
e.g. Printer status using powershell - Stack Overflow
You can run Powershell scripts using “run external program” plug-in.

I have done it with Powershell script.
I was just hoping that it could have been done with a true/false javascript.

Klaus

The following JS code uses the WMI object to obtain printer information from Windows as an array of objects:

var printers = WMI_Printers();
for(var i=0;i<printers.length;i++){
  Watch.log(JSON.stringify(printers[i]),2)
}

function WMI_Printers (){
  try {
    var WMI = GetObject('winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2');
  } catch (e) {
    throw "WMI not available - "+e;
  }
  return WMIQuery('SELECT * FROM Win32_Printer',true);

  function WMIQuery(query,isArr){
    var returnValue = isArr ? [] : {};
    var infos = WMI.ExecQuery(query);
    forEach( infos, function(colItem){
      var obj = {};
      forEach( colItem.Properties_, function(colP){
        obj[colP.Name] = colP.Value;
      });
      if(isArr) {
        returnValue.push(obj);
      } else {
        returnValue = obj;
      }
    });
    return returnValue;
  }

  function forEach(collection, func) {
    for (var o = new Enumerator(collection); !o.atEnd(); o.moveNext()) {
      func(o.item());
    }
  }
}

There’s a huge amount of information available through the WMI object, I have been planning on writing a blog article about it, but never seem to get around to it!

1 Like

Hi Phill

Had to use this at a customer today.
Is here a way to retur a complete JSON sting to the workflow - made by the script ?

In this case the printers are “just” listed with the log function:
Watch.log(JSON.stringify(printers[i]),2)

I could also write all the printers to a txt file and then load that file again in the workflow.
Just wondering if there was a more clever way to do it.

Klaus

If you look at the script, you’ll see that the the printers variable is actually a JSON array. You can write the entire array to a file and then pick that up in a script later in the process or in a different process. For instance:

var fso = new ActiveXObject("Scripting.FileSystemObject");
var printerFile = fso.CreateTextFile("C:/tests/PrinterList.json");
printerFile.Write(JSON.stringify(printers,null,2));
printerFile.Close();

You could also store a text version of that array in a local variable that can be re-used later in the process:

Watch.SetVariable("PrinterList",JSON.stringify(printers));
1 Like