Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell how to get acls from files/directories recursively also including "[" or "]"

I have the script below working for most of the files and folders but is not working for files and folders with "[" "]"

#Set variables
$path =  $args[0]
$filename = $args[1]
$date = Get-Date

#Place Headers on out-put file
$list = "Permissions for directories in: $Path"
$list | format-table | Out-File "C:\Powershell\Results\$filename"
$datelist = "Report Run Time: $date"
$datelist | format-table | Out-File -append "C:\Powershell\Results\$filename"
$spacelist = " "
$spacelist | format-table | Out-File -append "C:\Powershell\Results\$filename"

#Populate Folders Array
[Array] $folders = Get-ChildItem -path $path -force -recurse 

#Process data in array
ForEach ($folder in [Array] $folders)
{
#Convert Powershell Provider Folder Path to standard folder path
$PSPath = (Convert-Path $folder.pspath)
$list = ("Path: $PSPath")
$list | format-table | Out-File -append "C:\Powershell\Results\$filename"

Get-Acl -path $PSPath | Format-List -property AccessToString | Out-File -append "C:\Powershell\Results\$filename"

"-----------------------" | Out-File -FilePath "C:\Powershell\Results\$filename" -Append

} #end ForEach  
like image 298
Eduardo Avatar asked Dec 11 '22 09:12

Eduardo


2 Answers

I'm not sure this does 100% the same thing but I'm using the following one-liner

get-childitem "C:\windows\temp" -recurse | get-acl | Format-List | Out-File "c:\temp\output.txt"
like image 154
Peter Avatar answered Dec 28 '22 06:12

Peter


The problem is with the Convert-Path cmdlet trying to "interpret" the path including the square brackets which it "interprets" as wildcard characters. Instead you want to have it use the literal path.

Change this line:

$PSPath = (Convert-Path $folder.pspath)

To this:

$PSPath = (Convert-Path -LiteralPath $folder.pspath)

Also, change the Get-Acl -path to Get-Acl -LiteralPath so it looks like this:

Get-Acl -LiteralPath $PSPath | Format-List -property AccessToString | Out-File -append "C:\Powershell\Results\$filename"

If you don't have PowerShell Version 3.0 (where Get-Acl added -LiteralPath support), you can use Get-Item as a workaround:

$item = Get-Item -LiteralPath $PSPath
$item.GetAccessControl() | Format-List -property AccessToString | Out-File -append "C:\Powershell\Results\$filename"

For more information see this article: LiteralPaths

like image 37
HAL9256 Avatar answered Dec 28 '22 08:12

HAL9256