question

Kaleemullahbilal-2236 avatar image
0 Votes"
Kaleemullahbilal-2236 asked IanXue-MSFT commented

Exports userwise prmission using Powershell

Hi,

I am using below procedure to Export the Permissions from the File Server 2012.

This Command Works but create a Empty CSV File and Says that there is Permission issue..

$FolderPath = dir -Directory -Path "\\fs1\Shared" -Recurse -Force
$Report = @()
Foreach ($Folder in $FolderPath) {
$Acl = Get-Acl -Path $Folder.FullName
foreach ($Access in $acl.Access)
{
$Properties = [ordered]@{'FolderName'=$Folder.FullName;'AD
Group or
User'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited}
$Report += New-Object -TypeName PSObject -Property $Properties
}
}
$Report | Export-Csv -path "C:\data\FolderPermissions.csv"

windows-serverwindows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

StoyanChalakov avatar image
1 Vote"
StoyanChalakov answered StoyanChalakov edited

Hi @Kaleemullahbilal-2236,

I have just tested your script and it works just fine with me. The .csv contains all the ACLs I needed.
So I would assume that you just have no "Read" rights on the path, which you want to export.
Please test with another path, where you have at least read permissions and it should work.

If for another reason you still face issues, can you please post also the permissions error you get when you try to export the values?
Can you please also check if the "$Report" variable contains all the permissions?

Regards,
Stoyan

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered Kaleemullahbilal-2236 commented

Hi,
The script works for me. Do you have permissions to the path you try to access? If yes, please check the return value of Get-Acl -Path $Folder.FullName. Also try -LiteraPath instead of -Path as there may be a wildcard character like square bracket in directory names.

Best Regards,
Ian

============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,

Actually am new to Power shell Scripting ..yes i have tried by setting the permission of the Path am accessing ..what do you mean by this can you please elaborate how can i use this command.....Get-Acl -Path $Folder.FullName

0 Votes 0 ·
Kaleemullahbilal-2236 avatar image
0 Votes"
Kaleemullahbilal-2236 answered Kaleemullahbilal-2236 rolled back

Below is the Error am getting...Shared Folder has many Subfolders also..i am trying to run the command using Domain Admin..and permssions are set from and to the path am accessing..


dir : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must
be less than 248 characters.
At line:1 char:15
+ $FolderPath = dir -Directory -Path "\\neurodc.nsh.com\\Shared Folders" -Recurse ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ReadError: (\\neurodc.nsh.c..._09_2012_202127:String) [Get-ChildItem], PathTooLongException
+ FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand


dir : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must
be less than 248 characters.
At line:1 char:15
+ $FolderPath = dir -Directory -Path "\\neurodc.nsh.com\\Shared Folders" -Recurse ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ReadError: (\\neurodc.nsh.c..._09_2012_202752:String) [Get-ChildItem], PathTooLongException
+ FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

StoyanChalakov avatar image
0 Votes"
StoyanChalakov answered

Hi @Kaleemullahbilal-2236,

it is clear now. The error you get does not relate to missing permissions, but is actually due to a know limitation in the Windows API when you work with longer paths.
Unfortunately there is no clear guide on how to solve this, but there are a few tricks to overcome this limitation. I have selected two working examples for you:

List All Files Regardless of 260 Character Path Restriction Using PowerShell and Robocopy

and also

PowerShell: Increase Default Windows 260-Character Paths Limit

If you do a research in Internet you will most probably find also other solutions. So you will have to re-write your script to be able to do that.

Hope I was able to help.


(If the reply was helpful please don't forget to upvote or accept as answer, thank you)
Best regards,
Stoyan


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered IanXue-MSFT commented

Hi,
According to the error message, the full path is too long. The maximum length of a path defined in Windows APIs is 260 characters. You could use a prefix "\\?\UNC\" instead of "\\" in the UNC path to call the Unicode version of the Windows APIs. The first line of the script could be like this

 $FolderPath = dir -Directory -LiteralPath '\\?\UNC\fs1\shared' -Recurse -Force

For more information you may refer to this link
https://docs.microsoft.com/zh-cn/archive/blogs/bclteam/long-paths-in-net-part-1-of-3-kim-hamilton

Best Regards,
Ian

============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi IanXue-MSFT ,

I have Run it as you mentioned but same error..

dir : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
At line:2 char:19
+ $FolderPath = dir -Directory -Path "\\neurodc\Shared Folders" -Recurse -Forc ...

0 Votes 0 ·
IanXue-MSFT avatar image IanXue-MSFT Kaleemullahbilal-2236 ·

Hi,
According to the error, the prefix in the path is still "\\". You‘re supposed to modify the first line of your script and it should be like this

 $FolderPath = dir -Directory -LiteralPath '\\?\UNC\neurodc\Shared Folders' -Recurse -Force
 $Report = @()
 Foreach ($Folder in $FolderPath) {
 $Acl = Get-Acl -Path $Folder.FullName
 foreach ($Access in $acl.Access)
 {
 $Properties = [ordered]@{'FolderName'=$Folder.FullName;'ADGroup or User'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited}
 $Report += New-Object -TypeName PSObject -Property $Properties
 }
 }
 $Report | Export-Csv -path "C:\data\FolderPermissions.csv"

Best Regards,
Ian

0 Votes 0 ·

Hi,

Now the CSV is getting created ..but its empty.. it supposed to be filled with the details of all the Permissions assigned to the users of all the sub-folders in that Shared Folder..??

0 Votes 0 ·
IanXue-MSFT avatar image IanXue-MSFT Kaleemullahbilal-2236 ·

Hi,
Please check if $FolderPath and $Acl are empty. To check them, you could run $FolderPath = dir -Directory -LiteralPath '\\?\UNC\neurodc\Shared Folders' -Recurse -Force and Get-Acl -Path $FolderPath.FullName in your powershell console.

0 Votes 0 ·