Solved

Still a scripting noob

Posted on 2014-04-01
10
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 65

Expert Comment

by:RobSampson
ID: 39971437
0
 
LVL 71

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

738 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