# Using Win32_FileSpecification WMI instances

I am trying to get a checksum of a remote file using Win32_FileSpecification class via WMI, but am running into a few problems.

1. 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?
2. 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!

-

there is no direct way to calculate an MD5 in PowerShell

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:

$crypto = [System.Security.Cryptography.MD5]::Create()$data = [System.IO.File]::ReadAllBytes("\\path\to\file")
$md5 = [System.Convert]::ToBase64String($crypto.ComputeHash($data))  Where md5 is a base64 encoded value of the MD5 hash. Or if you need a hex representation instead of base64 of the hash: $md5 = [System.BitConverter]::ToString($crypto.ComputeHash($data)).Replace("-", "")


You can of course wrap that up in a nice little PowerShell function:

Function MD5Hash
{
param ($file)$crypto = [System.Security.Cryptography.MD5]::Create()
$data = [System.IO.File]::ReadAllBytes($file)
[System.BitConverter]::ToString($crypto.ComputeHash($data)).Replace("-", "")
}


And invoke like so:

MD5Hash "\\path\to\file"

-
This is how I have implemented it, but the files are quite large (video) so copying it over the network (and our VPN connection to the data center) makes using a UNC path unusable. This can also be done via remoting so the file stays local on the target, but this leads to commands as strings passed to the remote machine, which just isn't pretty (although it is working). – Brian Jan 31 '12 at 16:43
@Brian I believe (still verifying) that the MD5Checksum property is only populated if you obtain an instance of WIN32_FileSpecification through Win32_PatchFile's property Check. I don't think you can get the MD5 of an arbitrary file using this. – vcsjones Jan 31 '12 at 18:26