Public/Test-P1Prerequisites.ps1

function Test-P1Prerequisites {
    <#
    .Synopsis
    Test if prerequisites of PlannerOne are installed on the machine.

    .Description
    Look for a list a features that must be installed.
    Use Install parameter to force installation of missing features.

    .Parameter Install
    Force installation of missing features.

    .Parameter AppServerOnly
    Install only PlannerOne application server prerequisites

    .Example
    # Test prerequisites and install missing features.
    Test-P1Prerequisites -Install
    #>

    [cmdletbinding()]
    param (
        [switch] $Install = $false,
        [switch] $AppServerOnly = $false
    )
    Process
    {
        $OperatingSystemCaption = (Get-WmiObject win32_operatingsystem).caption

        if ((Get-Module -ListAvailable -Name 'ServerManager') -and ($OperatingSystemCaption -like "*windows*server*")) {

            Import-Module 'ServerManager'

            if ($AppServerOnly) {
                $featureNames = ('NET-Framework-45-Core')
            } else {
                $featureNames = ('Web-Server', 'Web-Mgmt-Console', 'Web-Windows-Auth', 'NET-Framework-45-Core', 'Web-Asp-Net45', 'Web-Net-Ext45', 'Web-ISAPI-Filter', 'Web-ISAPI-Ext', 'NET-WCF-HTTP-Activation45', 'NET-Framework-45-ASPNET')
            }
            
            Write-Verbose "Checking PlannerOne's prerequisites..."
            $features = Get-WindowsFeature $featureNames

            $missingFeatures = ($features | Where-Object InstallState -ne Installed)

            if ($missingFeatures.Count -eq 0) {
                Write-Verbose 'No missing features! Ready for PlannerOne!'
            } else {
                if ($Install) {
                    Write-Verbose 'Installing missing features'
                    Add-WindowsFeature $missingFeatures | Out-Null
                    $features = Get-WindowsFeature $featureNames
                } else {
                    Write-Warning 'Something is missing. Run the command again with -Install to fullfill prerequisites.'
                    Write-Warning 'Run with -AppServerOnly switch to test only PlannerOne application server prerequisites.'
                }
            }
        } elseif ((Get-Module -ListAvailable -Name 'DISM') -Or (Test-Path $DISMModulePath)) {
            if ($AppServerOnly) {
                Write-Warning "AppServerOnly option is not available in DISM mode."
                return
            }

            if (Test-Path $DISMModulePath) {
                Write-Verbose "Using $DISMModulePath to load DISM module"
                Import-Module $DISMModulePath
                $env:path = $DISMModulePath
            } else {
                Import-Module 'DISM'
            }

            $featureNames = ('IIS-WebServer', 'IIS-ManagementConsole', 'IIS-WindowsAuthentication', 'IIS-ASPNET45', 'IIS-NetFxExtensibility45', 'IIS-ISAPIFilter', 'IIS-ISAPIExtensions', 'WCF-HTTP-Activation45', 'NetFx4Extended-ASPNET45')

            Write-Verbose "Checking PlannerOne's prerequisites..."
            $features = @()

            foreach ($featureName in $featureNames) {
                Write-Verbose ">> Checking feature: $featureName"
                $tempFeature = Get-WindowsOptionalFeature -Online -FeatureName $featureName
                if ($null -ne $tempFeature) { 
                    $features += $tempFeature 
                } else {
                    Write-Warning "Feature $featureName not found!"
                }
            } 

            $missingFeatures = ($features | Where-Object State -ne Enabled)

            if ($missingFeatures.Count -eq 0) {
                Write-Verbose 'No missing features! Ready for PlannerOne!'
            } else {
                if ($Install) {
                    Write-Verbose 'Installing missing features'
                    $features = @()
                    foreach ($missingFeature in $missingFeatures) {
                        Enable-WindowsOptionalFeature -Online -FeatureName $missingFeature.FeatureName -All | Out-Null
                        $features += Get-WindowsOptionalFeature -Online -FeatureName $missingFeature.FeatureName
                    }
                } else {
                    Write-Warning 'Something is missing. Run the command again with -Install to fullfill prerequisites.'
                }
            }

        } else {
            Write-Warning 'Your OS doesn''t allow automatic installation of PlannerOne prerequisites.'
            Write-Warning 'If your are using Windows 7 or 8, try to install Windows ADK (https://technet.microsoft.com/en-us/library/hh825494.aspx) and retry.'
        }

        return $features
    }
}