Search code examples
powershellprintingsccmwql

Using WQL query from SCCM in powershell


I have a query in SCCM that will take a printer IP address and return all workstations in SCCM that have the printer installed on it. I am wanting to create a powershell script that will take said query and use the workstations that it returns to then list current print jobs in the print queue on the workstation.

I know that you can use Get-CIMInstance -query to query different things in WMI. That works well if I am trying to find out information locally. However if I dump the WQL query into a Here-String and assign it to a variable and then call it with Get-CIMInstance -query it returns an error saying invalid query. The same thing happens when I use Get-WmiObject -Namespace "root\wmi" -Query $WQlquery

So how would I be able to use the WQL query from SCCM in powershell? Here is an example of what I have so far:

$WQLquery = @"
select SMS_R_System.Name from
SMS_R_System inner join
SMS_G_System_PRINTER_DEVICE on
SMS_G_System_PRINTER_DEVICE.ResourceID = 
SMS_R_System.ResourceId where 
SMS_G_System_PRINTER_DEVICE.PortName like "10.10.10.10"
"@

Get-CIMInstance -query $WQLquery

Assuming that worked and returned a list of workstation ids, I would then use Get-Printjob cmdlet to list current jobs in each workstations print queue. I have found a few questions posted here already that have helped me get this far. Any additional help would be appreciated. Go easy on me, still a newb here.


Solution

  • You need to specify the namespace for the sccm site root\sms\site_SITECODE and the sccm-server if you're running it from a remote computer. Ex:

    $WQLquery = @"
    select SMS_R_System.Name from
    SMS_R_System inner join
    SMS_G_System_PRINTER_DEVICE on
    SMS_G_System_PRINTER_DEVICE.ResourceID = 
    SMS_R_System.ResourceId where 
    SMS_G_System_PRINTER_DEVICE.PortName like "10.10.10.10"
    "@
    
    Get-WmiObject -Query $WQLquery -ComputerName "SCCMSERVER" -Namespace "root\sms\site_PRI"