Skip to content

Instantly share code, notes, and snippets.

@MyITGuy
Created September 5, 2019 13:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MyITGuy/1a13d3dd57459206d80fb1f6676cb4c7 to your computer and use it in GitHub Desktop.
Save MyITGuy/1a13d3dd57459206d80fb1f6676cb4c7 to your computer and use it in GitHub Desktop.
#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment