`refactor`: add lib to uninstall/install optional features

- Optimized code from optimize-windows-features
- There are to switches to the function: `-Disabled` or `-Enabled`
- Also disable Internet Explorer from Optional Features
main
Plínio Larrubia 2 years ago committed by Plínio Larrubia
parent 4f17107bdb
commit 05dda986d8
No known key found for this signature in database
GPG Key ID: 057B0A87CB137C69

@ -0,0 +1,83 @@
Import-Module -DisableNameChecking $PSScriptRoot\..\lib\"title-templates.psm1"
function Find-OptionalFeature() {
[CmdletBinding()]
[OutputType([Bool])]
param (
[Parameter(Mandatory = $true)]
[String] $OptionalFeature
)
If (Get-WindowsOptionalFeature -Online -FeatureName $OptionalFeature) {
return $true
}
Else {
Write-Status -Symbol "?" -Type $TweakType -Status "The $OptionalFeature optional feature was not found." -Warning
return $false
}
}
function Set-OptionalFeatureState() {
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[Switch] $Disabled,
[Parameter(Mandatory = $false)]
[Switch] $Enabled,
[Parameter(Mandatory = $true)]
[Array] $OptionalFeatures,
[Parameter(Mandatory = $false)]
[Array] $Filter,
[Parameter(Mandatory = $false)]
[ScriptBlock] $CustomMessage
)
$Script:SecurityFilterOnEnable = @("IIS-*")
$Script:TweakType = "OptionalFeature"
ForEach ($OptionalFeature in $OptionalFeatures) {
If (Find-OptionalFeature $OptionalFeature) {
If (($OptionalFeature -in $SecurityFilterOnEnable) -and ($Enabled)) {
Write-Status -Symbol "?" -Type $TweakType -Status "Skipping $OptionalFeature to avoid a security vulnerability ..." -Warning
Continue
}
If ($OptionalFeature -in $Filter) {
Write-Status -Symbol "?" -Type $TweakType -Status "The $OptionalFeature will be skipped as set on Filter ..." -Warning
Continue
}
If (!$CustomMessage) {
If ($Disabled) {
Write-Status -Symbol "-" -Type $TweakType -Status "Uninstalling the $OptionalFeature optional feature ..."
}
ElseIf ($Enabled) {
Write-Status -Symbol "+" -Type $TweakType -Status "Installing the $OptionalFeature optional feature ..."
}
Else {
Write-Status -Symbol "?" -Type $TweakType -Status "No parameter received (valid params: -Disabled or -Enabled)" -Warning
}
}
Else {
Write-Status -Symbol "@" -Type $TweakType -Status $(Invoke-Expression "$CustomMessage")
}
If ($Disabled) {
Get-WindowsOptionalFeature -Online -FeatureName $OptionalFeature | Where-Object State -Like "Enabled" | Disable-WindowsOptionalFeature -Online -NoRestart
}
ElseIf ($Enabled) {
Get-WindowsOptionalFeature -Online -FeatureName $OptionalFeature | Where-Object State -Like "Disabled*" | Enable-WindowsOptionalFeature -Online -NoRestart
}
}
}
}
<#
Set-OptionalFeatureState -Disabled -OptionalFeatures @("OptionalFeature1", "OptionalFeature2", "OptionalFeature3")
Set-OptionalFeatureState -Disabled -OptionalFeatures @("OptionalFeature1", "OptionalFeature2", "OptionalFeature3") -Filter @("OptionalFeature3")
Set-OptionalFeatureState -Disabled -OptionalFeatures @("OptionalFeature1", "OptionalFeature2", "OptionalFeature3") -Filter @("OptionalFeature3") -CustomMessage { "Uninstalling $OptionalFeature feature!"}
Set-OptionalFeatureState -Enabled -OptionalFeatures @("OptionalFeature1", "OptionalFeature2", "OptionalFeature3")
Set-OptionalFeatureState -Enabled -OptionalFeatures @("OptionalFeature1", "OptionalFeature2", "OptionalFeature3") -Filter @("OptionalFeature3")
Set-OptionalFeatureState -Enabled -OptionalFeatures @("OptionalFeature1", "OptionalFeature2", "OptionalFeature3") -Filter @("OptionalFeature3") -CustomMessage { "Installing $OptionalFeature feature!"}
#>

