Local Tech Repair: Powershell get owner of folder

Friday, April 10, 2015

Powershell get owner of folder

This is a simple script that front line support can use to grab a dfs and find the security groups that have access to the folder.
*edit*
updated version of the script located here

*end edit*

After selecting the security group it will output the groups managed by user.

<# Created by http://localtechrepair.blogspot.com Date: 4/9/2015 version: .01 this script takes in dfs and then it gets the group and then exports the owners of the group #>

param (
[Parameter( Mandatory=$true)]
[string]$path

)
get-item $path | get-acl | Format-List AccessToString

$group = read-host "group name?"

$root = [ADSI]"LDAP://example.com"
$search = [adsisearcher]$root
$Search.Filter = "(&(objectCategory=group)(cn=$group))"
$colResults = $Search.FindAll()
foreach ($i in $colResults)
{
$distinguishedname = [string]$i.Properties.Item('distinguishedName')
#write-output $distinguishedname
$managedbyuser = [string]$i.Properties.Item('managedBy')
#Write-Output $managedbyuser
$Search.Filter = "(&(sAMAccountType=805306368)(distinguishedname=$managedbyuser))"
$colResults = $Search.FindAll()
foreach ($i in $colResults)
{
Write-Output $group
[string]$i.Properties.Item('givenName'),[string]$i.properties.item("sn") -join " "

[string]$i.properties.item("samaccountname")
}

}


I wrote this script because i was tired of opening up explorer window right clicking on the folder the user wanted access to and then checking the security tab to see which group gave the least amount of access for the folder. Then after finding the group having to type it out in Active directory users and computers and then opening the group up and then going to the managed by field so i could find the owner.

All this script does is allow me to mass select and paste into tickets and send out approval with 3 pastes instead of much more.

Thanks for reading and I hope you share this if you find it useful.
- Local Tech Repair Admin

No comments:

Post a Comment