Handling international WMI clients / servers with Hyper-V
If you ever try to run a WMI script / program against Hyper-V on a system that is using a language (or locale) different to your own – you may be surprised to see that a number of the properties are localized. This can cause problems if you are checking for language specific strings.
Take the following example:
When retrieving a list of virtual machines from Hyper-V it is usual to look at the caption – in order to filter out the MSVM_ComputerSystem entry of the parent partition. You would do this in PowerShell with a command like this:
gwmi MSVM_ComputerSystem -namespace root\virtualization | where {$_.caption -eq "Virtual Machine"}
But if I run this command on a German installation of Windows:
I get no results. If I remove the where clause you can quickly see the problem:
On this system the caption field is not “Virtual Machine” but is “Virtualler Computer”.
So what should you do to handle this? Well – the answer is quite simple. In WMI the server will always try to respond in the language that is used by the client. Normally the client just picks up the language automatically – but you can manually specify a language to be used. Here is the same PowerSehll command from above – but this time I am requesting that the server always respond in English:
gwmi MSVM_ComputerSystem -namespace root\virtualization -locale MS_409 | where {$_.caption -eq "Virtual Machine"}
Running this yields the expected results:
A couple of things to note here:
- The downside of specifying your own locale in a script / program is that any error messages will be returned in the language you specified – which may not be desirable if your end user is not fluent in that language.
- You can see all the locale codes for WMI here: https://msdn.microsoft.com/en-us/library/aa393651(VS.85).aspx
- If you specify a locale which the server does not support – you will get English.
- All server installations support English and their install language. If you want to access other languages – you will need to install the appropriate server language pack: https://www.microsoft.com/downloads/details.aspx?FamilyID=03831393-eef7-48a5-a69f-0ce72b883df2
Cheers,
Ben