Skip to content

Instantly share code, notes, and snippets.

@hsupu
Created July 1, 2022 11:19
Show Gist options
  • Save hsupu/c70b9c1d332269bc44a30140175c8126 to your computer and use it in GitHub Desktop.
Save hsupu/c70b9c1d332269bc44a30140175c8126 to your computer and use it in GitHub Desktop.
I'm angry!
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)
@hsupu
Copy link
Author

hsupu commented Jul 1, 2022

Alternative: Add a new local group, add AAD user into the group, apply perms on the group.

net localgroup GroupName /add
net localgroup GroupName Domain\Name /add

From https://superuser.com/questions/1016528/how-to-give-file-permissions-to-azuread-user-on-windows-10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment