mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
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
This commit is contained in:
parent
928880c639
commit
69ac88176d
1 changed files with 19 additions and 17 deletions
|
@ -20,43 +20,45 @@
|
||||||
# POWERSHELL_COMMON
|
# POWERSHELL_COMMON
|
||||||
|
|
||||||
$params = Parse-Args $args;
|
$params = Parse-Args $args;
|
||||||
|
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
|
||||||
|
|
||||||
$result = New-Object PSObject;
|
$name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true
|
||||||
Set-Attr $result "changed" $false;
|
$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
|
$result = @{
|
||||||
|
changed = $false
|
||||||
$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'"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$description = Get-Attr $params "description" $null
|
|
||||||
|
|
||||||
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
|
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
|
||||||
$group = $adsi.Children | Where-Object {$_.SchemaClassName -eq 'group' -and $_.Name -eq $name }
|
$group = $adsi.Children | Where-Object {$_.SchemaClassName -eq 'group' -and $_.Name -eq $name }
|
||||||
|
|
||||||
try {
|
try {
|
||||||
If ($state -eq "present") {
|
If ($state -eq "present") {
|
||||||
If (-not $group) {
|
If (-not $group) {
|
||||||
$group = $adsi.Create("Group", $name)
|
If (-not $check_mode) {
|
||||||
$group.SetInfo()
|
$group = $adsi.Create("Group", $name)
|
||||||
|
$group.SetInfo()
|
||||||
|
}
|
||||||
|
|
||||||
Set-Attr $result "changed" $true
|
$result.changed = $true
|
||||||
}
|
}
|
||||||
|
|
||||||
If ($null -ne $description) {
|
If ($null -ne $description) {
|
||||||
IF (-not $group.description -or $group.description -ne $description) {
|
IF (-not $group.description -or $group.description -ne $description) {
|
||||||
$group.description = $description
|
$group.description = $description
|
||||||
$group.SetInfo()
|
If (-not $check_mode) {
|
||||||
Set-Attr $result "changed" $true
|
$group.SetInfo()
|
||||||
|
}
|
||||||
|
$result.changed = $true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ElseIf ($state -eq "absent" -and $group) {
|
ElseIf ($state -eq "absent" -and $group) {
|
||||||
$adsi.delete("Group", $group.Name.Value)
|
If (-not $check_mode) {
|
||||||
Set-Attr $result "changed" $true
|
$adsi.delete("Group", $group.Name.Value)
|
||||||
|
}
|
||||||
|
$result.changed = $true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
|
|
Loading…
Add table
Reference in a new issue