Created
April 10, 2020 22:02
-
-
Save raulunzue/53e2a92429ee9b7e904e91d175cd38b4 to your computer and use it in GitHub Desktop.
Script Powershell para cambiar owner carpetas y archivos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
########################################### | |
####### RAUL UNZUE - ELBLOGDENEGU ######### | |
########################################### | |
########## Script cambio owner ############ | |
########################################### | |
# https://www.maquinasvirtuales.eu/script-powershell-para-cambiar-owner-carpetas-y-archivos/ | |
# EMPEZAMOS LOG: | |
Start-Transcript ("c:\temp\logs\OWNER-ScriptLog{0:yyyyMMdd-HHmm}.txt" -f (Get-Date)) | |
# SHARES PERFILES | |
$origen = "\\SHARE\perfiles_citrix\OLD" | |
$destino = "\\SHARE\perfiles_citrix\NEW" | |
# CARGA PERFILES | |
#Si queremos una cadena de perfiles: | |
#$profiles = 'elblogdenegu','raulunzue','negu','pepe' | |
#Solamente un perfil: | |
$profiles = 'elblogdenegu' | |
# BUCLE | |
foreach ($profile in $profiles) | |
{ | |
#$p = $profile.Name | |
$p = $profile | |
# USUARIO A PROCESAR | |
Write-Output "Comenzamos con el usuario: $p" | |
# COPIA DE SEGURIDAD OWNER | |
Write-Output "Backup ACLs para usuario: $p" | |
icacls "$origen\$p" /save C:\temp\Backup\ACL_Backup_$p.txt > C:\temp\Logs\ACL_Backup_$p.log | |
# RESETEAR PERMISOS | |
Write-Output "Resetear permisos para usuario: $p" | |
icacls "$origen\$p" /T /Q /C /RESET | |
# CAMBIAR OWNER A NUESTRO SYSADMIN (USUARIO QUE EJECUTA EL SCRIPT) | |
Write-Output "Modificamos Owner carpeta: $p" | |
takeown.exe /f $origen\$p /r /d S | |
# AGREGAR PERMISOS FULL CONTROL ADMINS DEL DOMINIO | |
Write-Output "Full Control admins en carpeta: $p" | |
icacls.exe "$origen\$p" /grant:r "NEGU\Admins. del dominio:(OI)(F)" /T | |
# COPIAR FICHEROS PERFIL | |
Copy-Item –Path "$origen\$p" -Recurse –Destination $destino | |
# MOVER PERFIL | |
# Move-Item –Path "$origen\$p" –Destination $destino | |
# VOLVER PERMISOS OWNER ORIGINAL | |
Write-Output "Restauramos OWNER para carpeta: $p" | |
# Si dejamos el perfil original porque copiamos DESCOMENTAR si movemos COMENTAR | |
# icacls.exe "$origen\$p" /setowner "NEGU\$p" /T /C /L /Q | |
icacls.exe "$destino\$p" /setowner "NEGU\$p" /T /C /L /Q | |
# RESTAURAR PERMISOS ACLs | |
Write-Output "Restauramos ACLs para usuario: $p" | |
# Si dejamos el perfil original porque copiamos DESCOMENTAR si movemos COMENTAR | |
# icacls.exe "$origen" /restore C:\temp\Backup\ACL_Backup_$p.txt | |
icacls.exe "$destino" /restore C:\temp\Backup\ACL_Backup_$p.txt | |
# VERIFICAR | |
# icacls "$destino\$p" /verify /T | |
# LOG | |
Add-Content c:\temp\Logs\OWNER-Perfiles-Procesados.log "$p" | |
} | |
# Paramos log | |
Stop-Transcript |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment