Skip to content

Instantly share code, notes, and snippets.

@bill-long
Created June 3, 2020 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bill-long/faba86520813ced97a21b2ba929aeb2d to your computer and use it in GitHub Desktop.
Save bill-long/faba86520813ced97a21b2ba929aeb2d to your computer and use it in GitHub Desktop.
# Remove-SelfPermission.ps1
param($alias)
$searcher = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().FindGlobalCatalog().GetDirectorySearcher()
$searcher.Filter = "(mailnickname=$alias)"
$user = $searcher.FindOne()
if ($null -eq $user)
{
Write-Host "User not found."
return
}
Write-Host "Removing SELF on $($user.Path)"
$mbxSd = $user.Properties["msExchMailboxSecurityDescriptor"][0]
$sd = New-Object System.Security.AccessControl.RawSecurityDescriptor([byte[]]$mbxSd, 0)
$sddl = $sd.GetSddlForm("All")
Write-Host "Before:"
Write-Host $sddl
$newSddl = $sddl -replace "\(A;.+PS?\)", ""
Write-Host "After:"
Write-Host $newSddl
$newSd = New-Object -TypeName System.Security.AccessControl.RawSecurityDescriptor -ArgumentList $newSddl
$user = [ADSI]("LDAP://" + ($user.Properties["distinguishedName"][0]))
$user.Properties["msExchMailboxSecurityDescriptor"].Clear()
[byte[]]$mbxSdBytes = [System.Array]::CreateInstance([System.Byte], $newSd.BinaryLength)
$newSd.GetBinaryForm($mbxSdBytes, 0)
$user.Properties["msExchMailboxSecurityDescriptor"].Add($mbxSdBytes) | Out-Null
$user.CommitChanges()
Write-Host "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment