小编Adr*_*ete的帖子

如何在 Windows 10 上向 Get-WindowsOptionalFeature 返回真/假

我试图在安装之前检查是否安装了 Windows 功能,以避免重新安装。

我用它来检查:

function Check-WindowsFeature {
    [CmdletBinding()]
    param(
        [Parameter(Position=0,Mandatory=$true)] [string]$FeatureName 
    )  
  if((Get-WindowsOptionalFeature -FeatureName $FeatureName -Online) -Like "Enabled") {
        # $FeatureName is Installed
        # (simplified function to paste here)
    } else {
        # Install $FeatureName 
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但是“if”总是返回false,即使安装了功能。
例子:

C:\> (Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online) -Like "Enabled"
False

C:\> Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online | ft -Property State
State
-----
Enabled
Run Code Online (Sandbox Code Playgroud)

我已经尝试过格式为表格,格式为宽,过滤器-Property State,输出为字符串,与正则表达式匹配,-eq与contains和匹配,但都不起作用。

功能$FeatureName正常,因为安装有效。

如何使这项工作?

powershell windows-10

0
推荐指数
1
解决办法
5038
查看次数

标签 统计

powershell ×1

windows-10 ×1