Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excluding specific folder path in powershell

Posted on 2016-10-27
7
Medium Priority
?
101 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 71

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 71

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 71

Accepted Solution

by:
Qlemo earned 2000 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

New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

Question has a verified solution.

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

There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

885 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