5

在 Windows 10 中,您可以在控制面板中“打开和关闭 Windows 功能” ;您会看到这样的屏幕: 在此处输入图像描述

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

最终目标

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

4

2 回答 2

7

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

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

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
于 2018-08-11T04:55:00.330 回答
5

我想到了。

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

$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

于 2018-08-10T16:33:16.570 回答