2017-02-09 5 views
1

共有ドライブ構造内の各フォルダにあるすべてのACLを報告することが任されています。それに加えて、返された各ユニークグループのメンバシップを調べる必要があります。深いフォルダ上のNTFSSecurityモジュールを使用したPowershellメモリの枯渇

get-childitem2コマンドレットと組み合わせてNTFSSecurityモジュールを使用して、260文字のパスの長さ制限を超えます。私が横断しているパスは、260文字の制限を超えてから何百ものフォルダが深く長くなっています。

私は数週間これを叩いています。私の最初の挑戦は私の仕事を一気にやるようにスクリプトを作ることでしたが、今私の問題を考えています...手元の問題はリソース、特にメモリ枯渇です。スクリプトが深いフォルダの1つに入ると、それはすべてのRAMを消費し、ディスクへのスワップを開始し、最終的にはディスク領域を使い果たします。ここで

はスクリプトです:

$csvfile = 'C:\users\user1\Documents\acl cleanup\dept2_Dir_List.csv' 

foreach ($record in Import-Csv $csvFile) 
{ 

$Groups = get-childitem2 -directory -path $record.FullName -recurse | Get-ntfsaccess | where -property accounttype -eq -value group 
$groups2 = $Groups | where -property account -notmatch -value '^builtin|^NT AUTHORITY\\|^Creator|^AD\\Domain' 
$groups3 = $groups2 | select account -Unique 

$GroupMembers = ForEach ($Group in $Groups3) { 
    (Get-ADGroup $Group.account.sid | get-adgroupmember | select Name, @{N="GroupName";e={$Group.Account}} 

)} 
$groups2 | select FullName,Account,AccessControlType,AccessRights,IsInherited | export-csv "C:\Users\user1\Documents\acl cleanup\Dept2\$($record.name).csv" 
$GroupMembers | export-csv "C:\Users\user1\Documents\acl cleanup\Dept2\$($record.name)_GroupMembers.csv" 
} 

注:これは、get-childitem2 -directoryから作成したトップレベルのフォルダで読み込みDIR一覧| export-csv filename.csv

実行中は、メモリが正しくフラッシュされていないようです。これは観測からの推測です。コードを実行するたびに変数が上書きされるはずだと思いましたが、メモリがダウンしないので、メモリが元に戻りませんでした。それを適切に解放する?私が言ったように、推測...私はランスペースについて読んでいるが、私はこのスクリプトでそれを実装する方法について混乱している。これは正しい方向ですか?

ご協力いただきありがとうございます...!

+0

_私が通過しているパスは何百ものフォルダですが、何がこのような状況につながっているのだろうか。 Windowsで作業する場合、これは無数の問題を引き起こします...あなただけでなく、ユーザーにとっても。 – sodawillow

+1

完全に合意しました。これが修復の第一歩です。どのようなグループが何にアクセスできるかを知る必要があります。その前に、組織をよりクリーンで組織的な環境に移行することについて話すことができます。私はいつも260文字制限が存在することを知っていましたが、本当にそれに対処する必要があることを私が覚えているとは言いません。ここでは、毎日です。 alphafs.dllとget-childitem2コマンドレットのために、毎日のPowerShellの神に感謝します。 – RichardX

+0

同様のスクリプトがあり、開始時に$ ErrorActionPreference = 'silentlycontinue'という行が追加されたので、「Get-ChildItem」を使用したエラーメッセージは表示されません。 try {} catch {}はforeachにエラーを処理する必要があります。ファイル名のフィルタは、これらの長いフォルダ名のリストを取得するか、無視します。Get-ChildItem | ? {$ _。FullName.length -lt 255} – lloyd

答えて

0

面白いと思いますが、面白いと思うスクリプトの修正版が完成したばかりです。友人がここでうまくいくような「機能フィルタ」に私を向ける。明日、大きなディレクトリでそれをテストして、メモリ管理がどれだけ優れているかを見ていますが、これまでのところは素晴らしいようです。

#Define the function ‘filter’ here and call it ‘GetAcl’. Process is the keyword that tells the function to deal with each item in the pipeline one at a time 
Function GetAcl { 

      PROCESS { 
      Get-NTFSAccess $_ | where -property accounttype -eq -value group | where -property account -notmatch -value '^builtin|^NT AUTHORITY\\|^Creator|^AD\\Domain' 
        } 
       } 

#Import the directory top level paths 
$Paths = import-csv 'C:\users\rknapp2\Documents\acl cleanup\dept2_Dir_List.csv' 

#Process each line from the importcsv one at a time and run GetChilditem against it. 
#Notice the second part – I ‘|’ pipe the results of the GetChildItem to the function that because of the type of function it is, handles each item one at a time 
#When done, pass results to Exportcsv and send it to a file name based on the path name. This puts each dir into its own file. 
ForEach ($Path in $paths) { 
(Get-ChildItem2 -path $path.FullName -Recurse -directory) | getacl | export-csv "C:\Users\rknapp2\Documents\acl cleanup\TestFilter\$($path.name).csv" } 
関連する問題