csv - Match user from list against filenames -
i have long winded script gets user csv file, matches user against csv filename specific directory.
the user matched against csv file in format <data><samaccountname><text>.csv
the aim here ad user list, scan folder csv files in , match against user. there restore user ad attributes.
the issue here output of last user twice, have rem out export @ end can see on screen first.
clear-host #get username users list , match against csv file name. $fdate = (get-date).tostring("yyymmdd") $project = "<folderpath>" #project name used setup folders , reports etc $projectroot = "<path>\" # backup folder $restorepath = $projectroot + $project #combined path restoring $userslistfile = $projectroot + '\userlist.csv' #userlist $results = @{} # storage 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 , 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 restore ad user data } } else { write-host "no match" -foregroundcolor red } } } #$report | export-csv $restorepath'\test.csv' -notypeinformation -force $report | sort-object name
updated script move new-object psobject above $row, creates new object each time, rather overwriting previous entry.
Comments
Post a Comment