启用功能在 powershell 控制台中有效,但不适用于脚本
Enable feature works in powershell console but not as script
我有一个脚本应该根据提供的列表安装功能。输出很好,但命令总是无法安装,但它们可以在 powershell 控制台中运行。
$features_to_enable=New-Object
System.Collections.Generic.List[System.Object]
# We only want to read the features that are enabled.
Get-Content 'C:\features.txt' | Where-Object {$_ -match ".*Enabled"} |
ForEach-Object {
$features_to_enable.Add($_)
}
# Converted List to Array
$features_to_enable.ToArray()
forEach($feature in $features_to_enable) {
#Enable-WindowsOptionalFeature -Online -FeatureName "$($feature)" -All -NoRestart
dism /Online /Enable /FeatureName:"$($feature)" /All
}
使用启用 windows 可选功能方法我得到这个错误:
Enable-WindowsOptionalFeature : Feature name NetFx4ServerFeatures
Enabled is unknown.
At C:\features.ps1:12 char:3
+ Enable-WindowsOptionalFeature -Online -FeatureName "$($feature)" -A ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Enable-WindowsOptionalFeature], COMException
+ FullyQualifiedErrorId : Microsoft.Dism.Commands.EnableWindowsOptionalFeatureCommand
在 powershell 脚本中使用 dism 导致:
Deployment Image Servicing and Management tool
Version: 10.0.16299.15
Image Version: 10.0.16299.125
Error: 87
The enable option is unknown.
For more information, refer to the help by running DISM.exe /?.
The DISM log file can be found at C:\Windows\Logs\DISM\dism.log
从dism
版本来看,我认为Windows10在你的电脑上是运行,所以推荐使用PowerShell。
$features = (Get-WindowsOptionalFeature -Online | ? state -eq 'enabled').FeatureName
foreach($feature in $features) {
Enable-WindowsOptionalFeature -FeatureName $feature -Online -All -NoRestart
}
这在测试后对我有用。 :)
问题最终是字符串 $feature 实际上是 "Enabled" 而我无法看到它们在同一行上。
最后,我是这样解决问题的:
$f = $feature.replace(' ' , '').replace('Enabled', '')
Enable-WindowsOptionalFeature -Online -FeatureName $f -All -NoRestart
我有一个脚本应该根据提供的列表安装功能。输出很好,但命令总是无法安装,但它们可以在 powershell 控制台中运行。
$features_to_enable=New-Object
System.Collections.Generic.List[System.Object]
# We only want to read the features that are enabled.
Get-Content 'C:\features.txt' | Where-Object {$_ -match ".*Enabled"} |
ForEach-Object {
$features_to_enable.Add($_)
}
# Converted List to Array
$features_to_enable.ToArray()
forEach($feature in $features_to_enable) {
#Enable-WindowsOptionalFeature -Online -FeatureName "$($feature)" -All -NoRestart
dism /Online /Enable /FeatureName:"$($feature)" /All
}
使用启用 windows 可选功能方法我得到这个错误:
Enable-WindowsOptionalFeature : Feature name NetFx4ServerFeatures
Enabled is unknown.
At C:\features.ps1:12 char:3
+ Enable-WindowsOptionalFeature -Online -FeatureName "$($feature)" -A ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Enable-WindowsOptionalFeature], COMException
+ FullyQualifiedErrorId : Microsoft.Dism.Commands.EnableWindowsOptionalFeatureCommand
在 powershell 脚本中使用 dism 导致:
Deployment Image Servicing and Management tool
Version: 10.0.16299.15
Image Version: 10.0.16299.125
Error: 87
The enable option is unknown.
For more information, refer to the help by running DISM.exe /?.
The DISM log file can be found at C:\Windows\Logs\DISM\dism.log
从dism
版本来看,我认为Windows10在你的电脑上是运行,所以推荐使用PowerShell。
$features = (Get-WindowsOptionalFeature -Online | ? state -eq 'enabled').FeatureName
foreach($feature in $features) {
Enable-WindowsOptionalFeature -FeatureName $feature -Online -All -NoRestart
}
这在测试后对我有用。 :)
问题最终是字符串 $feature 实际上是 "Enabled" 而我无法看到它们在同一行上。
最后,我是这样解决问题的:
$f = $feature.replace(' ' , '').replace('Enabled', '')
Enable-WindowsOptionalFeature -Online -FeatureName $f -All -NoRestart