Debloat lib refactor/cleanup part 1

- Decrease the unnecessary amount of complexity in some funcions
main
Plínio Larrubia 2 years ago committed by Plínio Larrubia
parent fa0a439b63
commit 197b8e47f4
No known key found for this signature in database
GPG Key ID: 057B0A87CB137C69

@ -36,7 +36,7 @@ jobs:
path: .\ path: .\
recurse: true recurse: true
# Exclude rules that will not be worked on / are invalid # Exclude rules that will not be worked on / are invalid
# Command: invoke-scriptanalyzer . -excludeRule: "PSAvoidUsingInvokeExpression", "PSAvoidUsingWriteHost", "PSUseShouldProcessForStateChangingFunctions" # Command: invoke-scriptanalyzer . -recurse -excludeRule: "PSAvoidUsingInvokeExpression", "PSAvoidUsingWriteHost", "PSUseShouldProcessForStateChangingFunctions"
excludeRule: '"PSAvoidUsingInvokeExpression", "PSAvoidUsingWriteHost", "PSUseShouldProcessForStateChangingFunctions"' excludeRule: '"PSAvoidUsingInvokeExpression", "PSAvoidUsingWriteHost", "PSUseShouldProcessForStateChangingFunctions"'
output: results.sarif output: results.sarif

