1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

win_owner: Clean up and check-mode support (#21385)

* win_owner: Clean up and check-mode support

Changes include:
- Use Get-AnsibleParam with -type/-validateset
- Replace $result PSObject with normal hash
- Add check-mode support

* Implemented -WhatIf:$check_mode support

* Revert to original formatting
This commit is contained in:
Dag Wieers 2017-02-24 08:33:35 +01:00 committed by Matt Davis
parent 313a9d3845
commit 928880c639
2 changed files with 15 additions and 17 deletions

View file

@ -67,7 +67,7 @@ Function UserSearch
$Searcher.Filter = "userPrincipalName=$($accountName)" $Searcher.Filter = "userPrincipalName=$($accountName)"
} }
$result = $Searcher.FindOne() $result = $Searcher.FindOne()
if ($result) if ($result)
{ {
$user = $result.GetDirectoryEntry() $user = $result.GetDirectoryEntry()
@ -80,16 +80,17 @@ Function UserSearch
} }
} }
} }
$params = Parse-Args $args;
$result = New-Object PSObject; $result = @{
Set-Attr $result "changed" $false; changed = $false
}
$path = Get-Attr $params "path" -failifempty $true $params = Parse-Args $args -supports_check_mode $true
$user = Get-Attr $params "user" -failifempty $true $check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
$recurse = Get-Attr $params "recurse" "no" -validateSet "no","yes" -resultobj $result
$recurse = $recurse | ConvertTo-Bool $path = Get-AnsibleParam -obj $params -name "path" -type "path" -failifempty $true
$user = Get-AnsibleParam -obj $params -name "user" -type "str" -failifempty $true
$recurse = Get-AnsibleParam -obj $params -name "recurse" -type "bool" -default "no" -validateset "no","yes" -resultobj $result
If (-Not (Test-Path -Path $path)) { If (-Not (Test-Path -Path $path)) {
Fail-Json $result "$path file or directory does not exist on the host" Fail-Json $result "$path file or directory does not exist on the host"
@ -97,7 +98,7 @@ If (-Not (Test-Path -Path $path)) {
# Test that the user/group is resolvable on the local machine # Test that the user/group is resolvable on the local machine
$sid = UserSearch -AccountName ($user) $sid = UserSearch -AccountName ($user)
if (!$sid) if (-not $sid)
{ {
Fail-Json $result "$user is not a valid user or group on the host machine or domain" Fail-Json $result "$user is not a valid user or group on the host machine or domain"
} }
@ -110,9 +111,8 @@ Try {
If ($acl.getOwner([System.Security.Principal.SecurityIdentifier]) -ne $objUser) { If ($acl.getOwner([System.Security.Principal.SecurityIdentifier]) -ne $objUser) {
$acl.setOwner($objUser) $acl.setOwner($objUser)
Set-Acl $file.FullName $acl Set-Acl -Path $file.FullName -AclObject $acl -WhatIf:$check_mode
$result.changed = $true
Set-Attr $result "changed" $true;
} }
If ($recurse) { If ($recurse) {
@ -122,9 +122,8 @@ Try {
If ($acl.getOwner([System.Security.Principal.SecurityIdentifier]) -ne $objUser) { If ($acl.getOwner([System.Security.Principal.SecurityIdentifier]) -ne $objUser) {
$acl.setOwner($objUser) $acl.setOwner($objUser)
Set-Acl $file.FullName $acl Set-Acl -Path $file.FullName -AclObject $acl -WhatIf:$check_mode
$result.changed = $true
Set-Attr $result "changed" $true;
} }
} }
} }

View file

@ -44,7 +44,6 @@ options:
recurse: recurse:
description: description:
- Indicates if the owner should be changed recursively - Indicates if the owner should be changed recursively
required: false
choices: choices:
- no - no
- yes - yes