Skip to content

Commit

Permalink
Merge pull request #11 from sbaerlocher/develop/win_capability
Browse files Browse the repository at this point in the history
add win_capability
  • Loading branch information
sbaerlocher authored Sep 11, 2020
2 parents bb8db22 + 0f91e28 commit 1802301
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ and [human-readable changelog](https://keepachangelog.com/en/1.0.0/).

## master

## 0.0.6

### Added

- Add module win_capability

## 0.0.5

### Changed
Expand Down
3 changes: 2 additions & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
---
namespace: 'sbaerlocher'
name: 'windows'
version: 0.0.5
version: 0.0.6
readme: README.md
authors:
- 'Simon Baerlocher (https://sbaerlocher.ch)'
description: 'Ansible Collection for Windows functions.'
license:
- MIT
license_file: 'LICENSE'
tags:
- windows
dependencies: {}
Expand Down
55 changes: 55 additions & 0 deletions plugins/modules/win_capability.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!powershell

# Copyright: (c) 2020, Simon Baerlocher <[email protected]>
# 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 = "list"; elements = "str"; required = $true }
state = @{ type = "str"; default = "present"; choices = @("absent", "present") }
}
supports_check_mode = $true
}

$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)

$name = $module.Params.name
$state = $module.Params.state

$module.Result.reboot_required = $false

if (-not (Get-Command -Name Get-WindowsCapability -ErrorAction SilentlyContinue)) {
$module.FailJson("This version of Windows does not support the Get-WindowsCapability.")
}


foreach ($capability_name in $name) {
try {
$capability_state_start = Get-WindowsCapability -Online -Name $capability_name
}
catch [System.Runtime.InteropServices.COMException] {
$capability_state_start = $null
}
if (-not $capability_state_start) {
$module.FailJson("Failed to find Capability '$capability_name'")
}

if ($state -eq "present" -and $capability_state_start.State -like "*NotPresent*") {
if (-not $module.CheckMode) {
Add-WindowsCapability -Online -Name $capability_name
#$module.Result.reboot_required = $action_result.RestartNeeded
}
$module.Result.changed = $true
}
elseif ($state -eq "absent" -and $capability_state_start.State -notlike "NotPresent") {
if (-not $module.CheckMode) {
$action_result = Remove-WindowsCapability -Online -Name $capability_name
$module.Result.reboot_required = $action_result.RestartNeeded
}
$module.Result.changed = $true
}
}

$module.ExitJson()

0 comments on commit 1802301

Please sign in to comment.