Solved

Excluding specific folder path in powershell

Posted on 2016-10-27
7
40 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
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 68

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 68

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 68

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

Suggested Solutions

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

23 Experts available now in Live!

Get 1:1 Help Now