Skip to content

Commit

Permalink
Update WindowsOptionalFeatures.ps1
Browse files Browse the repository at this point in the history
  • Loading branch information
nhtkid authored Aug 12, 2024
1 parent c9b2889 commit acd4ab7
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions WindowsOptionalFeatures.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@
Manages Windows optional features and configures the MsKeyboardFilter service.
Use @() for $featuresToEnable or $featuresToDisable if no changes are needed.
#>

# Define features to enable and disable
$featuresToEnable = @("Client-KeyboardFilter", "Client-EmbeddedLogon")
$featuresToDisable = @("Internet-Explorer-Optional-amd64", "WindowsMediaPlayer")

# Set log file path and initialize changes tracker
$logPath = "C:\Windows\Logs\WindowsOptionalFeatures.log"
$logPath = "C:\Windows\Logs\WindowsFeatureManagement.log"
$changesApplied = $false

# Function to write log messages
function Write-Log($Message) {
$logMessage = "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] $Message"
Add-Content -Path $logPath -Value $logMessage
Write-Host $logMessage
}

# Function to manage Windows features (enable or disable)
function Manage-WindowsFeatures($features, $action) {
function Set-WindowsFeatures($features, $action) {
foreach ($feature in $features) {
# Check current state of the feature
$state = (Get-WindowsOptionalFeature -Online -FeatureName $feature).State
Expand All @@ -32,16 +28,15 @@ function Manage-WindowsFeatures($features, $action) {
$script:changesApplied = $true
# Configure MsKeyboardFilter service if enabling Client-KeyboardFilter
if ($feature -eq 'Client-KeyboardFilter' -and $action -eq 'Enable') {
Configure-MsKeyboardFilterService
Set-MsKeyboardFilterService
}
} else {
Write-Log "Feature $feature is already $($action.ToLower())d. Skipping."
}
}
}

# Function to configure MsKeyboardFilter service
function Configure-MsKeyboardFilterService {
function Set-MsKeyboardFilterService {
try {
# Set service to Automatic startup and start it
Set-Service -Name "MsKeyboardFilter" -StartupType Automatic
Expand All @@ -51,16 +46,12 @@ function Configure-MsKeyboardFilterService {
Write-Log "Error configuring MsKeyboardFilter service: $_"
}
}

# Main script execution
Write-Log "Script execution started"

# Enable specified features
Manage-WindowsFeatures $featuresToEnable 'Enable'

Set-WindowsFeatures $featuresToEnable 'Enable'
# Disable specified features
Manage-WindowsFeatures $featuresToDisable 'Disable'

Set-WindowsFeatures $featuresToDisable 'Disable'
# Reboot if changes were applied
if ($changesApplied) {
Write-Log "Changes applied. Rebooting in 10 seconds..."
Expand All @@ -69,5 +60,4 @@ if ($changesApplied) {
} else {
Write-Log "No changes applied. No reboot necessary."
}

Write-Log "Script execution completed"

0 comments on commit acd4ab7

Please sign in to comment.