簡體   English   中英

PowerShell從多個文件中提取所有者元數據-失敗

[英]PowerShell pulling owner metadata from multiple files - failure

在另一個線程的幫助下,我整理了一個腳本以從特定文件夾中提取文件名,並在較大的數據庫中搜索原始文件並確定其“所有者”。 當前腳本是:

$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
}

該腳本以用戶移動本地文件($ images)並提取文件名的文件夾為目標。 然后,它在較大的數據庫($ 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”語句最初沒有為每個文件提供所有者?

謝謝!

據我所知,While($ 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM