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, certainly we have the environment sheet tell us which is which. But I need to write code to distinguish it. What technique I need to use? I didn't find a .Net Assembly to do that. Looking for expert to share your knowledge or guidance, any research direction or link, anything will be appreciated!
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 : ..... ....
To check this from the command prompt you can run this: systeminfo | Select-String "System"
Example output for virtual server:
System Manufacturer: Microsoft Corporation
System Model: Virtual Machine
System Type: x64-based PC
Example output for physical server:
System Manufacturer: HP
System Model: ProLiant BL460c G6
System Type: x64-based PC
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
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.
Run the systeminfo command in the command prompt to see the system manufacturer and system model details. There you can find the virtual and physical machine information.
Try this:
FOR /F "tokens=*" %a IN ('wmic bios get bioscharacteristics^|find /c "33"') DO set USBlegacy=%a
This returns "1" for the limited range of desktops and laptops in my environment and "0" for VMWare workstation 9, ESX 5.5, and Citrix 6.5 and 7.6. BIOSCharacteristic "50" (one "reserved for system vendor") I've only found in the four virtual environments so that would work in reverse.
Edit: or there's this:
FOR /F "tokens=*" %a IN ('wmic path win32_pnpentity get ^|find /c "ACPI Fan"') DO set ACPIfan=%a
Returns "5" on an HP Desktop, "0" on VMware workstation 9 and ESX 5.5, not tested on the others.
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
This works very well in Powershell:
$ecv = (get-wmiobject win32_BIOS).EmbeddedControllerMajorVersion
$isPhysical = $ecv -gt 0 -and $ecv -lt 255
A VM does not have a controller or has a dummy-value of '0' as a version.
The only *programmatic* way I know of doing this reliably is:
- Write an app that crawls your network (or IP range) to get a list of machines.
- Display those machines to a person and ask them to check a box if it's a VM...
- Print the report.
© 2022 - 2024 — McMap. All rights reserved.