Kol*_*yon 5 windows powershell control-panel windows-10 powershell-5.0
在 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
Run Code Online (Sandbox Code Playgroud)
题
如何将这些功能的名称映射到 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
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码返回这个:
There are 170 Windows features available
FeatureName : IIS-WMICompatibility
State : Disabled
Run Code Online (Sandbox Code Playgroud)
因此,要启用该功能,您可以运行:
$feature = Get-WindowsOptionalFeature -Online -FeatureName 'IIS-WMICompatibility'
Enable-WindowsOptionalFeature $feature -Online
Run Code Online (Sandbox Code Playgroud)
注意:您必须以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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7555 次 |
| 最近记录: |