2017-11-01 1 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)를 검색하고 소유자를 가져옵니다. 이 스크립트는 하나의 파일 이름으로 작동하지만 여러 파일이 '$ images'폴더에있을 때 '반복'하지 않고 계속 작동합니다. 나는 시도는 다음과 스크립트는 무기한 실행되지만 출력 CSV 데이터를 제공 나던 : 루프 기능은 스크립트를 나누기 이유

$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에 기록, 뭔가 같은 :

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

나는 -NoClobber 들어 본 적이 없다! 나는 -Append와 no loop를 가지고 갔다. 너무 감사합니다 @가 레스 리용! – Garrett

+0

여러분을 환영합니다! –

+0

파일이 이미 존재하면'-NoClobber'는 오류가 발생합니다. 기본적으로 "파일을 덮어 쓰면 실패합니다". 내가 아는 한,'-NoOverwrite'와 동의어입니다. 실제로 파일에 추가하려면'-Append'를 지정해야합니다. –

관련 문제