lightwing
lightwing

Reputation: 162

workaround for win32_printer access denied

I'm writing a powershell script for windows 7 to replace some printer connections on users' desktops and I need to be able to get a list of print queues on a windows print server. I do not have access to the print server beyond browsing, connecting and printing to the print queues.

Problem: When querying the server for a list via win32_printer with get-wmiobject, I get access denied.

PS M:> Get-WmiObject -Class Win32_Printer -computername newprintservername

Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) At line:1 char:14 + Get-WmiObject <<<< -Class Win32_Printer -computername newprintservername + CategoryInfo : NotSpecified: (:) [Get-WmiObject],UnauthorizedAccessException + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Complication: "newprintservername" could actually be one of two different servers, so I want to be able to check for the existence of print queue before trying to connect to it. I suppose I could attempt a connection, check the printer connections and if it doesn't exist, attempt connection to the other server, but I'd like to not have to take that route unless absolutely necessary.

Any ideas? Thanks in advance.

Upvotes: 0

Views: 3090

Answers (2)

TheMadTechnician
TheMadTechnician

Reputation: 36297

For some reason I had a hard time finding it but I knew it had to exist: the .Net class for print servers, printers, print queues, and what. So, here's what I found...

Link to script that showed me how to load the .DLL up to get access to the classes:
http://poshcode.org/2942

Here's what I used out of that (I have a 64bit OS):

$SystemPrinting = Get-ChildItem "$($env:systemroot)\assembly\GAC_64\System.Printing"
$SystemPrintingFile = Get-ChildItem -Name "*system.printing*" -Recurse -Path $SystemPrinting.FullName
$SystemPrintingFile = "$($SystemPrinting.FullName)\$($SystemPrintingFile)"
Add-Type -Path $SystemPrintingFile

Then I added this:

$PrintServer = [System.Printing.PrintServer]"\\ServerName"
try{
    $PrintQueue = $PrintServer.GetPrintQueue("PrintQueueName")
}
Catch{
    "Print Queue Not Found"
}

I tried it with a valid print queue and could view queue info when I looked at $PrintQueue, and when I tried an invalid one it responded saying "Print Queue Not Found".

Is that enough for you to work with to get done what you need to?

Upvotes: 1

Kris Powell
Kris Powell

Reputation: 131

You will probably need elevated privileges to the machine to query WMI. Try running your commands with credentials that have administrator rights to that machine and see how that goes.

Get-WmiObject -Class Win32_Printer -computername newprintservername -Credential (Get-Credential)

Alternatively, if you are unable to get administrator rights on that machine, you may want to look into this blog post with someone who setup non-Admins to query WMI - link.

At the very least, it may be able to help point you in the right direction.

Cheers,

Upvotes: 0

Related Questions