Wednesday, November 25, 2015

Installing Windows Features dynamically using PowerShell

I recently had to install a set of Windows IIS features on a Windows Server 2008 R2 box, when I was suddenly faced with this error message:

"The all option is not recognized in this context."

I had been using this article for DISM to execute my commands in the past on Windows Server 2012 and Windows Server 2012 R2: 

Even though the article says that it applies to Windows Server 2008 R2, according to the error message, this does not appear to work!

Therefore, I decided to write my own custom PowerShell script that would allow me to "dynamically" install Windows Features without manually having to re-write my existing Windows Feature command for different platforms.

This is finally what I came up with:

$IISFeatures = @("NetFx3", "IIS-ASPNET", "IIS-NetFxExtensibility", "IIS-ApplicationDevelopment","IIS-WebServer", "IIS-WebServerRole", "IIS-DefaultDocument", "IIS-CommonHttpFeatures", "IIS-ISAPIFilter", "IIS-ISAPIExtensions", "IIS-NetFxExtensibility", "IIS-RequestFiltering", "IIS-Security")
$IISFeatureNames = New-Object System.Collections.ArrayList
foreach ($feature in $IISFeatures)
    $FeatureName = "/FeatureName:" + $feature
    Write-Host $FeatureName
#Dynamically enable the list of features
& DISM /Online /Enable-Feature $IISFeatureNames

When I want to add or install a new Windows Feature, I simply add the name of the Windows Feature to the $IISFeatures array.

You can get a list of available Windows Features by running this command:

dism /online /get-features | more

No comments:

Post a Comment