ManagementException: Generic failure accessing Win32_Printer (resulting in RPC server is unavailable)

2.7k views Asked by At

This is an exception that recently started occurring had on a server2003 and 2008r2:

ManagementException: Generic failure
Stack:    at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
  at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()

After receiving this error I've also seen this exception occuring (spooler stopped?):

The RPC server is unavailable

Win32Exception: The RPC server is unavailable
Stack:    at System.Drawing.Printing.PrinterSettings.get_InstalledPrinters()

The code that generates the WMI query is listed here:

// printername:   \\server\printer  or   mylocalprinter

PrinterAttributes attribs = PrinterAttributes.Hidden;
ManagementObjectSearcher searchPrinter = null;
ManagementObjectCollection prntCol = null;

try 
{
    // "SELECT Attributes, Name FROM Win32_Printer"
    string qry = string.Format("SELECT Attributes, Name FROM Win32_Printer WHERE Name = \'{0}\'", printerName);
    searchPrinter = new ManagementObjectSearcher(qry);
    prntCol = searchPrinter.Get();

    foreach (ManagementObject prnt in prntCol) {
      if (prnt["Name"].ToString() == printerName) { /* ... */ }
    }
}
finally 
{
    if (prntCol != null) prntCol.Dispose();
    if (searchPrinter != null) searchPrinter.Dispose();
}

// ...

After doing a search I've found these links to give me a possible hint:

  1. https://stackoverflow.com/a/5247725/187650
  2. http://social.msdn.microsoft.com/Forums/vstudio/en-US/b20c35f7-375b-4b67-af2e-bd432b6915da/how-to-retrieve-all-printer-names-installed-in-a-pc
  3. http://www.tech-archive.net/Archive/Development/microsoft.public.win32.programmer.wmi/2005-12/msg00097.html
  4. http://www.vistax64.com/powershell/134250-get-wmiobject-generic-failure.html

Even when I try in PowerShell to do this query for a remote pc:

Get-WmiObject -Query "SELECT Attributes, Name FROM Win32_Printer" -ComputerName "remotepc"

I receive this error after a while:

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:1 char:1
+ Get-WmiObject -Query "SELECT Name FROM Win32_Printer" -ComputerName "remotepc"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Are there any WMI experts who could share improvements on the piece of C# code, because at this moment I have no clue why it only happens once in a while.

0

There are 0 answers