Get-cimInstance

Welcome Forums General PowerShell Q&A Get-cimInstance

This topic contains 12 replies, has 6 voices, and was last updated by

 
Participant
9 months, 1 week ago.

  • Author
    Posts
  • #92662

    Participant
    Points: 25
    Rank: Member

    Greetings,
    When I execute this code, I am getting an 'Invalid query' powershell error. Have I formatted the code correctly?

    Get-CimInstance -ClassName cim_storagevolume -Filter "DriveLetter='C:'"

  • #92663

    Keymaster
    Points: 12
    Team Member
    Rank: Member

    That particular class doesn't appear to like filtering via the Get-CimInstance cmdlet. I'll ask around to see if someone can explain exactly why.

    In the meantime, I've found that this works.

    (Get-CimInstance -ClassName Cim_StorageVolume).where({$PSItem.DriveLetter -eq 'C:'})
  • #92668

    Participant
    Points: 13
    Rank: Member

    This also works

    Get-CimInstance -ClassName cim_storagevolume | where driveletter -eq "C:"

  • #92671
    Jon

    Participant
    Points: 22
    Rank: Member

    I'm not able to get it to filter either, I did something similar to Will to get the results. Many ways to skin the PowerShell cat!

    Get-CimInstance -ClassName CIM_StorageVolume | where driveletter -eq "C:"

    ETA: Beat me by a min Simon! 😉

    • #92674

      Keymaster
      Points: 12
      Team Member
      Rank: Member

      Simon is pretty quick. 😉

      Surprisingly, Where-Object does appear to be faster in this instance too.

      PS C:\WINDOWS\system32> Measure-Command {(Get-CimInstance -ClassName Cim_StorageVolume).where({$PSItem.DriveLetter -eq 'C:'})}
      Measure-Command {Get-CimInstance -ClassName Cim_StorageVolume | Where-Object DriveLetter -EQ 'C:'}
      
      
      Days              : 0
      Hours             : 0
      Minutes           : 0
      Seconds           : 0
      Milliseconds      : 50
      Ticks             : 504126
      TotalDays         : 5.83479166666667E-07
      TotalHours        : 1.40035E-05
      TotalMinutes      : 0.00084021
      TotalSeconds      : 0.0504126
      TotalMilliseconds : 50.4126
      
      Days              : 0
      Hours             : 0
      Minutes           : 0
      Seconds           : 0
      Milliseconds      : 28
      Ticks             : 284677
      TotalDays         : 3.29487268518518E-07
      TotalHours        : 7.90769444444444E-06
      TotalMinutes      : 0.000474461666666667
      TotalSeconds      : 0.0284677
      TotalMilliseconds : 28.4677
  • #92675

    Keymaster
    Points: 12
    Team Member
    Rank: Member

    Explanation per Mr. Richard Siddaway – who is way better at WMI things than I am:

    The class

    https://msdn.microsoft.com/en-us/library/mt432375(v=vs.85).aspx

    doesn't appear to have a DriveLetter property so filter won't work

    Interestingly

    PS> Get-CimInstance -ClassName cim_storagevolume | gm

    TypeName: Microsoft.Management.Infrastructure.CimInstance#root/cimv2/Win32_Volume

    Win32_volume derives from cim_storagevolume

    PS> Get-WmiObject -Class Win32_volume | select -f 1 | select -expand __derivation

    CIM_StorageVolume

    CIM_StorageExtent

    CIM_LogicalDevice

    CIM_LogicalElement

    CIM_ManagedSystemElement

    Looks to me like the call to CIM_StorageVolume actually ends up calling Win32_Volume

    CIM_StorageVolume doesn't appear to have a driveletter property so you can't filter on it BUT Win32_Volume does have a drive letter property which is what is displayed

    I always recommend using the Win32-* classes rather than the CIM_ classes because the Win32_* classes were designed fro Windows whereas the CIM_* classes are generic

  • #92677
    Jon

    Participant
    Points: 22
    Rank: Member

    "doesn't appear to have a DriveLetter property so filter won't work"

    Pardon my ignorance, if that is the case then why would the below commands return a value if their is not a property? Are they something else than properties?

    Get-CimInstance -ClassName CIM_StorageVolume | select driveletter
    Get-CimInstance -ClassName CIM_StorageVolume | where driveletter -eq "C:"
  • #92696

    Participant
    Points: 0
    Rank: Member

    Notice the type being returned

    PS> Get-CimInstance -ClassName CIM_StorageVolume | gm

    TypeName: Microsoft.Management.Infrastructure.CimInstance#root/cimv2/Win32_Volume

    When you're calling CIM_StorageVolume your getting back Win32_Volume which is derived from CIM_StorageVolume. Win32_Volume HAS a DriveLetter property which is why your use of Select_Object and Where-Object works

  • #92707
    Jon

    Participant
    Points: 22
    Rank: Member

    So select/where can read what is in win32_volume but -filter is unable to?

    • #92734

      Participant
      Points: 21
      Rank: Member

      What I think they are saying is that the CIM_ class call is actually fulfilled by making a call to Win32_Volume behind the scenes, but apparently after an attempt is made to apply the filter. When you query CIM_StorageVolume, and provide a filter it is failing because CIM_StorageVolume doesn't have that DriveLetter property, and I'll even go out on a limb a guess that it actually has *no* properties. The data returned has properties thanks to Win32_Volume. So, when I do a Get-CIMInstance Win32_Volume -Filter 'DriveLetter = "E:"', I get back info just for the E drive.

  • #92777

    Participant
    Points: 0
    Rank: Member

    COM_StorageVolume does have properties

    Get-CimClass Win32_Volume
    and
    Get-CimClass CIM_storagevolume

    will show the difference

    DriveLetters are a Windows thing which is probably why they aren't present on CIM_SrorageVolume which is meant to be generic

    • #92792

      Participant
      Points: 25
      Rank: Member

      Thanks Richard.....by the way, your book 'Powershell and WMI' is a great resource. I'm really enjoying the book...

  • #92791

    Participant
    Points: 25
    Rank: Member

    Thanks all for your great insights on the CIM_StorageVolume and Win32_Volume.....

The topic ‘Get-cimInstance’ is closed to new replies.