if else report data not correct

$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”}
{$Iso365 = “FALSE”}
{$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?

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 

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

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:

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


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

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.

That’s what I meant. :wink: 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'

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

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!

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:

My second which is the same type of data just different comes out like:

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?

You know that the order of the columns does not matter for Powershell or for functionality, don’t you? :wink: 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. :wink: It’ll just gonna be a little quirky when you have some more - many more - columns to deal with.