本文介绍了用于检测Hyper-V Gen 2 VM与Gen 1 VM的PowerShell代码(从VM本身运行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在从VM运行的PowerShell脚本中,我想以编程方式确定VM是第一代Hyper-V VM,还是第二代Hyper-V VM.假设该虚拟机运行的Windows版本与第二代虚拟机中运行的Windows兼容.

In a PowerShell script running from a VM, I'd like to programmatically determine if the VM is a generation 1 Hyper-V VM, or if it is a generation 2 Hyper-V VM. Assume the VM is running a version of Windows compatible with running in a Gen 2 VM.

此问题与检测Hyper-V无关.既然脚本正在VM上运行,并且VM在Hyper-V上运行,那么如何确定VM的代号?

This question is not about detecting Hyper-V. Given that the script is running on a VM and the VM is running on Hyper-V, how do I determine the VM's generation number?

可能的线索:

示例C ++ MSDN查询VM生成ID的代码-也许有一种方法可以将此C ++代码转换为C#,然后在运行时使用Add-Type动态地对其进行编译?我认为这可能是最好的方法,但这超出了我的技术水平.

Sample C++ MSDN code to query VM Generation ID - perhaps there is a way to convert this C++ code to C#, then dynamically compile it at runtime using Add-Type? I think this might be the best approach, but it's beyond my skill level.

GitHub上的VMDE项目-具有各种检测VM平台的方法,但未编写在PowerShell中,使其正常工作超出了我的技能水平

VMDE Project on GitHub - has all kinds of methods for detecting VM platforms, but it's not written in PowerShell, and getting it to work is beyond my skill level

提前谢谢!

坦率

推荐答案

我假设,如果系统是Hyper-V VM,并且正在运行UEFI(而不是BIOS),则它是Hyper-V.第2代VM.同样,如果它是未运行UEFI的Hyper-V VM,则它必须是Hyper-V Gen 1 VM.在撰写本文时,这些假设是正确的.

I made the assumption that if the system is a Hyper-V VM and if it is running UEFI (instead of BIOS), then it is a Hyper-V Gen 2 VM. Likewise, if it is a Hyper-V VM not running UEFI, then it must be a Hyper-V Gen 1 VM. At the time of writing, these assumptions are true.

我今天下午参加了一场编程马拉松,并且有一个有效的解决方案,最容易在我的GitHub上阅读和维护: https://github.com/franklesniak/PowerShell_Resources/blob/master/Get-HyperVVMGenerationNumberFromWithinVM.ps1

I went on a coding marathon this afternoon and have a working solution, most easily read and maintained on my GitHub: https://github.com/franklesniak/PowerShell_Resources/blob/master/Get-HyperVVMGenerationNumberFromWithinVM.ps1

不幸的是,该代码超出了此处有关Stack Overflow的帖子的允许限制,因此我无法在此帖子中提供代码.但是,感兴趣的功能是 Get-HyperVVMGenerationNumberFromWithinVM ,并且在下面将其包括在内,尽管如果未加载GitHub链接中包含的必备功能,它将无法运行.

Unfortunately, the code exceeds the allowable limit of a post here on Stack Overflow, so I can't supply the code in this post. But, the function of interest is Get-HyperVVMGenerationNumberFromWithinVM and I've included it below, though it will not run without the prerequisite functions loaded, included in the GitHub link.

#region License
###############################################################################################
# Copyright 2020 Frank Lesniak

# Permission is hereby granted, free of charge, to any person obtaining a copy of this software
# and associated documentation files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
# BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
###############################################################################################
#endregion License

function Get-HyperVVMGenerationNumberFromWithinVM {
    # Returns an integer indicating the Hyper-V VM generation number of this system
    # Returns 2 if this system is a Hyper-V VM running as a "Generation 2" Hyper-V VM
    # Returns 1 if this system is a Hyper-V VM running as a "Generation 1" Hyper-V VM
    #   Note: Microsoft Virtual Server and Virtual PC VMs can also return 1
    # Returns 0 if this system is a Hyper-V VM, but the generation number could not be
    #   determined due to an error. Usually this would only occur if the VM is Vista/Windows
    #   Server 2008 system and the PowerShell script was run without administrative privileges.
    #   Check the Warning stream for more information.
    #   Note: Microsoft Virtual Server and Virtual PC VMs can also return 0
    # Returns -1 if this system is not a Hyper-V VM
    $boolHyperVVM = Test-ThisSystemIsAHyperVVM
    if ($null -ne $boolHyperVVM) {
        if ($boolHyperVVM) {
            $boolUEFI = Test-UEFISystem
            if ($null -ne $boolUEFI) {
                if ($boolUEFI) {
                    # Hyper-V VM with UEFI
                    # Generation 2
                    2
                } else {
                    # Hyper-V VM not running UEFI
                    # Generation 1
                    1
                }
            } else {
                # Is a Hyper-V VM but could not determine whether UEFI is running
                # Error condition
                0
            }
        } else {
            # Not a Hyper-V VM
            -1
        }
    } else {
        $null
    }
}

这篇关于用于检测Hyper-V Gen 2 VM与Gen 1 VM的PowerShell代码(从VM本身运行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 18:37