【问题标题】:Identify if Windows hosted on physical or Virtual machine? #Powershell确定 Windows 是托管在物理机还是虚拟机上? #电源外壳
【发布时间】:2020-04-16 18:47:01
【问题描述】:

我正在尝试查找 Windows 操作系统是托管在物理机上还是虚拟机上。

互联网上有一个 sn-p 的 powershell 脚本,我在其中添加了几个条件来识别机器是否托管在云上(那么它可能是虚拟机)。


function GetMachineType {
    $ComputerSystemInfo = Get-WmiObject -Class Win32_ComputerSystem
    switch ($ComputerSystemInfo.Model) { 

        # Check for VMware Machine Type 
        "VMware Virtual Platform" { 
            Write-Output "This Machine is Virtual on VMware Virtual Platform."
            Break 
        } 

        # Check for Oracle VM Machine Type 
        "VirtualBox" { 
            Write-Output "This Machine is Virtual on Oracle VM Platform."
            Break 
        } 
        default { 

            switch ($ComputerSystemInfo.Manufacturer) {

                # Check for Xen VM Machine Type
                "Xen" {
                    Write-Output "This Machine is Virtual on Xen Platform"
                    Break
                }

                # Check for KVM VM Machine Type
                "QEMU" {
                    Write-Output "This Machine is Virtual on KVM Platform."
                    Break
                }
                # Check for Hyper-V Machine Type 
                "Microsoft Corporation" { 
                    if (get-service WindowsAzureGuestAgent -ErrorAction SilentlyContinue) {
                        Write-Output "This Machine is Virtual on Azure Platform"
                    }
                    else {
                        Write-Output "This Machine is Virtual on Hyper-V Platform"
                    }
                    Break
                }
                # Check for Google Cloud Platform
                "Google" {
                    Write-Output "This Machine is Virtual on Google Cloud."
                    Break
                }

                # Check for AWS Cloud Platform
                default { 
                    if ((((Get-WmiObject -query "select uuid from Win32_ComputerSystemProduct" | Select-Object UUID).UUID).substring(0, 3) ) -match "EC2") {
                        Write-Output "This Machine is Virtual on AWS"
                    }
                    # Otherwise it is a physical Box 
                    else {
                        Write-Output "This Machine is Physical Platform"
                    }
                } 
            }                  
        } 
    } 

}

仅当 VM 处于 HYPER-V 上时,以下注册表项才会提供信息

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters*

我想知道是否有任何通用方法可以通过编程方式确定 Windows 操作系统是托管在物理机上还是虚拟机上。

【问题讨论】:

  • 您可能需要将"Virtual Machine" { Write-Output "This Machine is Virtual on Hyper-V Platform."; break } 添加到$ComputerSystemInfo.Model 开关。
  • 这是一个可能有帮助的脚本:gallery.technet.microsoft.com/scriptcenter/…
  • 嘿@Theo,这是在 switch 语句的默认块中处理的。但不幸的是,这不是我的问题。 :( 。我需要一种替代的通用方法来确定机器是物理的还是虚拟的,因为它依赖于所有可用的虚拟化提供程序,这些提供程序需要单独处理。
  • @Glenn 那是非常相似的脚本..

标签: windows powershell virtual-machine virtualization


【解决方案1】:
    $IsVirtual=((Get-WmiObject win32_computersystem).model -eq 'VMware Virtual Platform' -or ((Get-WmiObject win32_computersystem).model -eq 'Virtual Machine'))

它将输出 $True 或 $False 与您的房地产中的其他虚拟模型/制造商一起扩展此功能

【讨论】:

  • 例如,您还需要检查“Parallels Virtual Platform”。因此,我会使用以下方法:$IsVirtual = ((Get-WmiObject Win32_ComputerSystem).model).Contains("Virtual")
猜你喜欢
  • 2013-03-07
  • 1970-01-01
  • 2021-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-30
相关资源
最近更新 更多