@ -1,3 +1,4 @@
Import-Module -DisableNameChecking $PSScriptRoot\..\lib\"set-windows-feature-state.psm1"
Import-Module -DisableNameChecking $PSScriptRoot\..\lib\"title-templates.psm1"
# Adapted from: https://github.com/ChrisTitusTech/win10script/pull/131/files
@ -5,39 +6,13 @@ Import-Module -DisableNameChecking $PSScriptRoot\..\lib\"title-templates.psm1"
function Optimize-WindowsFeaturesList() {
[CmdletBinding()]
param (
[Switch] $Revert,
[Array] $EnableStatus = @(
@{
Symbol = "-"; Status = "Uninstalling";
Command = { Get-WindowsOptionalFeature -Online -FeatureName $Feature | Where-Object State -Like "Enabled" | Disable-WindowsOptionalFeature -Online -NoRestart }
}
@{
Symbol = "+"; Status = "Installing";
Command = { Get-WindowsOptionalFeature -Online -FeatureName $Feature | Where-Object State -Like "Disabled*" | Enable-WindowsOptionalFeature -Online -NoRestart }
}
)
[Switch] $Revert
)
$TweakType = "Feature"
If (($Revert)) {
Write-Status -Symbol "<" -Type $TweakType -Status "Reverting: $Revert." -Warning
$EnableStatus = @(
@{
Symbol = "<"; Status = "Re-Installing";
Command = { Get-WindowsOptionalFeature -Online -FeatureName $Feature | Where-Object State -Like "Disabled*" | Enable-WindowsOptionalFeature -Online -NoRestart }
}
@{
Symbol = "<"; Status = "Re-Uninstalling";
Command = { Get-WindowsOptionalFeature -Online -FeatureName $Feature | Where-Object State -Like "Enabled" | Disable-WindowsOptionalFeature -Online -NoRestart }
}
)
}
Write-Title -Text "Uninstall features from Windows"
$DisableFeatures = @(
"FaxServicesClientPackage" # Windows Fax and Scan
"IIS-*" # Internet Information Services
"Internet-Explorer-Optional-*" # Internet Explorer
"LegacyComponents" # Legacy Components
"MediaPlayback" # Media Features (Windows Media Player)
"MicrosoftWindowsPowerShellV2" # PowerShell 2.0
@ -46,21 +21,6 @@ function Optimize-WindowsFeaturesList() {
"Printing-XPSServices-Features" # Microsoft XPS Document Writer
"WorkFolders-Client" # Work Folders Client
)
ForEach ($Feature in $DisableFeatures) {
If (Get-WindowsOptionalFeature -Online -FeatureName $Feature) {
If (($Revert -eq $true) -and ($Feature -like "IIS-*")) {
Write-Status -Symbol "?" -Type $TweakType -Status "Skipping $Feature to avoid a security vulnerability ..." -Warning
Continue
}
Write-Status -Symbol $EnableStatus[0].Symbol -Type $TweakType -Status "$($EnableStatus[0].Status) $Feature ..."
Invoke-Expression "$($EnableStatus[0].Command)"
}
Else {
Write-Status -Symbol "?" -Type $TweakType -Status "$Feature was not found." -Warning
}
}
Write-Title -Text "Install features for Windows"
$EnableFeatures = @(
"NetFx3" # NET Framework 3.5
@ -68,15 +28,20 @@ function Optimize-WindowsFeaturesList() {
"NetFx4Extended-ASPNET45" # NET Framework 4.x + ASPNET 4.x
)
ForEach ($Feature in $EnableFeatures) {
If (Get-WindowsOptionalFeature -Online -FeatureName $Feature) {
Write-Status -Symbol "+" -Type $TweakType -Status "Installing $Feature ..."
Get-WindowsOptionalFeature -Online -FeatureName $Feature | Where-Object State -Like "Disabled*" | Enable-WindowsOptionalFeature -Online -NoRestart
}
Else {
Write-Status -Symbol "?" -Type $TweakType -Status "$Feature was not found." -Warning
}
Write-Title -Text "Optional Features Tweaks"
Write-Section -Text "Uninstall Optional Features from Windows"
If ($Revert) {
Write-Status -Symbol "<" -Type "OptionalFeature" -Status "Reverting the tweaks is set to '$Revert'." -Warning
$CustomMessage = { "Re-Installing the $OptionalFeature optional feature ..." }
Set-OptionalFeatureState -Enabled -OptionalFeatures $DisableFeatures -CustomMessage $CustomMessage
}
Else {
Set-OptionalFeatureState -Disabled -OptionalFeatures $DisableFeatures
}
Write-Section -Text "Install Optional Features from Windows"
Set-OptionalFeatureState -Enabled -OptionalFeatures $EnableFeatures
}
function Main() {
@ -89,7 +54,7 @@ function Main() {
# List all Windows Capabilities:
#Get-WindowsCapability -Online | Select-Object -Property State, Name | Sort-Object State, Name | Out-GridView
If (!($Revert)) {
If (!$Revert) {
Optimize-WindowsFeaturesList # Disable useless features and Enable features claimed as Optional on Windows, but actually, they are useful
}
Else {

Loading…
Cancel
Save