I am trying to get a checksum of a remote file using Win32_FileSpecification class via WMI, but am running into a few problems.
- It uses CheckID as the key, which looks to be similar to the path with '\'s changed to '.'s and a GUID at the end. How do you get from a path (C:\test.txt) to this CheckID format (preferably from CIM_DataFile)? What is the significance of the GUID?
- Regardless of the above, when
gwmi WIN32_FileSpecification
is run, all files have an empty MD5Checksum value in the output. Should MD5Checksum be populated by Windows when getting an instance, and if not is there a method to invoke to populate it? It appears the only method on WIN32_FileSpecification is Invoke which is not implemented.
My only solution has been to write a cmdlet for PowerShell using remoting to run a script and generate the checksum value. This works but isn't the cleanest as there is no direct way to calculate an MD5 in PowerShell so there is additional maintenance of the code.
Thanks!
Best Answer
That's not entirely true. Remember that PowerShell has access to the entire .NET stack, and it's certainly possible to compute an MD5 hash in .NET.
If your goal is to get an MD5 hash, then you can avoid WMI all together and use something like this:
Where
md5
is a base64 encoded value of the MD5 hash.Or if you need a hex representation instead of base64 of the hash:
You can of course wrap that up in a nice little PowerShell function:
And invoke like so: