I\'m remoting desktop to windows servers in our Lab/datacenter. I have a requirement to figure out all our servers are virtual machines or physical servers programatically,
There is no easy way to tell if you're running in a bare metal or in a virtual computer, the best thing you can do is to get some hardware info and made an educated guess, for example, if the machine have a network adapter which contains Microsoft, VMware, Oracle, Hyper-V, Virtual or VirtualBox, most likely it's a virtual machine given that neither Microsoft, Oracle, or VMware fabricate network cards.
As you use C#, the class for retrieving this and other hardware info is ManagementClass, also there is this nice project that let you retrieve tons of info from your computer using ManagementClass.
As far as I know there is no easy way to do this.
There are a few workarounds but there is, at least as far as I know, not a one-size-fits-all solution.
Ben Armstrong wrote a post about Detecting Microsoft virtual machines and there's a low-level trick which can determine whether you are running within a Virtual PC or VMWare but that still leaves out VirtualBox and others.
A trick you might want to try is to detect whether VMWare Tools or VirtualBox Tools are installed. In most cases they are installed on the guest OS to provide needed features but it will be hard to maintain the different installation GUIDS on your end so it's not an ideal solution.
--- Also , if the VM is running in a Linux KVM environment, the output is like this one
The only *programmatic* way I know of doing this reliably is:
Run the systeminfo command @ command prompt see the system menufacturer n system model details. There you can find the virtule and physical machine information.
You can try to use the following PowerShell script, it utilizes WMI to find out if machine is virtual machine or physical machine.
gwmi -q "select * from win32_computersystem"
Certainly, you can use C# code to query WMI too. The output of script above will be like following:
Domain: ... Manufacturer: Microsoft Corporation Model: Virtual Machine Name : ..... ....
you can use this command in cmd or powershell
SYSTEMINFO
You will find a line with the following text (or similar):
System Manufacturer: VMware, Inc. System Model: VMware Virtual Platform