@ -1,60 +1,38 @@
Import-Module -DisableNameChecking $PSScriptRoot\..\"title-templates.psm1" Import-Module -DisableNameChecking $PSScriptRoot\..\"title-templates.psm1"
function Find-ScheduledTask() {
[CmdletBinding()]
[OutputType([Bool])]
param (
[Parameter(Mandatory = $true)]
[String] $ScheduledTask
)
If (Get-ScheduledTaskInfo -TaskName $ScheduledTask -ErrorAction SilentlyContinue) {
return $true
} Else {
Write-Status -Types "?", $TweakType -Status "The $ScheduledTask task was not found." -Warning
return $false
}
}
function Set-ScheduledTaskState() { function Set-ScheduledTaskState() {
[CmdletBinding()] [CmdletBinding()]
param ( param (
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $true)]
[Switch] $Disabled, [ValidateSet('Disabled', 'Enabled')]
[Parameter(Mandatory = $false)] [String] $State,
[Switch] $Ready,
[Parameter(Mandatory = $true)] [Parameter(Mandatory = $true)]
[Array] $ScheduledTasks, [Array] $ScheduledTasks,
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
[Array] $Filter, [Array] $Filter
[Parameter(Mandatory = $false)]
[ScriptBlock] $CustomMessage
) )
$Script:TweakType = "TaskScheduler" Begin {
$Script:TweakType = "TaskScheduler"
}
ForEach ($ScheduledTask in $ScheduledTasks) { Process {
If (Find-ScheduledTask $ScheduledTask) { ForEach ($ScheduledTask in $ScheduledTasks) {
If ($ScheduledTask -in $Filter) { If (!(Get-ScheduledTaskInfo -TaskName $ScheduledTask -ErrorAction SilentlyContinue)) {
Write-Status -Types "?", $TweakType -Status "The $ScheduledTask ($((Get-ScheduledTask $ScheduledTask).TaskName)) will be skipped as set on Filter..." -Warning Write-Status -Types "?", $TweakType -Status "The $ScheduledTask task was not found." -Warning
Continue Continue
} }
If (!$CustomMessage) { If ($ScheduledTask -in $Filter) {
If ($Disabled) { Write-Status -Types "?", $TweakType -Status "The $ScheduledTask ($((Get-ScheduledTask $ScheduledTask).TaskName)) will be skipped as set on Filter..." -Warning
Write-Status -Types "-", $TweakType -Status "Disabling the $ScheduledTask task..." Continue
} ElseIf ($Ready) {
Write-Status -Types "+", $TweakType -Status "Enabling the $ScheduledTask task..."
} Else {
Write-Status -Types "?", $TweakType -Status "No parameter received (valid params: -Disabled or -Ready)" -Warning
}
} Else {
Write-Status -Types "@", $TweakType -Status $(Invoke-Expression "$CustomMessage")
} }
If ($Disabled) { If ($State -eq 'Disabled') {
Write-Status -Types "-", $TweakType -Status "Disabling the $ScheduledTask task..."
Get-ScheduledTask -TaskName (Split-Path -Path $ScheduledTask -Leaf) | Where-Object State -Like "R*" | Disable-ScheduledTask # R* = Ready/Running Get-ScheduledTask -TaskName (Split-Path -Path $ScheduledTask -Leaf) | Where-Object State -Like "R*" | Disable-ScheduledTask # R* = Ready/Running
} ElseIf ($Ready) { } ElseIf ($State -eq 'Enabled') {
Write-Status -Types "+", $TweakType -Status "Enabling the $ScheduledTask task..."
Get-ScheduledTask -TaskName (Split-Path -Path $ScheduledTask -Leaf) | Where-Object State -Like "Disabled" | Enable-ScheduledTask Get-ScheduledTask -TaskName (Split-Path -Path $ScheduledTask -Leaf) | Where-Object State -Like "Disabled" | Enable-ScheduledTask
} }
} }
@ -62,11 +40,9 @@ function Set-ScheduledTaskState() {
} }
<# <#
Set-ScheduledTaskState -Disabled -ScheduledTasks @("ScheduledTask1", "ScheduledTask2", "ScheduledTask3") Set-ScheduledTaskState -State Disabled -ScheduledTasks @("ScheduledTask1", "ScheduledTask2", "ScheduledTask3")
Set-ScheduledTaskState -Disabled -ScheduledTasks @("ScheduledTask1", "ScheduledTask2", "ScheduledTask3") -Filter @("ScheduledTask3") Set-ScheduledTaskState -State Disabled -ScheduledTasks @("ScheduledTask1", "ScheduledTask2", "ScheduledTask3") -Filter @("ScheduledTask3")
Set-ScheduledTaskState -Disabled -ScheduledTasks @("ScheduledTask1", "ScheduledTask2", "ScheduledTask3") -Filter @("ScheduledTask3") -CustomMessage { "Setting $ScheduledTask as Disabled!"}
Set-ScheduledTaskState -Ready -ScheduledTasks @("ScheduledTask1", "ScheduledTask2", "ScheduledTask3") Set-ScheduledTaskState -State Enabled -ScheduledTasks @("ScheduledTask1", "ScheduledTask2", "ScheduledTask3")
Set-ScheduledTaskState -Ready -ScheduledTasks @("ScheduledTask1", "ScheduledTask2", "ScheduledTask3") -Filter @("ScheduledTask3") Set-ScheduledTaskState -State Enabled -ScheduledTasks @("ScheduledTask1", "ScheduledTask2", "ScheduledTask3") -Filter @("ScheduledTask3")
Set-ScheduledTaskState -Ready -ScheduledTasks @("ScheduledTask1", "ScheduledTask2", "ScheduledTask3") -Filter @("ScheduledTask3") -CustomMessage { "Setting $ScheduledTask as Ready!"}
#> #>

@ -1,21 +1,5 @@
Import-Module -DisableNameChecking $PSScriptRoot\..\"title-templates.psm1" Import-Module -DisableNameChecking $PSScriptRoot\..\"title-templates.psm1"
function Find-Service() {
[CmdletBinding()]
[OutputType([Bool])]
param (
[Parameter(Mandatory = $true)]
[String] $Service
)
If (Get-Service $Service -ErrorAction SilentlyContinue) {
return $true
} Else {
Write-Status -Types "?", $TweakType -Status "The $Service service was not found." -Warning
return $false
}
}
function Set-ServiceStartup() { function Set-ServiceStartup() {
[CmdletBinding()] [CmdletBinding()]
param ( param (
@ -33,11 +17,18 @@ function Set-ServiceStartup() {
[ScriptBlock] $CustomMessage [ScriptBlock] $CustomMessage
) )
$Script:SecurityFilterOnEnable = @("RemoteAccess", "RemoteRegistry") Begin {
$Script:TweakType = "Service" $Script:SecurityFilterOnEnable = @("RemoteAccess", "RemoteRegistry")
$Script:TweakType = "Service"
}
Process {
ForEach ($Service in $Services) {
If (!(Get-Service $Service -ErrorAction SilentlyContinue)) {
Write-Status -Types "?", $TweakType -Status "The $Service service was not found." -Warning
Continue
}
ForEach ($Service in $Services) {
If (Find-Service $Service) {
If (($Service -in $SecurityFilterOnEnable) -and (($Automatic) -or ($Manual))) { If (($Service -in $SecurityFilterOnEnable) -and (($Automatic) -or ($Manual))) {
Write-Status -Types "?", $TweakType -Status "Skipping $Service ($((Get-Service $Service).DisplayName)) to avoid a security vulnerability..." -Warning Write-Status -Types "?", $TweakType -Status "Skipping $Service ($((Get-Service $Service).DisplayName)) to avoid a security vulnerability..." -Warning
Continue Continue

@ -3,17 +3,23 @@ Import-Module -DisableNameChecking $PSScriptRoot\..\"title-templates.psm1"
function Remove-UWPAppx() { function Remove-UWPAppx() {
[CmdletBinding()] [CmdletBinding()]
param ( param (
[Parameter(Mandatory = $true)]
[Array] $AppxPackages [Array] $AppxPackages
) )
$TweakType = "UWP"
ForEach ($AppxPackage in $AppxPackages) { Begin {
If ((Get-AppxPackage -AllUsers -Name $AppxPackage) -or (Get-AppxProvisionedPackage -Online | Where-Object DisplayName -like $AppxPackage)) { $Script:TweakType = "UWP"
Write-Status -Types "-", $TweakType -Status "Trying to remove $AppxPackage from ALL users..." }
Get-AppxPackage -AllUsers -Name $AppxPackage | Remove-AppxPackage # App
Get-AppxProvisionedPackage -Online | Where-Object DisplayName -like $AppxPackage | Remove-AppxProvisionedPackage -Online -AllUsers # Payload Process {
} Else { ForEach ($AppxPackage in $AppxPackages) {
Write-Status -Types "?", $TweakType -Status "$AppxPackage was already removed or not found..." -Warning If ((Get-AppxPackage -AllUsers -Name $AppxPackage) -or (Get-AppxProvisionedPackage -Online | Where-Object DisplayName -like $AppxPackage)) {
Write-Status -Types "-", $TweakType -Status "Trying to remove $AppxPackage from ALL users..."
Get-AppxPackage -AllUsers -Name $AppxPackage | Remove-AppxPackage # App
Get-AppxProvisionedPackage -Online | Where-Object DisplayName -like $AppxPackage | Remove-AppxProvisionedPackage -Online -AllUsers # Payload
} Else {
Write-Status -Types "?", $TweakType -Status "$AppxPackage was already removed or not found..." -Warning
}
} }
} }
} }

@ -3,11 +3,11 @@ Import-Module -DisableNameChecking $PSScriptRoot\..\"title-templates.psm1"
function Set-CapabilityState() { function Set-CapabilityState() {
[CmdletBinding()] [CmdletBinding()]
param ( param (
[Parameter(Mandatory = $true)]
[Array] $Capabilities,
[Parameter(Mandatory = $true)] [Parameter(Mandatory = $true)]
[ValidateSet('Disabled', 'Enabled')] [ValidateSet('Disabled', 'Enabled')]
[String] $State [String] $State,
[Parameter(Mandatory = $true)]
[Array] $Capabilities
) )
Begin { Begin {
@ -22,12 +22,18 @@ function Set-CapabilityState() {
} }
If ($State -eq 'Disabled') { If ($State -eq 'Disabled') {
Write-Status -Types "-", $TweakType -Status "Uninstalling the $((Get-WindowsCapability -Online -Name $Capability).Name) capability..." Write-Status -Types "-", $TweakType -Status "Uninstalling the $Capability ($((Get-WindowsCapability -Online -Name $Capability).DisplayName)) capability..."
Get-WindowsCapability -Online | Where-Object Name -Like "$Capability" | Remove-WindowsCapability -Online Get-WindowsCapability -Online -Name "$Capability" | Where-Object State -eq "Installed" | Remove-WindowsCapability -Online
} ElseIf ($State -eq 'Enabled') { } ElseIf ($State -eq 'Enabled') {
Write-Status -Types "+", $TweakType -Status "Installing the $((Get-WindowsCapability -Online -Name $Capability).Name) capability..." Write-Status -Types "+", $TweakType -Status "Installing the $Capability ($((Get-WindowsCapability -Online -Name $Capability).DisplayName)) capability..."
Get-WindowsCapability -Online | Where-Object Name -Like "$Capability" | Add-WindowsCapability -Online Get-WindowsCapability -Online -Name "$Capability" | Where-Object State -eq "NotPresent" | Add-WindowsCapability -Online
} }
} }
} }
} }
<#
Set-CapabilityState -State Disabled -Capabilities "Capability*"
Set-CapabilityState -State Disabled -Capabilities @("Capability1", "Capability2")
Set-CapabilityState -State Enabled -Capabilities @("Capability1", "Capability*")
#>

@ -1,21 +1,5 @@
Import-Module -DisableNameChecking $PSScriptRoot\..\"title-templates.psm1" Import-Module -DisableNameChecking $PSScriptRoot\..\"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 -Types "?", $TweakType -Status "The $OptionalFeature optional feature was not found." -Warning
return $false
}
}
function Set-OptionalFeatureState() { function Set-OptionalFeatureState() {
[CmdletBinding()] [CmdletBinding()]
param ( param (
@ -31,11 +15,18 @@ function Set-OptionalFeatureState() {
[ScriptBlock] $CustomMessage [ScriptBlock] $CustomMessage
) )
$Script:SecurityFilterOnEnable = @("IIS-*") Begin {
$Script:TweakType = "OptionalFeature" $Script:SecurityFilterOnEnable = @("IIS-*")
$Script:TweakType = "OptionalFeature"
}
Process {
ForEach ($OptionalFeature in $OptionalFeatures) {
If (!(Get-WindowsOptionalFeature -Online -FeatureName $OptionalFeature)) {
Write-Status -Types "?", $TweakType -Status "The $OptionalFeature optional feature was not found." -Warning
Continue
}
ForEach ($OptionalFeature in $OptionalFeatures) {
If (Find-OptionalFeature $OptionalFeature) {
If (($OptionalFeature -in $SecurityFilterOnEnable) -and ($Enabled)) { If (($OptionalFeature -in $SecurityFilterOnEnable) -and ($Enabled)) {
Write-Status -Types "?", $TweakType -Status "Skipping $OptionalFeature ($((Get-WindowsOptionalFeature -Online -FeatureName $OptionalFeature).DisplayName)) to avoid a security vulnerability..." -Warning Write-Status -Types "?", $TweakType -Status "Skipping $OptionalFeature ($((Get-WindowsOptionalFeature -Online -FeatureName $OptionalFeature).DisplayName)) to avoid a security vulnerability..." -Warning
Continue Continue

@ -52,14 +52,13 @@ function Optimize-TaskScheduler() {
If ($Revert) { If ($Revert) {
Write-Status -Types "*", "TaskScheduler" -Status "Reverting the tweaks is set to '$Revert'." -Warning Write-Status -Types "*", "TaskScheduler" -Status "Reverting the tweaks is set to '$Revert'." -Warning
$CustomMessage = { "Resetting the $ScheduledTask task as 'Ready'..." } Set-ScheduledTaskState -State 'Enabled' -ScheduledTask $DisableScheduledTasks
Set-ScheduledTaskState -Ready -ScheduledTask $DisableScheduledTasks -CustomMessage $CustomMessage
} Else { } Else {
Set-ScheduledTaskState -Disabled -ScheduledTask $DisableScheduledTasks Set-ScheduledTaskState -State 'Disabled' -ScheduledTask $DisableScheduledTasks
} }
Write-Section -Text "Enabling Scheduled Tasks from Windows" Write-Section -Text "Enabling Scheduled Tasks from Windows"
Set-ScheduledTaskState -Ready -ScheduledTask $EnableScheduledTasks Set-ScheduledTaskState -State 'Enabled' -ScheduledTask $EnableScheduledTasks
} }
function Main() { function Main() {

@ -50,9 +50,6 @@ function Main() {
# List all Windows Packages: # List all Windows Packages:
#Get-WindowsPackage -Online | Select-Object -Property ReleaseType, PackageName, PackageState, InstallTime | Sort-Object ReleaseType, PackageState, PackageName | Format-Table #Get-WindowsPackage -Online | Select-Object -Property ReleaseType, PackageName, PackageState, InstallTime | Sort-Object ReleaseType, PackageState, PackageName | Format-Table
# List all Windows Capabilities:
#Get-WindowsCapability -Online | Select-Object -Property State, Name | Sort-Object State, Name | Format-Table
If (!$Revert) { If (!$Revert) {
Optimize-WindowsFeaturesList # Disable useless features and Enable features claimed as Optional on Windows, but actually, they are useful Optimize-WindowsFeaturesList # Disable useless features and Enable features claimed as Optional on Windows, but actually, they are useful
} Else { } Else {

@ -22,9 +22,9 @@ function Remove-CapabilitiesList() {
If ($Revert) { If ($Revert) {
Write-Status -Types "*", "Capability" -Status "Reverting the tweaks is set to '$Revert'." -Warning Write-Status -Types "*", "Capability" -Status "Reverting the tweaks is set to '$Revert'." -Warning
Set-CapabilityState -Capabilities $DisableCapabilities -State Enabled Set-CapabilityState -State Enabled -Capabilities $DisableCapabilities
} Else { } Else {
Set-CapabilityState -Capabilities $DisableCapabilities -State Disabled Set-CapabilityState -State Disabled -Capabilities $DisableCapabilities
} }
} }

Loading…
Cancel
Save