So I’ve discovered that WMI is going to be deprecated so I’m trying to convert my old uses of Get-WMIObject to Get-CimInstance. The issue is, for example, if I try this:<\/p>\n
$usbDrive = Get-WMIObject win32_diskdrive | Where-Object {$_.interfacetype -eq \"USB\"} | ForEach-Object {Get-WmiObject -Query \"ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`\"$($_.DeviceID.replace('\\','\\\\'))`\"} WHERE AssocClass = Win32_DiskDriveToDiskPartition\"} | ForEach-Object {Get-WmiObject -Query \"ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`\"$($_.DeviceID)`\"} WHERE AssocClass = Win32_LogicalDiskToPartition\"} | ForEach-Object {$_.deviceid}\n<\/code><\/pre>\n
Advertisement
This basically just gets the drive letter of any USB device connected and if I were to swap out every “WmiObject” with “CimInstance”, it will still work. I really am not sure if I should rely on it just working or if there is anything else I need to change. Having flashbacks to when PHP changed mysql to mysqli. Whilst it seemed like just adding an “i” on the end, there were loads more parameters needed for a bunch of things so the whole thing had to be gone over.<\/p>\n
Advertisement
If I look at this win32_diskdrive thing with Wmi, I get three objects formatted like this…<\/p>\n
So, I can see this is the better format for objects but I’m really just asking to check there isn’t anything else I need to do in my example or any others if there are some common ones.<\/p>\n
In general, Get-WMIObject can be replaced with Get-CimInstance.<\/p>\n
Both commands return the same identical class data, but<\/p>\n
\n
\n
Get-WMIObject returns system properties and methods. Get-CimInstance doesn’t. This is more efficient in most cases.<\/p>\n<\/li>\n
\n
The wrapper classes are different (PowerShell wraps the data returned from WMI via COM in a .NET object which is what you see if you use Get-Member etc. You probably don’t care much about wrapper classes in most cases.<\/p>\n<\/li>\n
\n
If you want to get an object from WMI and want to invoke a method on it, the mechanism is quite different. With the WMI cmdlets, you get the WMI object, then call the method from the instantiated object. With CIM cmdlets, you use Invoke-CimMethod specifying the WMI object, the method name, and a hash table of parameter values.<\/p>\n<\/li>\n<\/ol>\n
tl:dr: for simple<\/em> things, you can replace \"Get-WmiObject’ with ‘Get-Ciminstance’.<\/p>","upvoteCount":1,"datePublished":"2019-02-07T12:29:17.000Z","url":"https://community.spiceworks.com/t/ciminstance-wmiobject/696457/11","author":{"@type":"Person","name":"DoctorDNS","url":"https://community.spiceworks.com/u/DoctorDNS"}},"suggestedAnswer":[{"@type":"Answer","text":"
So I’ve discovered that WMI is going to be deprecated so I’m trying to convert my old uses of Get-WMIObject to Get-CimInstance. The issue is, for example, if I try this:<\/p>\n
$usbDrive = Get-WMIObject win32_diskdrive | Where-Object {$_.interfacetype -eq \"USB\"} | ForEach-Object {Get-WmiObject -Query \"ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`\"$($_.DeviceID.replace('\\','\\\\'))`\"} WHERE AssocClass = Win32_DiskDriveToDiskPartition\"} | ForEach-Object {Get-WmiObject -Query \"ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`\"$($_.DeviceID)`\"} WHERE AssocClass = Win32_LogicalDiskToPartition\"} | ForEach-Object {$_.deviceid}\n<\/code><\/pre>\n
This basically just gets the drive letter of any USB device connected and if I were to swap out every “WmiObject” with “CimInstance”, it will still work. I really am not sure if I should rely on it just working or if there is anything else I need to change. Having flashbacks to when PHP changed mysql to mysqli. Whilst it seemed like just adding an “i” on the end, there were loads more parameters needed for a bunch of things so the whole thing had to be gone over.<\/p>\n
If I look at this win32_diskdrive thing with Wmi, I get three objects formatted like this…<\/p>\n
So, I can see this is the better format for objects but I’m really just asking to check there isn’t anything else I need to do in my example or any others if there are some common ones.<\/p>\n
If it were me, I’d take this one step at a time.<\/p>\n
The WMI cmdlets are not cross-platform however, the CIM cmdlets are and they provide better coverage too. But ultimately, they both return the same data. However, the wrapper objects are different, and the way you invoke method differs too.<\/p>\n
That is quite a complex one-liner that you are asking about. If it were me, I’d decompose it and convert it bit by bit. So start with :<\/p>\n
Like I said, just changing WmiObject to CimInstance actually works but I just wanted to check I wasn’t missing something. And the purpose is to just list USB Drives. So the result of both the Wmi and Cim versions of that one liner end up just putting a list of drive letters with a colon on the end. So it just says<\/p>\n