Solved

Still a scripting noob

Posted on 2014-04-01
10
202 Views
Last Modified: 2014-07-09
Looking for help from the group. I have this script that does a great job of returning data using get-acl. The manager I gave the output to wants me to add aduser information like company, department etc... so that the resulting list of users might be easier to find. I'm a scripting noob although I've been trying to teach myself for years.

 This script creates a csv file listing top level folder permissions in a folder.

 $OutFile = "c:\results.csv"
 $Header = "Folder Path,IdentityReference,AccessControlType,IsInherited,FileSystemRights"
 Del $OutFile
 Add-Content -Value $Header -Path $OutFile

 $maxdepth = 1
 $RootPath = "E:\shared\daily collection"
 $actual_depth_param = [int]([regex]::Matches($RootPath, "\\")).count + [int]$maxdepth + 1
 $Folders = dir $RootPath -recurse | where {$_.psiscontainer -eq $true}

 foreach ($Folder in $Folders){
 if (([regex]::Matches($Folder.fullname, "\\")).count -lt $actual_depth_param) {
 $ACLs = get-acl $Folder.fullname | ForEach-Object { $_.Access }
 Foreach ($ACL in $ACLs){
 $OutInfo = $Folder.Fullname + "," + $ACL.IdentityReference + "," + $ACL.AccessControlType + "," + $ACL.IsInherited+ "," + $ACL.FileSystemRights
 Add-Content -Value $OutInfo -Path $OutFile
 }}}


 $acl.AccessControlType


 Thanks in advance for your help.

Boston49
0
Comment
Question by:Boston49
  • 4
  • 2
10 Comments
 

Author Comment

by:Boston49
ID: 39969751
A bit more explanation if you need it. For each IdentityReference returned, I'd like to also get acluser information like Company and Description as well. Thanks again.
0
 

Author Comment

by:Boston49
ID: 39970559
Thank you yes this is a powershell script. Missed that.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39971436
Hi, I'm not that big on Powershell myself, but this article shoud give you some idea on which methods you need to use, based on your IdentityReference.

If you need me to I might be able to test a bit later on.

Rob
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 65

Expert Comment

by:RobSampson
ID: 39971437
0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 39971659
Good morning,

This is untested, no Active Directory here for me to contemplate testing it against :)

It's commented throughout, but this is a good bit more complex than your original.

# You need RSAT installed to be able to import this. 
# There are alternatives if this doesn't work for you.
Import-Module ActiveDirectory

$RootPath = "E:\shared\daily collection"
$OutFile = "c:\results.csv"

# Killing the -recurse flag, slows us down.
# Storing all of this in a variable so we can add to the output afterwards
$AccessReport = Get-ChildItem $RootPath | Where-Object { $_.PsIsContainer } | ForEach-Object {

  # Drop down into th esecond level of folders
  Get-ChildItem $_.FullName | Where-Object { $_.PsIsContainer } | ForEach-Object {

    # Hold this for a moment so we can construct and output object
    $FullName = $_.FullName

    # Build the output object based on the Access property from Get-Acl
    # @{Name=...;Expression={ ... }} describes a custom property we're adding to the output.
    Get-Acl $FullName | Select-Object -ExpandProperty Access | Select-Object`
      @{Name='Folder Path';Expression={ $FullName }},
      IdentityReference,
      AccessControlType,
      IsInherited,
      FileSystemRights
  }
}

# Users may appear more than once in a report, if we first pick out a unique list of users we can
# minimise the work we must do against Active Directory. That's a very good thing.
$IdentityList = $AccessReport | 
  Where-Object { $_.IdentityReference.Value -notmatch '^(BUILTIN|NT AUTHORITY)' } |
  ForEach-Object { $_.IdentityReference.Value -replace '^.+\\' } |
  Select-Object -Unique

# A hashtable (key / value list) will be used to store the results we get from AD
$ADObjects = @{}
$IdentityList | ForEach-Object {
  $ADObject = Get-ADUser -LdapFilter "(sAMAccountName=$_)" -Properties Company, Department, Description

  # Test to see if we found anything
  if ($ADObject) {
    # Store what we found in the hashtable. We'll use this again in a minute.    
    $ADObjects.Add($_, ($ADObject | Select-Object Company, Department, Description))
  }
}

# Time to combine the results
$AccessReport | ForEach-Object {
  $Identity = $_.IdentityReference.Value -replace '^.+\\'

  # See if we found a result for this above
  if ($ADObjects.Contains($Identity)) {
    # Add the new fields to the original object we created for the report
    $_ | Select-Object *,
      @{n='Company';e={ $ADObjects[$Identity].Company }},
      @{n='Department';e={ $ADObjects[$Identity.Department }},
      @{n='Description';e={ $ADObjects[$Identity.Description }} 
  }

# Finally, it's time to drop it all back to a file.
} | Export-Csv $OutFile -NoTypeInformation

Open in new window


Please yell if you have questions about the snippet above.

Cheers,

Chris
0
 

Author Comment

by:Boston49
ID: 39986175
Agreed. I had tried to do that earlier, just realized how to... Thanks.
0
 

Author Closing Comment

by:Boston49
ID: 40186078
This answer got me on the right track. I now have added the required information to my report. Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
A procedure for exporting installed hotfix details of remote computers using powershell
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now