我有一个漫长的脚本,从CSV文件中获取用户,将用户与特定目录中的CSV文件名匹配。
用户将以此格式<data><samaccountname><text>.csv
此处的目的是从列表中获取AD用户,然后扫描其中包含CSV文件的文件夹并与用户匹配。从那里恢复用户AD属性。
这里的问题是输出始终是最后一个用户两次,我在最后导出了REM,所以我可以先看到屏幕上的内容。
Clear-Host
#Get username from users list and match against CSV file name.
$FDate = (get-date).ToString("yyyMMdd")
$Project = "<FolderPath>" #Project name used to setup folders and for reports etc
$ProjectRoot = "<path>\" # Backup folder
$RestorePath = $ProjectRoot + $Project #combined path for restoring
$UsersListFile = $ProjectRoot + '\Userlist.csv' #Userlist
$Results = @{} # Storage for all csv files
$PSObject = New-Object psobject
$Report = @() #For Export-CSV
$Results = gci $RestorePath -Filter '*.csv'
$i = 0
foreach ($File in $Results) {
$i += 1
Write-Host 'Number of passes - '$i
Write-Host 'Current file processing - '$file.Name -for Green
foreach ($User in (import-csv $UsersListFile)) {
$SAM = $User.SamAccountName
Write-Host 'Current User processing - '$SAM -ForegroundColor Magenta
if ($file.Name -match $SAM) {
Write-host "Filename and user $SAM match " -for Yellow
$Row= New-Object psobject
$ROW | Add-Member -type NoteProperty -name Name -value $SAM -force
$Report += $Row
foreach ($Attrib in (import-csv $restorepath\$file)) {
#Write-host 'Attributes in file - ' $attrib.samaccountname $Attrib.mail -for Yellow
#Use this to restore AD User data
}
} else {
Write-Host "No match" -ForegroundColor Red
}
}
}
#$Report | Export-Csv $RestorePath'\Test.csv' -NoTypeInformation -Force
$Report | Sort-Object Name
答案 0 :(得分:1)
更新了将New-Object psobject移动到$ Row以上的脚本,因此每次都会创建一个新对象,而不是覆盖之前的条目。