C# 如何检查我的windows服务器是虚拟机还是物理机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15485601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check my windows server is virtual machine or physical machine
提问by ljh
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!
我正在将桌面远程连接到我们实验室/数据中心的 Windows 服务器。我需要以编程方式确定我们所有的服务器是虚拟机还是物理服务器,当然我们有环境表告诉我们哪个是哪个。但是我需要写代码来区分它。我需要使用什么技术?我没有找到一个 .Net 程序集来做到这一点。寻找专家分享您的知识或指导,任何研究方向或链接,任何东西都将不胜感激!
采纳答案by NotMe
You can try to use the following PowerShell script, it utilizes WMI to find out if machine is virtual machine or physical machine.
您可以尝试使用以下 PowerShell 脚本,它利用 WMI 来确定机器是虚拟机还是物理机。
gwmi -q "select * from win32_computersystem"
当然,您也可以使用 C# 代码来查询 WMI。上面脚本的输出将如下所示:Domain: ... Manufacturer: Microsoft Corporation Model: Virtual Machine Name : ..... ....
回答by Jensen
As far as I know there is no easyway 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 machinesand there's a low-level trick which can determine whether you are running within a Virtual PC or VMWarebut that still leaves out VirtualBox and others.
Ben Armstrong 写了一篇关于检测 Microsoft 虚拟机的文章,有一个低级技巧可以确定您是在Virtual PC 还是 VMWare中运行,但仍然忽略了 VirtualBox 和其他。
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.
您可能想要尝试的一个技巧是检测是否安装了 VMWare Tools 或 VirtualBox Tools。在大多数情况下,它们安装在来宾操作系统上以提供所需的功能,但很难在您的终端上维护不同的安装 GUIDS,因此这不是理想的解决方案。
--- Also , if the VM is running in a Linux KVM environment, the output is like this one
--- 另外,如果VM运行在Linux KVM环境下,输出是这样的
回答by Rafael
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.
没有简单的方法可以判断您是在裸机还是在虚拟计算机中运行,您能做的最好的事情是获取一些硬件信息并进行有根据的猜测,例如,机器是否有网络适配器其中包含 Microsoft、VMware、Oracle、Hyper-V、Virtual 或 VirtualBox,鉴于 Microsoft、Oracle 或 VMware 均未制造网卡,因此它很可能是虚拟机。
As you use C#, the class for retrieving this and other hardware info is ManagementClass, also there is this nice projectthat let you retrieve tons of info from your computer using ManagementClass.
当您使用 C# 时,用于检索此信息和其他硬件信息的类是ManagementClass,还有这个不错的项目可让您使用 ManagementClass 从计算机中检索大量信息。
回答by NotMe
The only*programmatic* way I know of doing this reliablyis:
我所知道的可靠地做到这一点的唯一*程序化*方式是:
- 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.
- 编写一个应用程序来抓取您的网络(或 IP 范围)以获取机器列表。
- 将这些机器展示给一个人,并要求他们选中一个框,如果它是一个虚拟机......
- 打印报告。
回答by Ajay Yadav
Run the systeminfo command @ command prompt see the system menufacturer n system model details. There you can find the virtule and physical machine information.
运行 systeminfo 命令@命令提示符查看系统 menufacturer n 系统型号详细信息。在那里您可以找到虚拟机和物理机信息。
回答by Mattias Lindberg
To check this from the command prompt you can run this: systeminfo | find "System"
要从命令提示符检查这一点,您可以运行以下命令: systeminfo | find "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
回答by AndyKn
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.
这对于我环境中有限范围的台式机和笔记本电脑返回“1”,对于 VMWare 工作站 9、ESX 5.5 以及 Citrix 6.5 和 7.6 返回“0”。BIOSCharacteristic“50”(一个“为系统供应商保留”)我只在四个虚拟环境中找到,所以它会反过来工作。
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.
在 HP 台式机上返回“5”,在 VMware 工作站 9 和 ESX 5.5 上返回“0”,未在其他台式机上测试。
回答by Hamid KhanZadeh
you can use this command in cmd or powershell
您可以在 cmd 或 powershell 中使用此命令
SYSTEMINFO
系统信息
You will find a line with the following text (or similar):
您会发现一行包含以下文本(或类似文本):
System Manufacturer: VMware, Inc. System Model: VMware Virtual Platform
系统制造商:VMware, Inc. 系统模型:VMware Virtual Platform