MyITGuy
9/5/2019 - 1:14 PM

Get-DismWindowsFeature

#region Get-DismWindowsFeature
function Get-DismWindowsFeature {
    [CmdletBinding()]
    PARAM(
        [Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true)]
        [string]
        $Name
        ,
        [Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true)]
        [switch]
        $Enabled
    )

    begin {
        Write-Verbose $MyInvocation.MyCommand
        $script:EnabledPreference = $PSBoundParameters.ContainsKey('Enabled')

        filter FilterByName {
            if ($Name) {
                $_ | Where-Object {$_.FeatureName -eq $Name}
            }
            else {
                $_
            }
        }

        filter FilterByEnabled {
            if ($script:EnabledPreference -eq $false) {
                $_
            }
            elseif ($Enabled -eq $true) {
                $_ | Where-Object { $_.State -eq 'Enabled' }
            }
            elseif ($Enabled -eq $false) {
                $_ | Where-Object { $_.State -ne 'Enabled' }
            }
        }
    }

    process {
        try {
            dism.exe /online /get-features /format:table | Select-Object -Skip 12 | ConvertFrom-Csv -Header "Feature Name", "State" -Delimiter "|" | ForEach-Object {
                $Properties = @{
                    FeatureName = $null
                    State       = $null
                }
                if ($_."Feature Name" -notmatch "The operation completed successfully.") {
                    $Properties.FeatureName = $_."Feature Name".Trim() ;
                    $Properties.State       = $_.State.Trim() ;
                    $obj = New-Object -TypeName PSObject -Property $Properties
                    Write-Output -InputObject $obj
                }
            } | FilterByName | FilterByEnabled
        }
        catch {
            Throw $_
        }
    }

    end {
    }
}
#endregion Get-DismWindowsFeature

# Get-DismWindowsFeature
# Get-DismWindowsFeature -Enabled
# Get-DismWindowsFeature -Enabled:$false
# Get-DismWindowsFeature -Name 'IIS-ASP' -Enabled
# Get-DismWindowsFeature -Name 'IIS-ASP' -Enabled:$false -Verbose