So, i've got a fantastic bit of code i love running which uses Out-File -InputObject
to Output data captured to a fresh CSV.
I specify a CSV and the headers, run a command say a cmdlet and can output the information to the CSV matching the headers i specified. It's great.
Now the task i'd like to try to achieve now is a bit more complex and adds to the above but i'm not there yet.
I have 2 CSV files as input - both contain a 'displayname' field. Essentially what i'm trying to do is ForEach
the first CSV, take the DisplayName
row, then run it in a nested Foreach and loop against the second CSV which also has a DisplayName
field, if a match is found, output this header and a couple of other basic headers to a 3rd CSV output File. My logic isn't quite right though, this is a confusing one.
Code so far:
$OutputFile = "c:\scripts\AOVPN_Users.csv"
Out-File -FilePath $OutputFile -InputObject "AO_DisplayName,AUR_User_DisplayNameMatch" -Encoding UTF8 -Append -Force
# Input List
$aur = import-csv 'C:\scripts\Active User report.csv'
$aovpn = import-csv 'C:\scripts\AOVPN User list.csv'
#Loop through data files
ForEach($user in $aovpn) {
$aovpn_Userdisplayname = $user.displayname
ForEach($aur_displayname in $aur){
$aur_displayname2 = $aur_displayname.displayname
If ($aur_displayname2 -like $aovpn_Userdisplayname) { Write-Host "Test Output - Match Found for" $aur_displayname2 -ForegroundColor Green
Out-file -filepath $OutputFile -InputObject "$($aur_displayname),$($ao_vpn_userdisplayname) -append
}
}