これは、server2003 および 2008r2 で最近発生し始めた例外です。
ManagementException: Generic failure
Stack: at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()
このエラーを受け取った後、この例外が発生することも確認しました (スプーラーが停止しましたか?):
The RPC server is unavailable
Win32Exception: The RPC server is unavailable
Stack: at System.Drawing.Printing.PrinterSettings.get_InstalledPrinters()
WMI クエリを生成するコードを次に示します。
// 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();
}
// ...
検索を行った後、これらのリンクを見つけて、ヒントを得ることができました。
- https://stackoverflow.com/a/5247725/187650
- http://social.msdn.microsoft.com/Forums/vstudio/en-US/b20c35f7-375b-4b67-af2e-bd432b6915da/how-to-retrieve-all-printer-names-installed-in-a-pc
- http://www.tech-archive.net/Archive/Development/microsoft.public.win32.programmer.wmi/2005-12/msg00097.html
- http://www.vistax64.com/powershell/134250-get-wmiobject-generic-failure.html
PowerShell でリモート PC に対してこのクエリを実行しようとしても、次のようになります。
Get-WmiObject -Query "SELECT Attributes, Name FROM Win32_Printer" -ComputerName "remotepc"
しばらくすると、次のエラーが表示されます。
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
C# コードの一部の改善点を共有できる WMI の専門家はいますか?
- 文字列の代わりに ObjectQuery?
- PacketPrivacy ( https://stackoverflow.com/a/8643611/187650 )?
- ObjectQuery タイムアウト?