Set-FSOwner.ps1 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. param(
  2. $Path,
  3. $Filter="WABTEC\*",
  4. $NewOwner=$env:USERNAME,
  5. [switch]
  6. $Set=$false,
  7. [Int]
  8. $Depth=99
  9. )
  10. $ErrorActionPreference = "stop"
  11. $NewOwnerAccount = New-Object System.Security.Principal.Ntaccount($NewOwner);
  12. Get-ChildItem -Recurse -Depth $Depth -Path $path | ForEach-Object {
  13. $currentPath = $_.FullName
  14. try
  15. {
  16. $ACL = Get-Acl $currentPath
  17. }
  18. catch
  19. {
  20. Write-Output ("Error getting permissions of: " + $currentPath)
  21. throw $_
  22. }
  23. $Owner = ($ACL).Owner
  24. $out = [PSCustomObject]@{
  25. Path=$currentPath;
  26. Owner=$Owner;
  27. NewOwner=$NewOwnerAccount;
  28. }
  29. $ACL.SetOwner($NewOwnerAccount) | Out-Null
  30. if (-not ($Owner -like $Filter)) {
  31. $out
  32. if ($Set) {
  33. try
  34. {
  35. $ACL | Set-Acl -Path $currentPath;
  36. }
  37. catch
  38. {
  39. Write-Output ("Error setting permissions of: " + $currentPath)
  40. throw $_
  41. }
  42. }
  43. }
  44. }