簡體   English   中英

Powershell - 如何使用Get-WindowsOptionalFeature命令“打開和關閉Windows功能”

[英]Powershell - How to use Get-WindowsOptionalFeature command to “Turn Windows Features On and Off”

在Windows 10中,您可以在控制面板中“打開和關閉Windows功能” ; 你看到一個屏幕: 在此輸入圖像描述

假設我想通過Enable-WindowsOptionalFeature中的Enable-WindowsOptionalFeature命令選擇IIS 6 WMI兼容性

如果我跑:

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命令?

結束目標

最終目標是自動設置新的開發人員和他的機器。

嗯,你找到了一個適合你的答案,但......

但是,您不需要使用通配符的函數。 這樣做......

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

我想到了。

以下代碼用於通過使用通配符查找功能

$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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM