windows 如何使用 PowerShell 检查是否启用了 Hyper-V?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37567596/
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 do you check to see if Hyper-V is enabled using PowerShell?
提问by Evan Amara
I am trying to write a PowerShell script that checks the Windows Optional Features to see if Hyper-V is installed. However, my code is not working. Even when Hyper-V is disabled, the script outputs that it is already enabled.
我正在尝试编写一个 PowerShell 脚本来检查 Windows 可选功能以查看是否安装了 Hyper-V。但是,我的代码不起作用。即使禁用了 Hyper-V,脚本也会输出它已启用。
#Requires -RunAsAdministrator
# Get the Hyper-V feature and store it in $hyperv
$hyperv = Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online
# Check if Hyper-V is already enabled.
if($hyperv.State = "Enabled") {
Write-Host "Hyper-V is already enabled."
} else {
Write-Host "Hyper-V is disabled."
}
There is no error when the code is run.
代码运行时没有错误。
采纳答案by Andrey Marchuk
I believe it has to do with your if
condition, try this:
我相信这与你的if
情况有关,试试这个:
if($hyperv.State -eq "Enabled")
The =
sign is not going to work, you need to do it PowerShell way
该=
标志不起作用,您需要以 PowerShell 的方式进行
回答by Jon
Here's the full powershell script that works for me. Just copy and paste it into an elevated powershell then press enter.
这是适用于我的完整 powershell 脚本。只需将其复制并粘贴到提升的 powershell 中,然后按 Enter。
$hyperv = Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online
# Check if Hyper-V is enabled
if($hyperv.State -eq "Enabled") {
Write-Host "Hyper-V is enabled."
} else {
Write-Host "Hyper-V is disabled."
}
回答by Ron Holloway
An easier way to do that is to go to Services by clicking the Start button and typing Services.msc and scrolling down to the Hyper-V Host Compute Service and seeing if it running. Also check the Hyper-V Virtual Machine Management service.
一种更简单的方法是通过单击“开始”按钮并键入 Services.msc 并向下滚动到 Hyper-V 主机计算服务并查看它是否正在运行来转到服务。还要检查 Hyper-V 虚拟机管理服务。
If they are both running you can safely assume that Hyper-V is running and active. My machine Windows 10 Pro with VMWARE Workstation 14.
如果它们都在运行,您可以放心地假设 Hyper-V 正在运行并且处于活动状态。我的机器 Windows 10 Pro 和 VMWARE Workstation 14。