0

I need help. I have to object arrays $mySQL and $tassSQL which i need to loop through and compare the column named folder code 1 and if this column data is not listed in $mySQL then add the info into a hash table.

So far my code looks like the following: However, i seem to still have it populating with all data.

$mySQL = @(
    [pscustomobject]@{
        'Parent ID' = '234'; 
        'Folder Name' = 'Senior Visual Art 31 2023'; 
        'Folder Colour' = '#45640b3'
        'Folder Picture' = $null
        'Folder Code 1' = '-VARSR-31'
        'Folder Code 2' = $null
        'Folder Code 3' = $null
        'YearLevel' = '12'
    }
    [pscustomobject]@{
        'Parent ID' = '264'; 
        'Folder Name' = '10 Manual Art 1 2022'; 
        'Folder Colour' = '#45640b3'
        'Folder Picture' = $null
        'Folder Code 1' = '-MAAR10122'
        'Folder Code 2' = $null
        'Folder Code 3' = $null
        'YearLevel' = '10'
    }
    
)
$tassSQL = @(
    [pscustomobject]@{
        'Parent ID' = '234'; 
        'Folder Name' = 'Senior Visual Art 31 2023'; 
        'Folder Colour' = '#45640b3'
        'Folder Picture' = $null
        'Folder Code 1' = '-VARSR-31'
        'Folder Code 2' = $null
        'Folder Code 3' = $null
        'YearLevel' = '12'
    }
)

$classes = @()

ForEach ($classCode In $tassSQL) 
{

    $parent = $classCode.'Parent ID'
    $Name = $classCode.'Folder Name'
    $Colour = $classCode."Folder Colour"
    $code1 = $classCode.'Folder Code 1'
    $grade = $classCode.'YearLevel'

    If ($MySQL.'Folder Code 1' -notcontains $code1)
    {
        $classes += New-Object PSObject -Property @{
            'Parent ID' = $parent
            'Folder Name' = $Name
            'Folder Colour' = $Colour
            'Folder Picture' = $null
            'Folder Code 1' = $code1
            'Folder Code 2' = $null
            'Folder Code 3' = $null
            'YearLevel' = $grade
        }

    }

}
Fallen Soul
  • 27
  • 1
  • 7
  • 1
    it seems like you're looking for `$classes = $mySQL | ? { $_.'Folder Code 1' -notin $tassSQL.'Folder Code 1' }` – Santiago Squarzon Sep 23 '22 at 20:44
  • 1
    Thank you. I think that may have done the job. I will now need to test with live data. – Fallen Soul Sep 23 '22 at 21:53
  • 1
    Basically you want to find objects where `'Folder Code 1'` is present in `$mySQL` and not present in `$tassSQL`, if that's the case what I posted above should do it. Though not the most efficient way to do it. If you have very big objects I can post a most efficient answer – Santiago Squarzon Sep 23 '22 at 22:03
  • 1
    [try avoid using the increase assignment operator (`+=`) to create a collection](https://stackoverflow.com/a/60708579/1701026) as it is exponentially expensive. Using [member-access enumeration](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_member-access_enumeration), you can simplify this to: `$classes = $TassSQL.Where{ $_.'Folder Code 1' -notin $MySql.'Folder Code 1' }` – iRon Sep 24 '22 at 07:11

0 Answers0