Powershell - 如何使用 Get-WindowsOptionalFeature 命令 "Turn Windows Features On and Off"

Powershell - How to use Get-WindowsOptionalFeature command to "Turn Windows Features On and Off"

在Windows10中,可以在控制面板中"Turn windows features on and off";您会看到这样的屏幕:

假设我想 select IIS 6 WMI 兼容性 通过在 powershell 中使用 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 命令?

最终目标

最终目标是自动设置新开发人员和他的机器。

我想通了。

下面的代码用于使用通配符查找特征

$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
    }
}

上面的代码returns这个:

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

很好,您找到了适合您的答案,但是...

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

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