Home > Net >  Filtering Data out of 2 CSVs and Getting Objects within Object
Filtering Data out of 2 CSVs and Getting Objects within Object

Time:02-11

So what I am attemping to do is filter some data through a csv from a csv and when it goes through it searches the column and give me all the values that have a matching value in the 2nd CSV from the 1st one. However the values its giving are the groups when in fact I need the devices that are in the groups. So kinda like a group of object inside of an object.

$Date = $(Get-Date -f MM_dd_yyyy)
$azuregroupdata = import-csv -Path "\\iPadIntuneMDM\CSV\$Date-AzureGroupsdata.csv"
 


$resultdata = import-csv -Path "\\iPadIntuneMDM\CSV\$Date-MatchinDevices.csv"
$groupsinboth = (Compare-Object -ReferenceObject ($resultdata.NewGroup) -DifferenceObject ($azuregroupdata.DisplayName) -IncludeEqual | `
        Where-Object { $_.SideIndicator -eq "==" } | `
        Select-Object -ExpandProperty InputObject) 


$results2 = ForEach ($Group in $groupsinboth) {
    
    $A2 = $resultdata | Where-Object { $_.NewGroup -eq $Group }
    $I2 = $azuregroupdata | Where-Object { $_.DisplayName -eq $Group }
    New-Object -TypeName psobject -Property @{
        "Serial"          = $A2.Serial
        "ObjectID"        = $A2.ObjectID
        "AzureADGroupID" = $I2.ObjectId
    }
}
Write-Host $results2
$results2 | Export-CSV -NoTypeInformation -Path \\iPadIntuneMDM\CSV\$(Get-Date -f MM_dd_yyyy)-DevicesandgroupIDs.csv

In the CSVs I have the following data

AzureGroupsData

#TYPE Microsoft.Open.AzureAD.Model.Group
DeletionTimestamp ObjectId Description DisplayName SecurityEnabled
4e29b8b7-bdc7-4d21-a13b-5517514a791e iPad-EDC-PreDeploy iPad-EDC-PreDeploy TRUE
9737a1de-79f3-4e0c-8b47-0933bd5613e4 iPad-SAE-Students-ML01 iPad-SAE-Students-ML01 TRUE
d7166f43-4562-4bff-8e92-e420df9b2e9c iPad-PER-Students-ML03 iPad-PER-Students-ML03 TRUE
1952810e-88da-49cd-bb8c-0edb26f61cd5 iPad-SAE-Staff-ML01 iPad-SAE-Staff-ML01 TRUE
39d2bc70-7ed8-4b9a-b4b4-b3843df7e20b iPad-SAE-Students-ML04 iPad-SAE-Students-ML04 TRUE
53b24a28-ee91-4c14-8020-21ab431c89de iPad-RLE-Staff-ML01 iPad-RLE-Staff-ML01 TRUE
c40bb857-4388-4c26-9b68-b7ffa688a5d4 iPad-GAE-Staff-ML04 iPad-GAE-Staff-ML04 TRUE
a4764233-3b7d-4420-838c-806d1db8ca05 iPad-SAE-Students-ML02 iPad-SAE-Students-ML02 TRUE
705d5fd0-b788-4158-b684-6f225783484a iPad-EDC-Staff-IT iPad-EDC-Staff-IT TRUE
da75d1f7-7945-4cb1-9618-51f914651ea6 iPad-SRC-Staff iPad-SRC-Staff TRUE

ResultData

