Solved

Excluding specific folder path in powershell

Posted on 2016-10-27
7
54 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
ID: 41862790
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
ID: 41862805
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
ID: 41862848
or is there a way i could import a CSV of the folder paths I want to run against?
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 69

Expert Comment

by:Qlemo
ID: 41862849
-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 69

Expert Comment

by:Qlemo
ID: 41862854
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
ID: 41862864
i could do either, which would be easier?
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 41864324
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

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.

Question has a verified solution.

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

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

810 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