Изменение вашего кода, как показано ниже, работает для меня (PS версия 5.1 для Windows 10 Pro):
function Get-Folder($initialDirectory) {
# [System.Reflection.Assembly]::LoadWithPartialName() is deprecated
Add-Type -AssemblyName System.Windows.Forms
$dialog = New-Object System.Windows.Forms.FolderBrowserDialog
$dialog.Description = "Select a folder"
$dialog.rootfolder = "MyComputer"
if($dialog.ShowDialog() -eq "OK") {
$folder += $dialog.SelectedPath
}
$dialog.Dispose()
return $folder
}
$SelectedPath = Get-Folder
$FolderName = Get-Item -Path $SelectedPath | Select-Object -ExpandProperty Name
$FolderPath = Get-ChildItem -Directory -Path $SelectedPath -Recurse -Force
$date = '{0:yyyyMMdd_HHmmss}' -f (Get-Date)
$outputFile = "$env:USERPROFILE\Desktop\$FolderName_$date.csv"
# collect the objects returned
$Report = foreach ($Folder in $FolderPath) {
$Acl = Get-Acl -Path $Folder.FullName
foreach ($Access in $acl.Access) {
[PSCustomObject]@{
'FolderName' = $Folder.FullName
'AD Group or User' = $Access.IdentityReference
'Permissions' = $Access.FileSystemRights.ToString()
'Inherited' = $Access.IsInherited
}
}
}
# output on screen
$Report | Format-Table -AutoSize
# export to csv file
$Report | Export-Csv -Path $outputFile -NoTypeInformation
Если по какой-то причине вы по-прежнему получаете числовые значения вместо строк для FileSystemRights
, вы можете использовать такой переключатель для преобразования чисел в описательные строки:
# collect the objects returned
$Report = foreach ($Folder in $FolderPath) {
$Acl = Get-Acl -Path $Folder.FullName
foreach ($Access in $acl.Access) {
# see https://docs.microsoft.com/en-us/dotnet/api/system.security.accesscontrol.filesystemrights
$permissions = switch($Access.FileSystemRights.value__) {
2032127 { 'FullControl '; break}
1179785 { 'Read '; break}
1180063 { 'Read, Write '; break}
1179817 { 'ReadAndExecute '; break}
1245631 { 'ReadAndExecute, Modify, Write '; break}
1180095 { 'ReadAndExecute, Write '; break}
268435456 { 'FullControl (Sub Only) '; break}
-1610612736 { 'ReadAndExecute, Synchronize '; break}
-536805376 { 'Modify, Synchronize '; break}
default { $Access.FileSystemRights.ToString()}
}
[PSCustomObject]@{
'FolderName' = $Folder.FullName
'AD Group or User' = $Access.IdentityReference
'Permissions' = $permissions
'Inherited' = $Access.IsInherited
}
}
}
Обновление
Выше приведен список наиболее распространенных прав доступа к файлам. У файловых объектов может быть гораздо больше значений разрешений, установленных в Формате маски доступа значение флагов.
Перечисление System.Security.AccessControl.FileSystemRights
хранит эти значения, сопоставляя наиболее распространенные значения с понятными для человека словами:
Name Value
---- -----
ListDirectory 1
ReadData 1
WriteData 2
CreateFiles 2
CreateDirectories 4
AppendData 4
ReadExtendedAttributes 8
WriteExtendedAttributes 16
Traverse 32
ExecuteFile 32
DeleteSubdirectoriesAndFiles 64
ReadAttributes 128
WriteAttributes 256
Write 278
Delete 65536
ReadPermissions 131072
Read 131209
ReadAndExecute 131241
Modify 197055
ChangePermissions 262144
TakeOwnership 524288
Synchronize 1048576
FullControl 2032127
Глядя на AccessMask в приведенной выше ссылке, вы можете встретить значения, которых нет в этом перечислении, и чтобы перевести все это в слова, вы можете попробовать что-то вроде ниже.
Он использует перечисление System.Security.AccessControl.FileSystemRights
, отсортированное в в обратном порядке по Value
и сохраненное в переменной $FileSystemRights
.
Переменная $value
- это числовое значение, которое вы получили от $Access.FileSystemRights.value__
.
$PermsList = [System.Collections.Generic.List[string]]::new()
#if ($value -band 0xF1000000) {
# Generic access rights (bits 24..31)
if ($value -band 0x80000000) { [void]$PermsList.AddRange([string[]]('Read', 'ReadAttributes', 'ReadExtendedAttributes', 'Synchronize'))} # GENERIC_READ
if ($value -band 0x40000000) { [void]$PermsList.AddRange([string[]]('Write', 'WriteAttributes', 'WriteExtendedAttributes', 'Synchronize'))} # GENERIC_WRITE
if ($value -band 0x20000000) { [void]$PermsList.AddRange([string[]]('ReadAndExecute', 'ReadAttributes', 'ReadPermissions'))} # GENERIC_EXECUTE
if ($value -band 0x10000000) { [void]$PermsList.Add('FullControl')} # GENERIC_All
if ($value -band 0x1000000) { [void]$PermsList.Add('ChangePermissions')} # Right to access SACL
#}
# Standard access and Object specific rights (bits 0..23)
$value = $value -band 0xFFFFFF
$FileSystemRights | ForEach-Object {
if (($value -band $_.Value)) { [void]$PermsList.Add($_.Name) }
}
($PermsList.ToArray() | Select-Object -Unique) -join ', '
Значения, которые вы упомянули в комментарии, вернутся:
-2147483648 --> 'Read, ReadAttributes, ReadExtendedAttributes, Synchronize'
-268435456 --> 'Read, ReadAttributes, ReadExtendedAttributes, Synchronize, Write, WriteAttributes, WriteExtendedAttributes, ReadAndExecute, ReadPermissions, FullControl'
-1073741760 --> 'Read, ReadAttributes, ReadExtendedAttributes, Synchronize, Write, WriteAttributes, WriteExtendedAttributes, FullControl, DeleteSubdirectoriesAndFiles'
Надеюсь, это объясняет больше о различных разрешениях для файловых объектов.