Skip to content

Instantly share code, notes, and snippets.

@devynspencer
Last active November 29, 2016 07:47
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 devynspencer/c54eb8ca577c3fab3536dd5f8c77d6b1 to your computer and use it in GitHub Desktop.
Save devynspencer/c54eb8ca577c3fab3536dd5f8c77d6b1 to your computer and use it in GitHub Desktop.
function Repair-HomeDirectoryAcl {
Param(
# TODO: cast this as a file-object / review PowerShell idioms for working with directories
[Parameter(Mandatory, ValueFromPipeline) # TODO: , ValueFromPipelineByProperty)]
[string] $Path,
[switch] $Recurse
)
process {
try {
[System.Security.Principal.NTAccount] $User = $Path.ToString() # TODO: is .ToString() necessary?
$Acl = Get-Acl -Path $Path
$Acl.SetOwner($User)
Set-Acl -Path $Path -AclObject $Acl
if ($Recurse) {
Get-ChildItem -Path $Path -Recurse | % {
$Acl = Get-Acl -Path $_.FullName
$Acl.SetOwner($User)
Set-Acl -Path $_.FullName -AclObject $Acl
}
}
}
catch {
Write-Error "Unable to set owner"
}
}
}
function Repair-ADHomeDirectory {
param(
[Parameter(Mandatory, ValueFromPipeline)] # , ValueFromTheThingIForgot)]
[whatever.the.ad.user.object.is] $User
)
# TODO: is this extra block necessary (does GCI include the root folder or no?)
$Acl = Get-Acl -Path $User.HomeDirectory
$Acl.SetOwner($User.SamAccountName)
Set-Acl -Path $User.HomeDirectory -AclObject $Acl
Get-ChildItem -Path $User.HomeDirectory -Recurse | % {
$Acl = Get-Acl -Path $_.FullName
$Acl.SetOwner($User.SamAccountName)
Set-Acl -Path $_.FullName -AclObject $Acl
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment