Question

This is the code I have at the moment:

$OutFile = "C:\Permissions.csv"
$Header = "Folder Path,IdentityReference,AccessControlType,IsInherited"
Del $OutFile
Add-Content -Value $Header -Path $OutFile 

$RootPath = "C:\Test Folder"
$Folders = dir $RootPath -recurse | where {$_.psiscontainer -eq $true} 

foreach ($Folder in $Folders){
$ACLs = get-acl $Folder.fullname | 
ForEach-Object { $_.Access  } | 
Where {$_.IdentityReference -notlike "*BUILTIN*" -and $_.IdentityReference -notlike "*NT AUTHORITY*"}
Foreach ($ACL in $ACLs){
$OutInfo = $Folder.Fullname + "," + $ACL.IdentityReference  + "," + $ACL.AccessControlType + "," + $ACL.IsInherited
Add-Content -Value $OutInfo -Path $OutFile
}}

I have setup some test folders in this file path, one of which is beyond the 260 character limit of Powershell to delve into.

When I run this code, PS chucks back an error saying that the path is too long, and then it displays a condensed version of the file path that has the problem. Is there a way to get that path out, so that I can run the code again with the long path in the $RootPath object - so that it can go deeper?

Was it helpful?

Solution

To get the short path name you can use Get-ShortPath from PowerShell Community Extensions (PSCX)

Here is one of my old version (I imagine less powerfull)

# Get the 8.3 short path of a file comming from the pipe (fileInfo or directoryInfo) or from a parameter (string)
Function Get-MyShortPath
{
  [CmdletBinding()]
  PARAM ([Parameter(mandatory=$true,ValueFromPipeline=$true)]
         $file)

  BEGIN
  {
    $fso = New-Object -ComObject Scripting.FileSystemObject
  }

  PROCESS
  {
    if ($file -is [String])
    {
      $file = Get-Item $file
    }

    If ($file.psiscontainer)
    {
      $fso.getfolder($file.fullname).shortPath
    }
    Else 
    {
      $fso.getfile($file.fullname).shortPath
    }
  }
}

usage :

  gci 'C:\Program Files (x86)' | Get-MyShortPath
  Get-MyShortPath 'C:\Program Files'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top