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

时间:2018-08-10 15:37:19

标签: windows powershell windows-10 powershell-v5.0 control-panel

在Windows 10中,您可以在控制面板中“打开和关闭Windows功能” ;您会看到这样的屏幕: enter image description here

假设我要通过Powershell中的/* Call WAITFOR DELAY inside SQL View */ /* Usefull for example for async testing */ CREATE FUNCTION WaitForDelay() RETURNS INT AS BEGIN RETURN ( SELECT Value FROM OPENROWSET ( 'SQLOLEDB', 'Trusted_Connection=yes; Integrated Security=SSPI; Server=localhost; Initial_Catalog=master;', 'WAITFOR DELAY ''00:00:02'' SELECT 0 AS Value' )) END GO CREATE VIEW Wait AS SELECT dbo.WaitForDelay() AS Value GO SELECT * FROM Wait /* Takes sql view 2 seconds to respond */ 命令选择 IIS 6 WMI兼容性

如果我运行:

Enable-WindowsOptionalFeature

我收到此错误:

Get-WindowsOptionalFeature "IIS 6 WMI Compatibility"

问题

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

最终目标

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

2 个答案:

答案 0 :(得分:3)

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

但是,您不需要使用通配符的功能。只是这样做...

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

答案 1 :(得分:2)

我知道了。

下面的代码用于使用通配符查找功能

$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