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.
 
    .Example
    # Test prerequisites and install missing features.
    Test-P1Prerequisites -Install
    #>

    [cmdletbinding()]
    param (
        [switch] $Install = $false
    )
    Process
    {
        Import-Module servermanager

        $features = ('Web-Server', 'Web-Mgmt-Console', 'Web-Request-Monitor', 'Web-Windows-Auth', 'Web-Asp-Net45', 'Web-Net-Ext45', 'Web-ISAPI-Filter', 'Web-ISAPI-Ext', 'NET-WCF-HTTP-Activation45', 'NET-Framework-45-ASPNET')
        $missing = @()
        $rebootNeeded = $false

        Write-Section "Checking PlannerOne's prerequisites..."

        foreach ($featureName in $features) {
            # check whether the featue is installed
            $feature = Get-WindowsFeature $featureName

            $message = $feature.DisplayName + " : " + $feature.InstallState
            
            switch ($feature.InstallState) {
                InstallPending
                    { Write-Alert $message; $rebootNeeded = $true }
                UninstallPending
                    { Write-Alert $message; $missing += $featureName }
                Available
                    { Write-Alert $message; $missing += $featureName }
                Default
                    { Write-OK $message }
            }
        }

        Write-Output ''

        if ($missing.Count -eq 0) {
            if ($rebootNeeded) {
                Write-Alert 'All features are installed, but you need to reboot!'
            } else {
                Write-OK 'No missing features! Ready for PlannerOne!'
            }
        } else {
            Write-Alert ('Missing features: '+ $missing)

            if ($Install) {
                Write-Output ''
                Write-Section 'Installing missing features'

                foreach ($featureName in $missing) {
                    # check whether the featue is installed
                    $feature = Get-WindowsFeature $featureName
                    Add-WindowsFeature $featureName
                }
            } else {
                Write-Alert 'Run the command again with -Install to fullfill prerequisites'
            }
        }
    }
}