2017-08-25 7 views
1

ユーザーがExcelでフィルタできるように、以下のようなCSV出力を取得しようとしています。Powershell:再帰的にディレクトリのアクセス権を取得

フォルダ、グループ、パーミッション 私は:\ Folder1に、CORP \ GROUP1、READDATA、EXECUTEFILEは、 I同期化:\ Folder1に\フォルダ2、CORP \グループ2、読み出しデータ、EXECUTEFILEを、以下

を同期が開始されたものですと。非常に非効率的であり、所望のCSV出力を与えない。助けていただければ幸いです。

$output_file = $(get-date -f MM-dd-yyyy_HH_mm_ss)+'.txt' 
"{0},{1},{2}" -f "Folder","Groups","Permissions"| add-content -path $output_file 


$file_content = '' 

function GetFolders($path = $pwd) 
{ 
    if($path -ne $null) { 
     $new_row = Get-ACL $path | select -ExpandProperty Access | Where-Object IdentityReference -Like "CORP*" | SELECT $path, IdentityReference, FileSystemRights | Format-Table -HideTableHeaders | Out-String 
     $fileContent += $new_row 
     $fileContent | add-content -path $output_file 

     foreach ($item in Get-ChildItem $path) 
     { 
      if (Test-Path $item.FullName -PathType Container) 
      { 
       Write-Output $item.FullName 
       GetFolders $item.FullName 
       $new_row = Get-ACL $item.FullName | select -ExpandProperty Access | Where-Object IdentityReference -Like "CORP*" | SELECT $item.FullName, IdentityReference, FileSystemRights | Format-Table -HideTableHeaders | Out-String 
       $fileContent += $new_row 
       $fileContent | add-content -path $output_file 
      } 
     } 
    } 
} 


GetFolders "J:\" 

答えて

1

あなたは適切な道を歩いていましたが、コースから少し離れました。

Set-Content -Path $FileName -Value 'Folder,Groups,Permissions' 

(Get-Acl -Path $Path).Access | 
    Where-Object -Property IdentityReference -like 'corp*' | 
    ForEach-Object { 
     Add-Content -Path $FileName -Value "$Path,$($_.IdentityReference),$($_.FileSystemRights -replace '\s')" 
    } 

(あなたが部分式またはその自然の何かでコードを編集したい場合は)もう少し派手

$Val = {"$Path,$($_.IdentityReference),$($_.FileSystemRights -replace '\s')"} 
... -Value (&$Val) ... 
+0

的には、使用している理由を私は得ることはありません ' 'Export-Csv'の代わりに' Add-Content'を追加します – Clijsters

+0

どちらも有効ですが、データがすでに形成されている場合、 'Add-Content'はオーバーヘッドが少なくなります。 @Clijsters – TheIncorrigible1

+0

確かに、それも遅いですね。私はそれがハードウェアに依存していると思うが、反復ごとにストリームを開閉すると重く聞こえる。しかし、ええ、オーバーヘッドが少なくなると、非常に明確で簡単なコード例になります。 – Clijsters

関連する問題