Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Still a scripting noob

Posted on 2014-04-01
10
Medium Priority
?
213 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
7 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 65

Expert Comment

by:RobSampson
ID: 39971437
0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 1500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Make the most of your online learning experience.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Screencast - Getting to Know the Pipeline
Suggested Courses

564 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