2017-11-01 8 views
0

別のスレッドのヘルプを使用して、特定のフォルダからファイル名を取得し、元のファイルの大きなデータベースを検索してその所有者を特定するスクリプトを作成しました。現在のスクリプトは次のとおりです。複数のファイルから所有者のメタデータを取得するPowerShell - 失敗

$desktopPath = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Desktop) 
$images = $desktopPath + '\Get_Owner' 
Get-ChildItem -Path $images | Select BaseName | Export-Csv $desktopPath`\Filenames.csv -NoTypeInformation 
$serverPath = 'C:\Users\tuggleg\Desktop\Archive' 
$files = Import-Csv -Path $desktopPath`\Filenames.csv 

#import filenames and search server for filename and owner 

ForEach ($fileName in $files.BaseName) 
{ 
    Get-ChildItem -Path $serverPath -Filter "*$fileName*" -Recurse -ErrorAction 'SilentlyContinue' | 
      Select-Object -Property @{ 
      Name='Owner' 
      Expression={(Get-Acl -Path $_.FullName).Owner} 
      },'*' | 
     Export-Csv -Path $desktopPath`\Owners.csv -NoTypeInformation 
} 

このスクリプトは、ユーザーがローカルファイル($画像)を移動し、ファイル名を引っ張るフォルダを対象としています。次に、元のファイルの大きなデータベース($ serverPath)を検索し、所有者を取得します。このスクリプトは1つのファイル名で動作しますが、複数のファイルが '$ images'フォルダにある場合、 'ループ'せずに作業を続行します。私は、次のことを試してみました、スクリプトが無期限に実行されますが、出力CSVにデータを提供するdoesntの:

$desktopPath = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Desktop) 
$images = $desktopPath + '\Get_Owner' 
Get-ChildItem -Path $images | Select BaseName | Export-Csv $desktopPath`\Filenames.csv -NoTypeInformation 
$serverPath = 'C:\Users\tuggleg\Desktop\Archive' 
$files = Import-Csv -Path $desktopPath`\Filenames.csv 

#import filenames and search server for filename and owner 
#loop script 
While($true) { 

ForEach ($fileName in $files.BaseName) 
{ 
    Get-ChildItem -Path $serverPath -Filter "*$fileName*" -Recurse -ErrorAction 'SilentlyContinue' | 
      Select-Object -Property @{ 
      Name='Owner' 
      Expression={(Get-Acl -Path $_.FullName).Owner} 
      },'*' | 
     Export-Csv -Path $desktopPath`\Owners.csv -NoTypeInformation 
} 
} 

ループ機能は、スクリプトを壊し、なぜ誰でも識別することはできますか?それとも、最初の 'foreach'ステートメントが最初に各ファイルの所有者を提供しないのはなぜですか?

ありがとうございます!

答えて

1

私は、($ true)が無限ループを実行していて、-NoClobberスイッチなしのExport-Csvがループの繰り返しごとにファイルを上書きしている間に(= $ filesのエントリ)。

NoClobber(および/または-Append)スイッチを追加してみてください。& While($ true)ループを削除すると、既にForEachを取得しているように見えます。

そうしないと、データを収集することができ&、最後に一回のようなものCSVするためにそれを書く:私は-noclobberのことを聞いたことがありません

ForEach ($fileName in $files.BaseName) 
{ 
$data += Get-ChildItem -Path $serverPath -Filter "*$fileName*" -Recurse -ErrorAction 'SilentlyContinue' | 
      Select-Object -Property @{ 
      Name='Owner' 
      Expression={(Get-Acl -Path $_.FullName).Owner} 
      },'*' 
} 

$data | Export-Csv -Path $desktopPath`\Owners.csv -NoTypeInformation 
+0

を!私は、-AppendとLoopなしで一緒に行きました。ありがとうございます@ガレスリヨン! – Garrett

+0

あなたはようこそ! –

+0

ファイルがすでに存在する場合、 '-NoClobber'はエラーとみなされます。基本的には「ファイルを上書きすると失敗します。私が知る限り、 '-NoOverwrite'と同義です。実際にファイルに追加するには '-Append'を指定する必要があります。 –

関連する問題