Created
July 1, 2022 11:19
-
-
Save hsupu/c70b9c1d332269bc44a30140175c8126 to your computer and use it in GitHub Desktop.
I'm angry!
This file contains hidden or 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
param( | |
[string]$RootDir, | |
[switch]$DryRun | |
) | |
# $currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent() | |
# $currentUserName = $currentUser.Name | |
# $currentUserSID = $currentUser.User.Value | |
$currentUserName = $env:USER | |
# $currentUserSID = & whoami /user # need to extract the value manually! | |
function Scan-Dir() { | |
param( | |
[IO.DirectoryInfo]$Dir | |
) | |
$items = Get-ChildItem $Dir | |
foreach ($item in $items) { | |
$acl = [IO.FileSystemAclExtensions]::GetAccessControl($item) | |
$owner = $acl.Owner | |
if ($owner -ieq $currentUserName) { | |
# Write-Host "Already $owner : $($item.FullName)" | |
continue | |
} | |
else { | |
Write-Host "Taking from $owner : $($item.FullName)" | |
if (-not $DryRun) { | |
& icacls.exe $item.FullName /setowner $currentUserName | |
& icacls.exe $item.FullName /grant "$currentUserName:F" | |
} | |
} | |
if ($item.Attributes.HasFlag([IO.FileAttributes]::Directory)) { | |
Scan-Dir -Dir $item | |
} | |
} | |
} | |
Scan-Dir -Dir (Get-Item $RootDir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alternative: Add a new local group, add AAD user into the group, apply perms on the group.
From https://superuser.com/questions/1016528/how-to-give-file-permissions-to-azuread-user-on-windows-10