簡體   English   中英

如何使用 PowerShell 檢查是否啟用了 Hyper-V?

[英]How do you check to see if Hyper-V is enabled using PowerShell?

我正在嘗試編寫一個 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."
}

代碼運行時沒有錯誤。

這是適用於我的完整 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."
}

我相信這與你的if條件有關,試試這個:

if($hyperv.State -eq "Enabled")

=符號不起作用,你需要用 PowerShell 的方式來做

適用於 Windows 10 專業版/教育版/企業版

if ((Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online).State -ne 'Enabled')
{
    Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
}

對於 Windows 服務器

if ((Get-WindowsFeature -Name Hyper-V) -eq $false)
{
    Install-WindowsFeature -Name Hyper-V -IncludeManagementTools
}

通用腳本

Write-Host "Enabling Hyper-V in host..."
if ((Get-CimInstance Win32_OperatingSystem).Caption -match 'Microsoft Windows 10')
{
    if ((Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online).State -ne 'Enabled')
    {
        Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
    }
}
if ((Get-CimInstance Win32_OperatingSystem).Caption -match 'Microsoft Windows Server')
{

    if ((Get-WindowsFeature -Name Hyper-V) -eq $false)
    {
        Install-WindowsFeature -Name Hyper-V -IncludeManagementTools
    }
}

一種更簡單的方法是通過單擊“開始”按鈕並鍵入 Services.msc 並向下滾動到 Hyper-V 主機計算服務並查看它是否正在運行來轉到服務。 還要檢查 Hyper-V 虛擬機管理服務。

如果它們都在運行,您可以放心地假設 Hyper-V 正在運行並且處於活動狀態。 我的機器 Windows 10 Pro 和 VMWARE Workstation 14。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM