Solved

Excluding specific folder path in powershell

Posted on 2016-10-27
7
69 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

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 70

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 70

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

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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