0

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
        }
    }
Royston
  • 433
  • 2
  • 9
  • 25
  • 1
    you need something like this:https://stackoverflow.com/questions/39630404/inner-join-in-powershell-without-sql – An-dir Nov 17 '22 at 19:42
  • 1
    So what doesn't work as expected? Note that unless the value of `$aovpn_Userdisplayname` happens to contain wildcard metacharacters (e.g, `Jane *`), your `-like` will function like `-eq`, so it will only find equal values (save for case variations). – mklement0 Nov 17 '22 at 20:01
  • 2
    Also, the `$aur_displayname` in `"$($aur_displayname),$($ao_vpn_userdisplayname)"` is an object representing a _whole row_, not just the single column value containing the display name. – mklement0 Nov 17 '22 at 20:12
  • 2
    You might want to have a look here: [In Powershell, what's the best way to join two tables into one?](https://stackoverflow.com/a/45483110/1701026)) – iRon Nov 17 '22 at 20:46
  • 1
    @iRon -Correct me if i'm wrong I think this works for joining on the displayname property, outputting the matched values for both CSVs? Join-Object -LeftObject $aovpn -RightObject $aur -On DisplayName | ft – Royston Nov 17 '22 at 21:33
  • 1
    p.s @iRon - great script – Royston Nov 17 '22 at 21:36
  • Yes, that would be a possible syntax, although I would consider to use the [PowerShell Pipeline](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_pipelines): `Import-Csv 'C:\scripts\AOVPN User list.csv' |LeftJoin (Import-Csv 'C:\scripts\Active User report.csv') -on DisplayName |...` (except that you can't write to the same input file in a pipe). – iRon Nov 18 '22 at 09:17

0 Answers0