ObjectID Serial NewGroup DeviceID OldGroup
0fb06be4-13c3-4040-af68-85fae174895a DLXNL73AG5YP iPad-EDC-PreDeploy 5f54d29a-aad6-4f41-98a0-ddf7a8c85250 4521
01c1628e-45d3-42d8-abd9-29f7f10ea321 DMPNP6W0G5VW iPad-SAE-Students-ML01 14c4cbcd-4be7-498d-8fdd-be376a2b3a7a 4597
0be2d71a-34cb-4f0a-a5dd-9b88d105c47a DMPNP5SQG5VW iPad-PER-Students-ML03 7e7b0c62-2f74-4000-aafb-1cc725d10ddd 4633
1a4393bf-c89d-478c-aa86-ff23c3eedd66 DMPNP65YG5VW iPad-EDC-PreDeploy 55d6ae86-e07a-4be5-ab0c-c3d8e9e5b4f4 4521
1d2afa19-ceb9-4e7c-8638-4e86607249d2 DMPPX0RDG5YP iPad-EDC-PreDeploy 0995fd79-cdd3-4186-93ef-237356401aa4 4521
1d884772-645c-4044-bec7-24240af0a52a DMPNP70WG5VW iPad-EDC-PreDeploy da20b47a-658e-4a2e-a02d-9c49aa585739 4521
1ddee2c6-9326-47d1-a6a2-8b4fa410a1e4 DMPNP6GNG5VW iPad-SAE-Students-ML01 dab324ed-68c6-43e4-8b66-25dc204b0705 4597
31c4d5f8-14a6-468d-806c-e4848b98d08c DMPNNW7SG5VW iPad-SAE-Staff-ML01 e1e15427-dd65-4174-a9c2-62bf63ed28cf 4611
36e66c40-dfcd-41ff-b038-96d66fa07ffb DMPNP685G5VW iPad-PER-Students-ML03 a1be4983-24e8-4063-ad4e-132c9efecffd 4633
3e1d76b5-e633-4372-a9f7-13aa8c6364ad DMPNP4U2G5VW iPad-SAE-Students-ML04 c6414402-ccb1-45ad-b756-fa8a987ff473 4600
3f5d7f96-a394-4c75-99c8-0a588437a6da DMPNP51EG5VW iPad-PER-Students-ML03 3c0d9145-a499-443e-a6a5-87deefe0d117 4633
4165c5f4-d57e-4cce-82ce-5b8a87ccecd5 DMPNP5HQG5VW iPad-EDC-PreDeploy d650e345-d978-482f-a418-543fede68299 4521
49ad55f2-dd4a-4598-80e8-c33d05afc3eb DMPNP67RG5VW iPad-PER-Students-ML03 4426c45f-bb12-4f70-9c8f-ab3db3aa2a7e 4633
53bfa152-80f8-451f-9086-3c37d3d61d13 DMPNP6Z3G5VW iPad-RLE-Staff-ML01 f75f767a-bac8-4a29-a57a-8362caeff759 4568
53d25bcc-f124-4ca9-afb1-cbf450a5116f DMPNPPSVG5VW iPad-EDC-PreDeploy 2c100fbc-0d17-4209-9f8a-18253878a465 4521
57436992-5749-431e-8cca-a2476b8f656c DMPPW3WFG5YP iPad-EDC-PreDeploy 212949c6-a49d-4d8a-89d1-fcc6939c7629 4521
599c5285-0b54-48cb-8d6a-8c79c3d3d7ca DMPNP61JG5VW iPad-GAE-Staff-ML04 491a581c-ef20-456e-8657-08cc63901603 4610
5a0bb215-059b-421e-9446-f6aed9cd62c4 DMPNP6EUG5VW iPad-SAE-Students-ML01 3b23aa12-4f3b-44f7-944b-c3b69f74c9ff 4597
6f096b30-6bc2-4289-a46e-7d91a08d5309 DMPNPB9MG5VW iPad-EDC-PreDeploy 662a15bf-c2a9-48ce-922c-688377030c3f 4521
74a84b5a-cdbb-4bd6-ae31-c0f785702ea2 DMPNP6DRG5VW iPad-SAE-Students-ML02 b175a280-a3eb-4b25-ae2b-06b4b586e235 4598
766a1907-31cd-41f9-bfac-4a0fa3e2f514 DMPPX0S2G5YP iPad-EDC-PreDeploy 25faf41f-5a52-4bfd-983e-5fe735530ee7 4521
7ab6b17b-08f3-45d7-afea-58ef01701d78 DMPNP6D9G5VW iPad-EDC-PreDeploy 7a6a2552-d683-4202-9b10-3d4af7bbbae5 4521
7c30ec0e-d754-4035-9762-b891f36bbc74 DMPNP6ETG5VW iPad-SAE-Students-ML02 3b254910-74ef-44c8-8d35-b63f7cad0cff 4598
7ff51330-9687-48b3-bdca-82b539ac64bc DMPN5PSYFK11 iPad-EDC-PreDeploy 009b60e4-d3ba-4df7-9bc9-71929963513e 4521
8cdab7fe-bd1f-4a8b-8bb0-a4b4e15cf921 DMPNP6WNG5VW iPad-EDC-PreDeploy 807635ac-5f55-4582-936a-7fb376e95003 4521
930d2407-074d-43c4-93c5-1724cd2e31d8 DMPNP6JRG5VW iPad-SAE-Students-ML01 469bfe61-02f4-485e-93c3-bad2e63028ee 4597
961b4b75-783f-4aa1-b012-7b4de13ab437 DMPNP6FSG5VW iPad-SAE-Students-ML02 b5a96974-05ad-44be-9d9b-d66ee9b64252 4598
96300aec-5e6b-4ddb-90a3-d1aec58d4934 DMPNP6VNG5VW iPad-EDC-PreDeploy 99114f99-b6de-4d0c-adda-125df8ee116b 4521
9e4a9329-35d2-40c7-9e09-de0c9a809ced DMPNP756G5VW iPad-EDC-PreDeploy c79bb992-7363-47b8-bcdf-b84e2312cf0c 4521
9ee5d6a4-2ffc-46ae-9c65-4a50208d035d DMPPX0PMG5YP iPad-EDC-PreDeploy 87f2149a-f853-4f4d-a1b4-89236b2c02b5 4521
ac1daa36-6382-493c-914f-a55d934c3a06 F9GDNNC8Q1GC iPad-SRC-Staff ebec06e9-2c12-49c1-806e-cc86d94053dd 4677
ad19d31e-11f2-450a-abee-1af1c9e4a7d4 DMPNP65UG5VW iPad-EDC-PreDeploy 7a6e73ad-31cb-4ae5-9fb1-e63f64fad914 4521
bd978541-6254-4ad1-bb4e-e7640dea083b DMPNP6DBG5VW iPad-EDC-PreDeploy 0f12fb3b-4cd8-4b2a-91d2-3bf824d4fdfb 4521
da8eb01e-2f5a-41d6-8ee3-a18c95a09256 DMPNP6KSG5VW iPad-EDC-PreDeploy dc81d237-a6ad-497d-9109-08999a01209f 4521
e434527b-4371-48d6-8396-dc8d085c5a04 DMPNP6D6G5VW iPad-EDC-Staff-IT 51a05fef-0161-41c6-a1ee-1955083ec8c6 4588
e6d8ad88-6971-4169-ae49-c2616eedb523 DMPNP6Q6G5VW iPad-SAE-Students-ML01 368ac2e0-2e08-440d-9c10-812087bfe855 4597

