Solved

Excluding specific folder path in powershell

Posted on 2016-10-27
7
28 Views
Last Modified: 2016-10-28
So i have this script that recursively pulls all permissions off of every folder and every drive.  It works well and gets the info i need, but I was wanting to exclude several folders as to speed up the process. Is there an easy mod to this script that will help that.?

$ErrorActionPreference = "Continue" 
$strComputer = $env:ComputerName 
$colDrives = Get-PSDrive -PSProvider Filesystem 
ForEach ($DriveLetter in $colDrives) { 
    $StartPath = "$DriveLetter`:\" 
    Get-ChildItem -LiteralPath $StartPath -Recurse | 
    ForEach { 
      $FullPath = Get-Item -LiteralPath (Get-Item -LiteralPath $_.PSPath) 
      (Get-Item -LiteralPath $FullPath).GetAccessControl() | 
      Select * -Expand Access | 
      Select @{N='Server Name';E={$strComputer}}, 
             @{N='Full Path';E={$FullPath}}, 
             @{N='Type';E={If($FullPath.PSIsContainer -eq $True) {'D'} Else {'F'}}}, 
             @{N='Owner';E={$_.Owner}}, 
             @{N='Trustee';E={$_.IdentityReference}}, 
             @{N='Inherited';E={$_.IsInherited}}, 
             @{N='Inheritance Flags';E={$_.InheritanceFlags}}, 
             @{N='Ace Flags';E={$_.PropagationFlags}}, 
             @{N='Ace Type';E={$_.AccessControlType}}, 
             @{N='Access Masks';E={$_.FileSystemRights}} } | 
      Export-CSV -NoTypeInformation -Delimiter "|" –Path "$strComputer`_$DriveLetter.csv" }

Open in new window

0
Comment
Question by:leadtheway
  • 3
  • 3
7 Comments
 
LVL 3

Expert Comment

by:Spencer Scherer
Comment Utility
You could do an if/else inside the foreach.  If ($fullpath -eq "path") {do nothing} else { run your script }.  

You can either define each "path" to exclude individually with if statements or you can make an array that you'd like to exclude.
0
 
LVL 1

Author Comment

by:leadtheway
Comment Utility
I'm not sure I'm understanding.  I'm still getting used to powershell so sometimes i struggle getting fancy..lol
0
 
LVL 1

Author Comment

by:leadtheway
Comment Utility
or is there a way i could import a CSV of the folder paths I want to run against?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
-Exclude for Get-ChildItem should do exactly that - exclude partitial matches of the path name.
This
      $FullPath = Get-Item -LiteralPath (Get-Item -LiteralPath $_.PSPath) 
      (Get-Item -LiteralPath $FullPath).GetAccessControl() | 

Open in new window

is much too much. $_ already contains the item, so it should just be
      ($_.GetAccessControl() | 

Open in new window

0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
If you want to use a positive list, does it only contain the folder name, or the path?
0
 
LVL 1

Author Comment

by:leadtheway
Comment Utility
i could do either, which would be easier?
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
Complete paths are easier.
$ErrorActionPreference = "Continue" 
$strComputer = $env:ComputerName 

Get-ChildItem -Recurse -LiteralPath (Get-Content C:\Temp\EE\Folders.txt) |
  % {
    $_.GetAccessControl() | 
    Select * -Expand Access | 
    Select @{N='Server Name'      ; E={$strComputer}}, 
           @{N='Full Path'        ; E={$_.FullPath}}, 
           @{N='Type'             ; E={'FD'[$_.PSIsContainer]}}, 
           Owner, 
           @{N='Trustee'          ; E={$_.IdentityReference}}, 
           @{N='Inherited'        ; E={$_.IsInherited}}, 
           @{N='Inheritance Flags'; E={$_.InheritanceFlags}}, 
           @{N='Ace Flags'        ; E={$_.PropagationFlags}}, 
           @{N='Ace Type'         ; E={$_.AccessControlType}}, 
           @{N='Access Masks'     ; E={$_.FileSystemRights}}
  } | 
  Export-CSV -NoTypeInformation -Delimiter "|" "$strComputer.csv"
 

Open in new window

The input file is a simple text file (not CSV - contains only the full paths).
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Synchronize a new Active Directory domain with an existing Office 365 tenant
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

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

12 Experts available now in Live!

Get 1:1 Help Now