Link to home
Start Free TrialLog in
Avatar of Leadtheway
LeadthewayFlag for United States of America

asked on

Excluding specific folder path in powershell

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

Avatar of Spencer Scherer
Spencer Scherer
Flag of United States of America image

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.
Avatar of Leadtheway

ASKER

I'm not sure I'm understanding.  I'm still getting used to powershell so sometimes i struggle getting fancy..lol
or is there a way i could import a CSV of the folder paths I want to run against?
Avatar of Qlemo
-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

If you want to use a positive list, does it only contain the folder name, or the path?
i could do either, which would be easier?
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial