Created
June 3, 2020 16:02
-
-
Save bill-long/e6216a7a25b1d6982c18016cb614ce7d 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
# Add-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 "Adding SELF on $($user.Path)" | |
$mbxSd = $user.Properties["msExchMailboxSecurityDescriptor"][0] | |
$sd = New-Object System.Security.AccessControl.RawSecurityDescriptor([byte[]]$mbxSd, 0) | |
$sddl = $sd.GetSddlForm("All") | |
if ($sddl -match "\(A;.+PS?\)") | |
{ | |
Write-Host "SELF already present." | |
return | |
} | |
Write-Host "Before:" | |
Write-Host $sddl | |
$firstParen = $sddl.IndexOf("(") | |
$newSddl = "" | |
if ($firstParen -lt 0) { | |
$newSddl = $sddl + "(A;CI;CCRC;;;PS)" | |
} else { | |
$newSddl = $sddl.Insert($firstParen - 1, "(A;CI;CCRC;;;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