Install-ISHWindowsFeature.ps1

<#
# Copyright (c) 2014 All Rights Reserved by the SDL Group.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#>


function Install-ISHWindowsFeature
{
    [CmdletBinding()]
    Param()
    
    begin 
    {
        . $PSScriptRoot\Private\Test-RunningAsElevated.ps1
        Test-RunningAsElevated -StopCallerPSCmdlet $PSCmdlet
    
        . $PSScriptRoot\Get-ISHOSInfo.ps1
    }

    process
    {
        $osInfo=Get-ISHOSInfo
        <#
        ServerManager is expected in Windows Server operating system.
        In a client OS like Windows 7,8 or 10, ServerManager is not available.
        Instead there is DISM.
        Server and DISM are much alike but the name of cmdlet, the terms and flow is a bit different.
        With DISM you need to check if a feature is installed before installing it.
 
        #>

        if($osInfo.IsServer)
        {

            $featureNames=@(
                #http://docs.sdl.com/LiveContent/content/en-US/SDL%20Knowledge%20Center%20full%20documentation-v2/GUID-47EC6977-C62C-493E-B6DA-F0A3D0003C9D
                #"Application-Server"
        "NET-WCF-HTTP-Activation45"
                #http://docs.sdl.com/LiveContent/content/en-US/SDL%20Knowledge%20Center%20full%20documentation-v2/GUID-F0F2DB60-4C4F-4962-9FC1-AE8D2F1929FE
                "Web-Default-Doc"
                "Web-Dir-Browsing"
                "Web-Http-Errors"
                "Web-Static-Content"
                "Web-Http-Logging"
                "Web-Request-Monitor"
                "Web-Stat-Compression"
                "Web-Dyn-Compression"
                "Web-Net-Ext45"
                "Web-ASP"
                "Web-Asp-Net45"
                "Web-ISAPI-Ext"
                "Web-ISAPI-Filter"
            )

            switch ($osInfo.Version)
            {
                '2016' {
                }
                '2012 R2' {
                    $featureNames+="AS-NET-Framework"
                    #http://docs.sdl.com/LiveContent/content/en-US/SDL%20Knowledge%20Center%20full%20documentation-v2/GUID-B06F62DB-9D30-4C2E-8C89-C116BD8F0829
                    $featureNames+="AS-WS-Atomic"
                    $featureNames+="AS-Incoming-Trans"
                    $featureNames+="AS-Outgoing-Trans"
                    $featureNames+="Web-Mgmt-Console"
                }
                Default {throw "Not supported operating system $($osInfo.Caption)" }
            }
        
            # On Windows Server 2016 the following import must happen explicitly
            Import-Module ServerManager
        
            Install-WindowsFeature -Name $featureNames |Out-Null
        }
        else
        {
            #List generated by Alex Sarafian's working laptop on Windows 10
            # Get-WindowsOptionalFeature -Online |Where-Object -Property State -Eq "Enabled"| Select-object -ExpandProperty FeatureName
            $features=@(
                "IIS-WebServerRole"
                "IIS-WebServer"
                "IIS-CommonHttpFeatures"
                "IIS-ApplicationDevelopment"
                "IIS-NetFxExtensibility45"
                "IIS-Security"
                "IIS-RequestFiltering"
                "IIS-Performance"
                "IIS-HttpCompressionDynamic"
                "IIS-WebServerManagementTools"
                "WAS-WindowsActivationService"
                "WAS-ProcessModel"
                "WAS-ConfigurationAPI"
                "IIS-WindowsAuthentication"
                "IIS-StaticContent"
                "IIS-DefaultDocument"
                "IIS-WebSockets"
                "IIS-ASPNET45"
                "IIS-ASP"
                "IIS-ISAPIExtensions"
                "IIS-ISAPIFilter"
                "IIS-BasicAuthentication"
                "IIS-HttpCompressionStatic"
                "IIS-ManagementConsole"
                "IIS-FTPServer"
                "WCF-Services45"
                "WCF-HTTP-Activation45"
                "NetFx4-AdvSrvs"
                "NetFx4Extended-ASPNET45"
            )

            switch ($osInfo.Version)
            {
                '10' {
                }
                Default {throw "Not supported operating system $caption" }
            }
            $notInstalledFeatures=$features|ForEach-Object {
                $feature=Get-WindowsOptionalFeature -FeatureName $_ -Online
                if($feature.State -eq "Enabled")
                {
                    Write-Warning "$($feature.FeatureName) is already installed"
                }
                else
                {
                    Write-Verbose "$($feature.FeatureName) will be installed"
                    $feature
                }
            }
            #$notInstalledFeatures|Format-Table
            $notInstalledFeatures| ForEach-Object {
                try
                {
                    $featureName=$_.FeatureName
                    Enable-WindowsOptionalFeature -FeatureName $featureName -Online -All
                    Write-Host "Enabled feature $featureName"
                }
                catch
                {
                    Write-Error "Error for $featureName"
                    Write-Error "$_"
                }
            }
       }
    }

    end
    {

    }
}