`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 Featuresmain
parent
4f17107bdb
commit
05dda986d8
@ -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!"}
|
||||
#>
|
Loading…
Reference in New Issue