I have a script that is supposed to install features based on a list provided. The out is fine, but the commands always fail to install but they work in the powershell console.
$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
}
Using the enable windows optional feature method I get this error:
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
Using dism within the powershell script leads:
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
The problem ended up being that the string $feature actually is "Enabled" and I wasn't able to see that they were on the same line.
In the end, I solved the problem by doing this:
$f = $feature.replace(' ' , '').replace('Enabled', '')
Enable-WindowsOptionalFeature -Online -FeatureName $f -All -NoRestart