From 69ac88176d441eeff2649d7cd7bc91f60219ecd4 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Fri, 24 Feb 2017 08:34:33 +0100 Subject: [PATCH] win_group: Clean up and check-mode support (#21384) * win_group: Clean up and check-mode support Changes include: - Use Get-AnsibleParam with -type/-validateset support - Replace $result PSObject with normal hash - Add check-mode support * Revert to original formatting --- lib/ansible/modules/windows/win_group.ps1 | 36 ++++++++++++----------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/lib/ansible/modules/windows/win_group.ps1 b/lib/ansible/modules/windows/win_group.ps1 index c3fc920c916..24f20103def 100644 --- a/lib/ansible/modules/windows/win_group.ps1 +++ b/lib/ansible/modules/windows/win_group.ps1 @@ -20,43 +20,45 @@ # POWERSHELL_COMMON $params = Parse-Args $args; +$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false -$result = New-Object PSObject; -Set-Attr $result "changed" $false; +$name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true +$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "present","absent" +$description = Get-AnsibleParam -obj $params -name "description" -type "str" -$name = Get-Attr $params "name" -failifempty $true - -$state = Get-Attr $params "state" "present" -$state = $state.ToString().ToLower() -If (($state -ne "present") -and ($state -ne "absent")) { - Fail-Json $result "state is '$state'; must be 'present' or 'absent'" +$result = @{ + changed = $false } -$description = Get-Attr $params "description" $null - $adsi = [ADSI]"WinNT://$env:COMPUTERNAME" $group = $adsi.Children | Where-Object {$_.SchemaClassName -eq 'group' -and $_.Name -eq $name } try { If ($state -eq "present") { If (-not $group) { - $group = $adsi.Create("Group", $name) - $group.SetInfo() + If (-not $check_mode) { + $group = $adsi.Create("Group", $name) + $group.SetInfo() + } - Set-Attr $result "changed" $true + $result.changed = $true } If ($null -ne $description) { IF (-not $group.description -or $group.description -ne $description) { $group.description = $description - $group.SetInfo() - Set-Attr $result "changed" $true + If (-not $check_mode) { + $group.SetInfo() + } + $result.changed = $true } } } ElseIf ($state -eq "absent" -and $group) { - $adsi.delete("Group", $group.Name.Value) - Set-Attr $result "changed" $true + If (-not $check_mode) { + $adsi.delete("Group", $group.Name.Value) + } + $result.changed = $true } } catch {