简体   繁体   中英

Get-WMIObject fails to enumerate Win32_LogicalDiskToPartiion in elevated powershell

I have a really simple Powershell query:

Get-WmiObject -query 'ASSOCIATORS OF {Win32_LogicalDisk.DeviceID="C:"} WHERE AssocClass = Win32_LogicalDiskToPartition'

On a Windows 7 (64-bit) machine, running this in a Powershell correctly enumerates a single management object. However if I run the same query in an elevated Powershell I get a long pause and then no results.

I find a similar problem when trying to execute the WMI query in code (which is what I am actually trying to do) - when my program runs without elevation the code works, when it runs with elevation no results are returned. This is the simplest version of my code that shows this problem:

static void Main(string[] args)
{
    var query = "ASSOCIATORS OF {Win32_LogicalDisk.DeviceID=\"C:\"} WHERE AssocClass = Win32_LogicalDiskToPartition";
    var searcher = new ManagementObjectSearcher(query);
    foreach (var o in searcher.Get())
    {
        Console.WriteLine(o);
    }
    Console.WriteLine("DONE");
    Console.ReadLine();
}

Why does this happen ? More importantly is there anyway I can ensure that this query will execute correctly when run elevated - as the final program will need to run elevated for other reasons.

I think I found the culprit - I have an encrypted drive mounted using TrueCrypt. When I dismount that drive the enumeration works correctly, when I mount it again the problem re-appears.

My best guess is that WMI is hitting a problem because the encrypted drive has no partitions - though why it works when not running elevated is another thing entirely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM