There’s probably quite a few situations where it would be useful to be able to programmatically detect if a particular OS is running under Virtual Server/Virtual PC or not. In my case, I was working at a new customer site; the sysadmin responsible for the site was out of town/unreachable, there was no documentation, and the only thing I had to go on was that the OS I needed to look at was running under Virtual Server.
So in working on my inventory script, I remembered seeing that in the output, some of the machines on my network had a model type of “Virtual Machine”. When I originally saw this, I didn’t give too much thought to it, but now, being in a situation where it would come in handy, I was immediately reminded of how useful this is.
After skimming through the script for a bit, I found what I needed and saw how/why the detection was working. Specifically the Win32_ComputerSystem class, which the WMI SDK says “represents a computer operating system in a Windows environment.” Contains a string called “Model”, which is a “Product name that a manufacturer gives to a computer.” Apparently, when the machine is working under Virtual Server 2005 or Virtual PC, the reported model type is “Virtual Machine”.
Okay, so great. This was really convenient, and helped me to solve my problem. So posted below you’ll see some code to do the detection. Also, after I did this I realized that Ben, over at the “Virtual PC Guy’s WebLog” already posted a similar script. His works a bit differently, which I’ll explain… He uses the Win32_BaseBoard class – which we know “represents a baseboard, which is also known as a motherboard or system board". And we can see he is looking at objItem.Manufacturer – a string that, again according to the SDK, represents the “Name of the organization responsible for producing the physical element.” Which is just to say, that he’s getting “Microsoft Corporation” from it, and he knows that any motherboards called “Microsoft Corporation”, are going to be Virtual Machines.
So his script actually looks pretty similar to mine. The only notable different is that he’s talking to a different class. But it’s neat, and useful... and there’s always more than one way to skin a cat! Have fun.
strComputer = "computername"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
For Each objItem in colItems
strModel = objItem.Model
If strModel = "Virtual Machine" Then
wscript.echo "This is a virtual machine!"
else
wscript.echo "This is NOT a Virtual Machine"
End if
Next
No comments:
Post a Comment