方法调用失败

问题描述 投票:0回答:1

尝试从共享驱动器之一导出文件夹结构和权限。

我们获得了下面提到的脚本,但是,我们在第 24 行收到错误提示。

这是错误:

方法调用失败,因为 [System.Management.Automation.PSObject] 不包含名为“op_Addition”的方法。 在 C:\Temp\Export 具有权限的文件夹结构 v2.ps1:24 字符:12

  •        $permissionsData += $permissionData
    
  •        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo:InvalidOperation:(op_Addition:String)[],RuntimeException
    • FullyQualifiedErrorId:MethodNotFound

这是运行的脚本。

支持导出带列的CSV: A 列:文件夹路径 B 栏:身份参考 C 列:访问控制类型 D 列:继承标志 E 列:PropagationFlags 列:F FileSyStemRights

这是脚本:

# Define the root folder path
$rootFolder = "C:\Path\To\Your\Folder"
# Define the output CSV file path
$outputFile = "C:\Path\To\Output\permissions.csv"
# Create an empty array to store permission data
$permissionsData = @()
# Function to recursively get and export permissions
function Export-Permissions {
   param (
       [string]$folderPath
   )
   $items = Get-ChildItem -Path $folderPath
   foreach ($item in $items) {
       $permissions = Get-Acl -Path $item.FullName | Select-Object -ExpandProperty Access
       foreach ($permission in $permissions) {
           $permissionData = New-Object PSObject -Property @{
               "FolderPath" = $item.FullName
               "IdentityReference" = $permission.IdentityReference
               "AccessControlType" = $permission.AccessControlType
               "InheritanceFlags" = $permission.InheritanceFlags
               "PropagationFlags" = $permission.PropagationFlags
               "FileSystemRights" = $permission.FileSystemRights
           }
           $permissionsData += $permissionData
       }
       Export-Permissions -folderPath $item.FullName
   }
}
# Start the export
Export-Permissions -folderPath $rootFolder`enter code here`
# Export the data to a CSV file
$permissionsData | Export-Csv -Path $outputFile -NoTypeInformation
Write-Host "Permissions exported to $outputFile
powershell export folder-permissions cacls
1个回答
0
投票

完全避免

+=
- 只需在进行过程中一一输出权限对象即可:

# Define the root folder path
$rootFolder = "C:\Path\To\Your\Folder"
# Define the output CSV file path
$outputFile = "C:\Path\To\Output\permissions.csv"

function Export-Permissions {
    param (
        [string]$folderPath
    )

    $items = Get-ChildItem -Path $folderPath

    foreach ($item in $items) {
        $permissions = Get-Acl -Path $item.FullName | Select-Object -ExpandProperty Access
        foreach ($permission in $permissions) {
            # just let the output object(s) bubble up to the caller - no need to collect anything
            [PSCustomObject]@{
                'FolderPath'        = $item.FullName
                'IdentityReference' = $permission.IdentityReference
                'AccessControlType' = $permission.AccessControlType
                'InheritanceFlags'  = $permission.InheritanceFlags
                'PropagationFlags'  = $permission.PropagationFlags
                'FileSystemRights'  = $permission.FileSystemRights
            }
        }

        if ($item -is [DirectoryInfo]) {
            Export-Permissions -folderPath $item.FullName
        }
    }
}

Export-Permissions -folderPath $rootFolder |Export-Csv -Path $outputFile -NoTypeInformation
© www.soinside.com 2019 - 2024. All rights reserved.