WMI / Print server - Query for shared printer and port faster ?

  • Thread starter Thread starter _Hardware_
  • Start date Start date
H

_Hardware_

Guest
Hi everyone.




I work on a RESTful service in WCF (C#) to manage Print Queues remotely on multiple print servers (Windows Server 2012 R2 / 2016). I use WMI classes (Win32_Printer, Win32_PrinterConfiguration, Win32_TCPIPPrinterPort...etc) and System.Management/System.Printing namespaces to achieve that. All works flawlessly, I can create my shared/published print queues in AD, with corresponding port and driver as well, through a website dedicated for that.


But, some of these servers host thousands of printer queues, 7000+ for the biggest.


The code I'm using right now is :


public List<Queue> GetQueuesByServer(SearchModel model)
{
var queues = new List<Queue>();
var query = new ObjectQuery($"SELECT * FROM Win32_Printer WHERE {model.Attribute} like '%{model.Value}%'");

using (var searcher = new ManagementObjectSearcher(managementScope, query))
{
var queuesCollection = searcher.Get();

foreach (ManagementObject m in queuesCollection)
{
var queue = new Queue();

if (m["Location"] != null)
{
queue.comment = m["Comment"]?.ToString();
queue.driverName = m["DriverName"]?.ToString();
queue.isDirect = (bool)m["Direct"];
queue.isPublished = (bool)m["Published"];
queue.isQueued = (bool)m["Queued"];
queue.isShared = (bool)m["Shared"];
queue.keepPrintedJobs = (bool)m["keepPrintedJobs"];
queue.isRawOnlyEnabled = (bool)m["RawOnly"];
queue.isBidiEnabled = (bool)m["EnableBIDI"];
queue.scheduleCompletedJobsFirst = (bool)m["DoCompleteFirst"];
queue.name = m["Name"]?.ToString();
queue.isBODPEnabled = GetBODPState(queue.name);
queue.portName = m["PortName"]?.ToString();
queue.portType = m["PrintJobDataType"]?.ToString();
queue.printProcessor = m["PrintProcessor"]?.ToString();
queue.location = m["Location"]?.ToString();
queue.shareName = m["ShareName"]?.ToString();
queue.server = model.Server;

queues.Add(queue);
}
}

return queues;
}
}

Same logic to get printer ports.


This operation can take more than 900 seconds with the biggest centralized print server. With a "normal" printserver (~10/20 queues), it's instant. I tested with both my REST client and our dedicated website.


URL: https://print.api/QueueService.svc/GetQueuesByServer


{
"Server":"SRV01",
"Attribute":"Location",
"Value":"/FRANCE/PARIS/SITE_XX/BUILDING_XX..."
}

In this example, I want to retrieve all print queues only for a specific site in Paris. We never want to get all print queues hosted on a specific centralized print server, the location attribute is used as filter, based on site / building / floor.


I am hopping someone has a faster way to do that. Is there an obvious optimization ?


Best regards.

Continue reading...
 
Back
Top