# if else report data not correct

**URL:** https://forums.powershell.org/t/if-else-report-data-not-correct/14066
**Category:** PowerShell Help
**Created:** [March 31, 2020, 3:54pm UTC](https://forums.powershell.org/t/if-else-report-data-not-correct/14066 "2020-03-31T15:54:20Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![lawson23](https://avatars.discourse-cdn.com/v4/letter/l/bbe5ce/32.png) [@lawson23](https://forums.powershell.org/u/lawson23)
#### Post date: [March 31, 2020, 3:54pm UTC](https://forums.powershell.org/t/if-else-report-data-not-correct/14066/1 "2020-03-31T15:54:20Z")

</div>

$FGroupMembers is a Groupmembership list of users of a AD group.

$DGroupMembers is a Groupmembership list of users of a AD group.

$O365GroupMembers is a Groupmembership list of users of a AD group.

`$finalReport = @()`  
`ForEach ($us1 in $FGroupMembers)`  
`{`  
`If ($DGroupMembers -contains $us1.ObjectGuid)`  
`{$IsEnrolled = “TRUE”`  
`If ($O365GroupMembers -contains $us1.ObjectGuid)`  
`{$Iso365 = “TRUE”}`  
`Else`  
`{$Iso365 = “FALSE”}`  
`}`  
`Else `  
`{$IsEnrolled = “FALSE”`  
`$Iso365 = “FALSE”}`

`$tempobj = @{`  
`‘Enrolled’ = $IsEnrolled`  
`‘O365’ = $IsO365`  
`‘Name’ = $us1.name`  
`‘Distinguishedname’ = $us1.Distinguishedname`  
`‘Enabled’ = $us1.enabled`  
`}`  
`$finalReport += New-Object psobject -property $tempObj`  
`}`  
`$finalReport | Where-Object ($_.enrolled -eq “TRUE”)`

My issue is in $finalReport Nothing is returning Enrolled = TRUE even if I look through the results manually and not use my where-object statement.

This is a list of about 250 users and about 230 of them should equal True.

Any ideas?

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [March 31, 2020, 4:55pm UTC](https://forums.powershell.org/t/if-else-report-data-not-correct/14066/2 "2020-03-31T16:55:54Z")

</div>

If I got you right you could _streamline_ your code a little bit like this:

```
$tmplte = @{
    Enrolled = $false
    O365 = $false
    Name = $null
    Distinguishedname = $null
    Enabled = $null
}

$ResultList = ForEach ($us1 in $FGroupMembers) {
    $HshTble = $tmplte.Clone()
    $HshTble.Name = $us1.name
    $HshTble.enabled = $us1.enabled
    $HshTble.Distinguishedname = $us1.Distinguishedname

    If ($DGroupMembers.ObjectGuid -contains $us1.ObjectGuid) {
        $HshTble.Enrolled = $true
    }
    If ($O365GroupMembers.ObjectGuid -contains $us1.ObjectGuid) { 
        $HshTble.O365 = $true 
    }
    [PSCustomObject]$HshTble
}
$ResultList
```

You use the comparison operator **-contains** in your condition statement. **-contains** looks for an element in an array of elements and it has to be a perfect match to provide a positive hit. Do your lists contain exact matches you can use for this kind of comparison? You may share some sanitized but still representative lines of your lists - formatted as code as well please.

BTW: You should use the code tags “ **PRE** ” to format your code as code. Thanks

---

<div class="post-metadata">

### Author: ![lawson23](https://avatars.discourse-cdn.com/v4/letter/l/bbe5ce/32.png) [@lawson23](https://forums.powershell.org/u/lawson23)
#### Post date: [April 1, 2020, 10:20am UTC](https://forums.powershell.org/t/if-else-report-data-not-correct/14066/3 "2020-04-01T10:20:10Z")

</div>

Olaf,  
Thank you for the code but I’m ending up with the same results as I was. It is like the contains lookup is not working because here is an example of a piece of data from the variables that I outputted to a CSV:  
$FGroupMembers

```
Name SamAccountName enabled Description objectguid DistinguishedName
Abb, Pat pabb TRUE Sr. Manager 4e4f4173-caa2-4c65-b9c7-744d55931a60 CN=Abb\, Pat,OU=Site,OU=DIV,OU=Users,DC=Domain,DC=com
```

$DGroupMembers

```
Name	SamAccountName	enabled	Description	objectguid	DistinguishedName
Abb, Pat 4e4f4173-caa2-4c65-b9c7-744d55931a60	CN=Abb\, Pat,OU=Site,OU=DIV,OU=Users,DC=Domain,DC=com
```

$O365GroupMembers  
I’m looking into what is going on with this group member collection command because it is currently not returning any members

Which explains one of the contain lookup failures.

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [April 1, 2020, 1:01pm UTC](https://forums.powershell.org/t/if-else-report-data-not-correct/14066/4 "2020-04-01T13:01:19Z")

</div>

> [@](#):
>
> It is like the contains lookup is not working …

That’s what I meant. 😉 I updated the code in my answer. If the values in “objectguid” of the according array are equal it should work now.

Here is a little snippet to illustrate what I meant:

```
$Array = 'John','George','Paul','Ringo'

$Array -contains 'John'
# Returns 'True'
$Array -contains 'Georg'
# Returns 'False'
```

---

<div class="post-metadata">

### Author: ![lawson23](https://avatars.discourse-cdn.com/v4/letter/l/bbe5ce/32.png) [@lawson23](https://forums.powershell.org/u/lawson23)
#### Post date: [April 1, 2020, 1:26pm UTC](https://forums.powershell.org/t/if-else-report-data-not-correct/14066/5 "2020-04-01T13:26:38Z")

</div>

Ok I have fixed the $O365GroupMembers and still getting the same results.

Here is the example of data for this Varible:

```
Name	SamAccountName	enabled	Description	objectguid	DistinguishedName
Abb, Pat	pabb 4e4f4173-caa2-4c65-b9c7-744d55931a60	CN=Abb\, Pat,OU=Site,OU=DIV,OU=Users,DC=Domain,DC=com
```

As you can see the Objectguid is the same for this user in all three groups and I should see True True but it just shows False False.

```
O365	Enrolled	Enabled	Name	Distinguishedname	SAMAccountName
FALSE	FALSE	TRUE	Abb, Pat	CN=Abb\, Pat,OU=Site,OU=DIV,OU=Users,DC=Domain,DC=com	pabb
```

---

<div class="post-metadata">

### Author: ![lawson23](https://avatars.discourse-cdn.com/v4/letter/l/bbe5ce/32.png) [@lawson23](https://forums.powershell.org/u/lawson23)
#### Post date: [April 1, 2020, 1:29pm UTC](https://forums.powershell.org/t/if-else-report-data-not-correct/14066/6 "2020-04-01T13:29:57Z")

</div>

Ok Ignore my last thread which I posted after you. Sorry

Using the updated code where I call the objectguid in each variable has fixed the problem!

Thank you again Olaf!

---

<div class="post-metadata">

### Author: ![lawson23](https://avatars.discourse-cdn.com/v4/letter/l/bbe5ce/32.png) [@lawson23](https://forums.powershell.org/u/lawson23)
#### Post date: [April 2, 2020, 1:15pm UTC](https://forums.powershell.org/t/if-else-report-data-not-correct/14066/7 "2020-04-02T13:15:03Z")

</div>

Olaf,  
One new question relating to this code. I have two different data sets that I’m using the SAME code on to produce a csv file. My problem is the CSV files generated come out in different column order. My first set which I originally started with comes out in the order of:  
Enrolled  
O365  
Enabled  
Name  
Distinguishedname

My second which is the same type of data just different comes out like:  
Enrolled  
Name  
O365  
Distinguishedname  
Enabled

All the same code. Is there a way to order the columns so that I get similar report out between the two reports for consistency?

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [April 2, 2020, 5:48pm UTC](https://forums.powershell.org/t/if-else-report-data-not-correct/14066/8 "2020-04-02T17:48:58Z")

</div>

> [@](#):
>
> … Is there a way to order the columns so that I get similar report out between the two reports for consistency?

You know that the order of the columns does not matter for Powershell or for functionality, don’t you? 😉 If you want to have a particular order you can use Select-Object and specify the properties you want to show in the order you want to show. Of course you can use this before you save/export the data to the csv files. 😉 It’ll just gonna be a little quirky when you have some more - many more - columns to deal with.

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:30pm UTC](https://forums.powershell.org/t/if-else-report-data-not-correct/14066/9 "2024-05-16T20:30:00Z")

</div>


