#!powershell # Copyright: (c) 2019, Carson Anderson # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) #AnsibleRequires -CSharpUtil Ansible.Basic $spec = @{ options = @{ name = @{ type = "str"; required = $true } state = @{ type = "str"; default = "present"; choices = @("absent", "present") } source = @{ type = "str" } include_parent = @{ type = "bool"; default = $false } } supports_check_mode = $true } $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) $name = $module.Params.name $state = $module.Params.state $source = $module.Params.source $include_parent = $module.Params.include_parent $module.Result.reboot_required = $false if (-not (Get-Command -Name Enable-WindowsOptionalFeature -ErrorAction SilentlyContinue)) { $module.FailJson("This version of Windows does not support the Enable-WindowsOptionalFeature.") } $feature_state_start = Get-WindowsOptionalFeature -Online -FeatureName $name if (-not $feature_state_start) { $module.FailJson("Failed to find feature '$name'") } if ($state -eq "present") { # Matches for "Enabled" and "EnabledPending" if ($feature_state_start.State -notlike "Enabled*") { $install_args = @{ FeatureName = $name All = $include_parent } if ($source) { if (-not (Test-Path -LiteralPath $source)) { $module.FailJson("Path could not be found '$source'") } $install_args.Source = $source } if (-not $module.CheckMode) { $action_result = Enable-WindowsOptionalFeature -Online -NoRestart @install_args $module.Result.reboot_required = $action_result.RestartNeeded } $module.Result.changed = $true } } else { # Matches for Disabled, DisabledPending, and DisabledWithPayloadRemoved if ($feature_state_start.State -notlike "Disabled*") { $remove_args = @{ FeatureName = $name } if (-not $module.CheckMode) { $action_result = Disable-WindowsOptionalFeature -Online -NoRestart @remove_args $module.Result.reboot_required = $action_result.RestartNeeded } $module.Result.changed = $true } } $module.ExitJson()