2015-10-17 17:07:39 +02:00
#!powershell
# This file is part of Ansible
#
# Copyright 2015, Hans-Joachim Kliemeck <git@kliemeck.de>
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# WANT_JSON
# POWERSHELL_COMMON
#Functions
Function UserSearch
{
2016-01-12 09:57:56 +01:00
Param ( [ string ] $accountName )
2015-10-17 17:07:39 +02:00
#Check if there's a realm specified
2016-01-12 09:57:56 +01:00
$searchDomain = $false
$searchDomainUPN = $false
if ( $accountName . Split ( " \ " ) . count -gt 1 )
2015-10-17 17:07:39 +02:00
{
2016-01-12 09:57:56 +01:00
if ( $accountName . Split ( " \ " ) [ 0 ] -ne $env:COMPUTERNAME )
2015-10-17 17:07:39 +02:00
{
2016-01-12 09:57:56 +01:00
$searchDomain = $true
$accountName = $accountName . split ( " \ " ) [ 1 ]
2015-10-17 17:07:39 +02:00
}
}
2016-01-12 09:57:56 +01:00
Elseif ( $accountName . contains ( " @ " ) )
2015-10-17 17:07:39 +02:00
{
2016-01-12 09:57:56 +01:00
$searchDomain = $true
$searchDomainUPN = $true
2015-10-17 17:07:39 +02:00
}
Else
{
#Default to local user account
2016-01-12 09:57:56 +01:00
$accountName = $env:COMPUTERNAME + " \ " + $accountName
2015-10-17 17:07:39 +02:00
}
2015-10-17 23:10:56 +02:00
2016-01-12 09:57:56 +01:00
if ( $searchDomain -eq $false )
2015-10-17 17:07:39 +02:00
{
# do not use Win32_UserAccount, because e.g. SYSTEM (BUILTIN\SYSTEM or COMPUUTERNAME\SYSTEM) will not be listed. on Win32_Account groups will be listed too
2016-01-12 09:57:56 +01:00
$localaccount = get-wmiobject -class " Win32_Account " -namespace " root\CIMV2 " -filter " (LocalAccount = True) " | where { $_ . Caption -eq $accountName }
2015-10-17 17:07:39 +02:00
if ( $localaccount )
{
return $localaccount . SID
}
}
2016-01-12 09:57:56 +01:00
Else
2015-10-17 17:07:39 +02:00
{
#Search by samaccountname
$Searcher = [ adsisearcher ] " "
2015-10-17 23:10:56 +02:00
2016-01-12 09:57:56 +01:00
If ( $searchDomainUPN -eq $false ) {
$Searcher . Filter = " sAMAccountName= $( $accountName ) "
2015-10-17 23:10:56 +02:00
}
Else {
2016-01-12 09:57:56 +01:00
$Searcher . Filter = " userPrincipalName= $( $accountName ) "
2015-10-17 23:10:56 +02:00
}
$result = $Searcher . FindOne ( )
2015-10-17 17:07:39 +02:00
if ( $result )
{
$user = $result . GetDirectoryEntry ( )
# get binary SID from AD account
$binarySID = $user . ObjectSid . Value
# convert to string SID
return ( New-Object System . Security . Principal . SecurityIdentifier ( $binarySID , 0 ) ) . Value
}
}
}
$params = Parse-Args $args ;
$result = New-Object PSObject ;
Set-Attr $result " changed " $false ;
$path = Get-Attr $params " path " -failifempty $true
$user = Get-Attr $params " user " -failifempty $true
2016-03-21 17:49:51 +01:00
$recurse = Get-Attr $params " recurse " " no " -validateSet " no " , " yes " -resultobj $result
$recurse = $recurse | ConvertTo-Bool
2015-10-17 17:07:39 +02:00
If ( -Not ( Test-Path -Path $path ) ) {
Fail-Json $result " $path file or directory does not exist on the host "
}
# Test that the user/group is resolvable on the local machine
$sid = UserSearch -AccountName ( $user )
if ( ! $sid )
{
Fail-Json $result " $user is not a valid user or group on the host machine or domain "
}
Try {
$objUser = New-Object System . Security . Principal . SecurityIdentifier ( $sid )
$file = Get-Item -Path $path
$acl = Get-Acl $file . FullName
If ( $acl . getOwner ( [ System.Security.Principal.SecurityIdentifier ] ) -ne $objUser ) {
2015-10-26 10:20:14 +01:00
$acl . setOwner ( $objUser )
Set-Acl $file . FullName $acl
2015-10-17 17:07:39 +02:00
Set-Attr $result " changed " $true ;
}
2015-10-21 21:11:51 +02:00
If ( $recurse ) {
2015-10-17 17:07:39 +02:00
$files = Get-ChildItem -Path $path -Force -Recurse
ForEach ( $file in $files ) {
$acl = Get-Acl $file . FullName
If ( $acl . getOwner ( [ System.Security.Principal.SecurityIdentifier ] ) -ne $objUser ) {
2015-10-26 10:20:14 +01:00
$acl . setOwner ( $objUser )
Set-Acl $file . FullName $acl
2015-10-17 17:07:39 +02:00
Set-Attr $result " changed " $true ;
}
}
}
}
Catch {
Fail-Json $result " an error occured when attempting to change owner on $path for $user "
}
Exit-Json $result