代码之家  ›  专栏  ›  技术社区  ›  Kellen Stuart Alan

Powershell-如何使用Get-WindowsOptionalFeature命令“打开和关闭Windows功能”

  •  3
  • Kellen Stuart Alan  · 技术社区  · 6 年前

    在Windows 10中,您可以 “打开和关闭windows功能” enter image description here

    假设我想选择 通过使用 Enable-WindowsOptionalFeature

    如果我跑:

    Get-WindowsOptionalFeature "IIS 6 WMI Compatibility"

    Get-WindowsOptionalFeature : A positional parameter cannot be found that accepts argument 'IIS 6 WMI Compatibility'.
    At line:1 char:1
    + Get-WindowsOptionalFeature "IIS 6 WMI Compatibility"
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-WindowsOptionalFeature], ParameterBindingException
        + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.Dism.Commands.GetWindowsOptionalFeatureCommand
    

    问题

    如何将这些功能的名称映射到PowerShell命令?

    最终目标

    2 回复  |  直到 6 年前
        1
  •  3
  •   postanote    6 年前

    很好你找到了一个适合你的答案,但是。。。

    但是,不需要函数来使用通配符。就这样。。。

    Get-WmiObject -Class $Win32_OperatingSystem
    
    
    SystemDirectory : C:\WINDOWS\system32
    Organization    : 
    BuildNumber     : 17134
    RegisteredUser  : 
    SerialNumber    : 00330-50027-66869-AAOEM
    Version         : 10.0.17134
    
    
    
    
    $PSVersionTable
    
    Name                           Value
    ----                           -----
    PSVersion                      5.1.17134.165
    PSEdition                      Desktop
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.17134.165}
    BuildVersion                   10.0.17134.165
    CLRVersion                     4.0.30319.42000
    WSManStackVersion              3.0
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
    
    
    
    # List features all
    (Get-WindowsOptionalFeature -Online -FeatureName '*') | Format-Table -Autosize
    (Get-WindowsOptionalFeature -Online -FeatureName '*').Count
    144
    
    # List features for IIS
    (Get-WindowsOptionalFeature -Online -FeatureName '*IIS*').Count
    54
    
    # List features for wmi
    (Get-WindowsOptionalFeature -Online -FeatureName '*wmi*').Count
    2
    
    # List features for IIS or wmi
    (Get-WindowsOptionalFeature -Online -FeatureName '*iis*|*wmi*').Count
    55
    
    
    # List features for IIS or wmi or hyperv
    (Get-WindowsOptionalFeature -Online -FeatureName '*iis*|*wmi*|*hyper*').Count
    63
    
        2
  •  2
  •   Kellen Stuart Alan    6 年前

    我想出来了。

    $features = Get-WindowsOptionalFeature -Online
    Write-Host ('There are ' + $features.Count + ' Windows features available') -ForegroundColor Green
    foreach($feature in $features)
    {
        if($feature.FeatureName -like "*IIS*WMI*") # wildcard search
        {
            $feature
        }
    }
    

    上面的代码返回:

    There are 170 Windows features available
    
    
    FeatureName : IIS-WMICompatibility
    State       : Disabled
    

    因此,要启用该功能,可以运行:

    $feature = Get-WindowsOptionalFeature -Online -FeatureName 'IIS-WMICompatibility'
    Enable-WindowsOptionalFeature $feature -Online
    

    注意:你必须跑 Enable-WindowsOptionalFeature

    您可以通过运行以下命令验证它是否已启用:

    (Get-WindowsOptionalFeature -Online -FeatureName 'IIS-WMICompatibility').State