Output $results2

ObjectID Serial AzureADGroupID
31c4d5f8-14a6-468d-806c-e4848b98d08c DMPNNW7SG5VW 1952810e-88da-49cd-bb8c-0edb26f61cd5
3e1d76b5-e633-4372-a9f7-13aa8c6364ad DMPNP4U2G5VW 39d2bc70-7ed8-4b9a-b4b4-b3843df7e20b
System.Object[] System.Object[] 4e29b8b7-bdc7-4d21-a13b-5517514a791e
53bfa152-80f8-451f-9086-3c37d3d61d13 DMPNP6Z3G5VW 53b24a28-ee91-4c14-8020-21ab431c89de
e434527b-4371-48d6-8396-dc8d085c5a04 DMPNP6D6G5VW 705d5fd0-b788-4158-b684-6f225783484a
System.Object[] System.Object[] 9737a1de-79f3-4e0c-8b47-0933bd5613e4
System.Object[] System.Object[] a4764233-3b7d-4420-838c-806d1db8ca05
599c5285-0b54-48cb-8d6a-8c79c3d3d7ca DMPNP61JG5VW c40bb857-4388-4c26-9b68-b7ffa688a5d4
System.Object[] System.Object[] d7166f43-4562-4bff-8e92-e420df9b2e9c
ac1daa36-6382-493c-914f-a55d934c3a06 F9GDNNC8Q1GC da75d1f7-7945-4cb1-9618-51f914651ea6

CodePudding user response:

The Object[] in the result CSV means that there are multiple values found per DisplayName (i.e.: iPad-PER-Students-ML03 has multiple appearances in the big CSV) hence why there are multiple serials and multiple objectIDs. This would require 2 nested loops to unroll all the data:

  1. An outer loop to enumerate all rows of the Azure Groups CSV.
  2. And, an inner loop to enumerate all rows of the Big CSV per appearance of DisplayName / NewGroup.
$azureGroups = Import-Csv 'this\is\theAZUREGROUPS.csv'
$bigCsvHash = Import-Csv 'this\is\theBIGcsv.csv' |
    Group-Object NewGroup -AsHashTable -AsString

$result = foreach($i in $azureGroups) {
    foreach($z in $bigCsvHash[$i.DisplayName]) {
        [pscustomobject]@{
            Serial         = $z.Serial
            ObjectID       = $z.ObjectId
            AzureADGroupID = $i.ObjectId
        }
    }
}

$result | Export-Csv ....
  • Related