首页 > 解决方案 > 将每个唯一的文件系统写在新行上 | 电源外壳

问题描述

我正在创建一个 PowerShell 脚本,它将对目录中所有文件夹启用的权限写入 CSV 文件以进行报告。此报告的要求是文件系统权限必须分别位于单独的行上,而不是全部位于一行上(我的代码目前就是这样做的)。例如,当前这些权限如下所示:

C:/temp-folder_123 (column) ReadData- ExecuteFile- Synchronize

但我希望它的格式如下:

C:/temp-folder_123 (column) ReadData
C:/temp-folder_123 (column) ExecuteFile
C:/temp-folder_123 (column) Synchronize

这是我的代码:

foreach ($access_right in $acl.Access) 
{
if ( ($access_right.FileSystemRights -notmatch $exclude_filesystem_rightss_regex) ) 
{
$file_stream_output.WriteLine(('{0}, {1}, {2}' -f $directory.FullName, $access_right.IdentityReference, ($access_right.FileSystemRights -replace ",","-" )))
}

标签: powershell

解决方案


要拆分 .FileSystemRights 您必须使用另一个 ForEach 对其进行迭代

foreach ($access_right in $acl.Access) {
    if (($access_right.FileSystemRights -notmatch $exclude_filesystem_rightss_regex) ){
        foreach($fsr in $access_right.FileSystemRights){
            $file_stream_output.WriteLine(('{0}, {1}, {2}' -f `
                $directory.FullName, 
                $access_right.IdentityReference, 
                $fsr)
        }
    )
}

推荐阅读