Created
June 3, 2020 16:02
-
-
Save bill-long/faba86520813ced97a21b2ba929aeb2d to your computer and use it in GitHub Desktop.
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
# 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