PowerShell从多个文件中提取所有者元数据-失败

PowerShell从多个文件中提取所有者元数据-失败,powershell,loops,Powershell,Loops,在另一个线程的帮助下,我编写了一个脚本,用于从特定文件夹中提取文件名,并在更大的数据库中搜索原始文件并确定其“所有者”。当前脚本是: $desktopPath = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Desktop) $images = $desktopPath + '\Get_Owner' Get-ChildItem -Path $images | Select BaseName | E

在另一个线程的帮助下,我编写了一个脚本,用于从特定文件夹中提取文件名,并在更大的数据库中搜索原始文件并确定其“所有者”。当前脚本是:

$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”语句最初不提供每个文件的所有者

谢谢

据我所知,$true)正在运行一个无限循环,而在不使用-NoClobber开关的情况下导出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

我从来没有听说过——诺克洛伯!我使用了-Append,没有循环,它是有效的。非常感谢@Gareth Lyons<代码>-如果文件已存在,则假定NoClobber出错。基本上是“如果文件被覆盖,则失败”。据我所知,它是
-NoOverwrite
的同义词。您需要指定
-Append
以实际附加到文件。