powershell脚本 扫描出的文件路径不正确

问题遇到的现象和发生背景

大佬,辛苦帮忙下呢,网上这块的资料有点少,根据您这边的代码,我改了下扫描出的文件路径不正确,少了文件夹名字

用代码块功能插入代码,请勿粘贴截图
Function _Main_()
{ 
     [String]$INSPECT_Dir = "D:\document"
     [String]$lastTime = $(Get-Date)
     if([io.Directory]::Exists($INSPECT_Dir)){ #判断文件是否存在
        Write-Host "开始时间:$lastTime"
        Write-Host "Path exists"
        }else{
        Write-Host "Path doesnt exist!"
        }
    $UserList = New-Object -TypeName System.Collections.ArrayList
    #ref关键字使参数按引用传递,在调用方法的参数中使用ref关键字可以使得变量能够改变
    GetFDirByUserAuth -Path $INSPECT_Dir -Out ([REF]$UserList)
}

Function GetFDirByUserAuth
{   
    param(
        [String]$Path,
        [REF]$Out
    )
    # 目录存在
    if ($false -eq [IO.Directory]::Exists($Path))
    {
        $Out.Value = $null
        return $False
    }
     #获取目录下的文件夹/子文件
       try
    {
        [Array]$list = Get-ChildItem -Recurse $Path
        Write-Host "ChildDir-List: "$list.Count
 
        [int]$Count = 0;
        for([int]$i = 0; $i -lt $list.Count; $i++)
        {
            #扫描到的目录路径
            [String]$temPath = $Path + "\" + $list[$i]  
            Write-Host "temPath: $temPath"
        }
   }
        #递归遍历目录下的所有文件/子目录
        # Get-ChildItem $INSPECT_Dir -Recurse | ForEach-Object -Process{       
        #     if($_ -is [System.IO.FileInfo])
        #     {
        #     Write-Host($_.name);
        #     }

        # }
        catch
        {
            Write-Host "Error"
        }
} 

&_Main_

运行结果及报错内容

扫描出的文件路径不正确,少了文件夹名字。大佬,辛苦大佬看下有什么办法解决没呢
temPath: D:\document\《K8s CKA+CKS认证实战班》.pdf
temPath: D:\document\部署一套完整的K8s高可用集群(kubeadm-V1.20) .docx
temPath: D:\document\部署一套完整的K8s高可用集群(二进制-V1.20).docx
temPath: D:\document\(2022版)一套教程搞定k8s安装到实战 K8s集群安装(二进制安装Kubernetes 1.20).docx

img

我的解答思路和尝试过的方法
我想要达到的结果

递归下指定文件夹的目录及子文件 提取用户名及权限 并写到csv中

目前单独的命令是可以运行的
#导出文件目录下的权限和相关信息
Get-ChildItem -Path D:\document -Recurse| Get-Acl | Export-Csv -Encoding Default -Force -LiteralPath "E:\aa.csv"
#导出文件目录下相关信息
Get-ChildItem -Path D:\document -Recurse | Export-Csv -Encoding Default -Force -LiteralPath "E:\childItems.csv"
#导出文件目录下权限
Get-Acl D:\document -ErrorAction SilentlyContinue | Export-Csv -Encoding Default -Force -LiteralPath "E:\a.csv"

$folderPath = "C:\Path\To\Folder"
$users = Get-ChildItem $folderPath | ForEach-Object {Get-Acl $_.FullName} | Select-Object -ExpandProperty Access | Select-Object -ExpandProperty IdentityReference
$permissions = Get-ChildItem $folderPath | ForEach-Object {Get-Acl $_.FullName} | Select-Object -ExpandProperty Access | Select-Object -Property FileSystemRights
$output = @()

for ($i = 0; $i -lt $users.Count; $i++) {
    $output += New-Object PSObject -Property @{
        User = $users[$i]
        Permissions = $permissions[$i].FileSystemRights
    }
}
$output | Export-Csv "C:\Path\To\Output.csv" -NoTypeInformation

在上面的代码中,$folderPath 变量用于存储文件夹的路径。接下来,使用 Get-ChildItem 命令获取文件夹中的所有子文件。然后,使用 ForEach-Object 命令来遍历每个文件,并使用 Get-Acl 命令获取文件的 ACL 信息。

接着,使用 Select-Object 命令来提取文件的用户名和权限信息。最后,使用 Export-Csv 命令将这些信息写入 CSV 文件中。