Created
September 5, 2019 13:14
-
-
Save MyITGuy/1a13d3dd57459206d80fb1f6676cb4c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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