diff --git a/WindowsOptionalFeatures.ps1 b/WindowsOptionalFeatures.ps1 index 9823340..4694686 100644 --- a/WindowsOptionalFeatures.ps1 +++ b/WindowsOptionalFeatures.ps1 @@ -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 @@ -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 @@ -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..." @@ -69,5 +60,4 @@ if ($changesApplied) { } else { Write-Log "No changes applied. No reboot necessary." } - Write-Log "Script execution completed"