Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Using a lookup from one CSV file to another CSV file errors with a name clash

$
0
0

Hi

I have a master CSV file created by querying the AD. I need to populate the "Team" attribute by doing a lookup using Name against  the Name Column any of 6 CSVs,  someone has prepared for me .  The problem is the data is inconsistent in the 6 CSV files so Name can hold either the Name or Username i.e. SAmAccountName or LoginName

Now my ps script works when the data is good. but I have around 30 exception cases whereby there is a mismatch .  To explain:

is in this line ( see the big code block below) 

   $userObject = $nameAndTeamCsvData | Where-Object {$_.Name -eq $masterUserName} 

The $_.Name is using the top level masterCSVData Name but I was hoping that it was using the$nameAndTeamCSVData.Name as in only within the scope of the Where-Object {} ..I guess I have misunderstood the syntax of what I had written.

. A quick a dirty fix would be rename this column in all of the 4 spreadsheets or trying to fix the code.....

# now update the empty team value for user object $csvMasterData | ForEach-Object ` { $masterName = $_.Name $masterUserName = $_.Username # to deal with exception cases #force scope to as the properties in the outer with the same name should be out of scope # lookup the Name to see if we can extract the user's Team $userObject = $nameAndTeamCsvData | Where-Object {$_.Name -eq $masterName } # deal with the situation where the name in the businesses spreadsheets is actually the Username (login name) in the master csv if ( $userObject -eq $null ) { # lookup the username (loginname) to see if we can extract the user's Team

# !!!!error occurs here with the $_.Name !!!!! $userObject = $nameAndTeamCsvData | Where-Object {$_.Name -eq $masterUserName} if ( $userObject -eq $null ) { $_.Team = "UNKNOWN" } else { # replace the mastercsv.Team with the one we have looked up $_.Team = $userObject.Team } } else { # Name matches so replace the mastercsv.Team with the one we have looked up $_.Team = $userObject.Team } }

 


Daniel



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>