3

I'm having a bit of a block with part of my PowerShell script.

I have an array which contains users' email addresses and their compliance state. The same user may have multiple entries in the array as they are in multiple policies.

For example:

Username: User1, State: OK
Username: User1, State: Not OK
Username: User1, State: OK
Username: User 2, State: OK
Username: User 2, State: OK

What I need to do is merge all the entries for each user and then write their overall status to the screen. If all states are OK then report OK, but if Not OK is in any of their states report Not OK. For example:

User 1 - Not OK
User 2 - OK

Any guidance is appreciated. Below is my code:

foreach ($Listing in $FullProtectionStatus) {
    if ($listing.state -eq "compliant") {
        Write-Host $Listing.userPrincipalName "compliant"
    }
    if ($Listing.state -eq "non compliant") {
        Write-Host $Listing.userPrincipalName "not compliant" -ForegroundColor Red
    }
}
4
  • I am thinking about merging each user into a single element in the array and then having all the states in a single element for that user. So Username: User 1, State, OK,OK,Not OK and then query the state to see if Not OK exists in the string. It's just the merging part I am struggleing with Commented Feb 26, 2019 at 10:36
  • You should edit your original post and include the code there rather than add it here in the comments. As for the overall status, grouping the collection by user name and then check compliance might be one way to solve the problem. Commented Feb 26, 2019 at 10:38
  • 2
    Are your input data an array of strings, or are Username and State properties of objects? Commented Feb 26, 2019 at 11:06
  • They are properties of an object. [System.Collections.ArrayList]$FullProtectionStatus = @() Commented Feb 26, 2019 at 11:45

2 Answers 2

4

You could group the objects by username, then check if each group contains a "Not OK" state.

$FullProtectionStatus |
    Select-Object Username, State -Unique |
    Group-Object Username |
    Select-Object @{n='Username';e={$_.Name}}, @{n='State';e={
        $_.Group |
            Select-Object -Expand State |
            Sort-Object |
            Select-Object -First 1
    }}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, let me give this a blast
Thats amazing, works a treat. I will break down offline so I understand what it's doing and so I know for next time.
@Ansgar: How did you dry run this? Sometimes, I cant believe that how do you manage to get all these. :D
@RanadipDutta Nothing special. I just built a list of custom objects with the the properties from the input sample. The tricky part is to recognize things you can use to your advantage (like sorting alphabetically by State property and then picking the first result, which gives "Not OK" if there is a "Not OK", and "OK" otherwise).
@AnsgarWiechers: Appreciate your explanation. I will try it today :)
1

Your validation inside foreach needs improvement because of the two If conditions. Either there will be an else condition or an elseif . Also, iterate each user and parse through the conditions.

ForEach($Listing in $FullProtectionStatus) 
{ 
    If($listing.state -eq "compliant") 
    { 
    Write-host $Listing.userPrincipalName "compliant" 
    } 
    elseif($Listing.state -eq "non compliant") 
    { 
    Write-host $Listing.userPrincipalName "not compliant" -ForegroundColor Red 
    }
}

2 Comments

Thanks, thats fine but I will still get multiple lines per user, I am after a single OK, Not OK per user if that makes sense
Then you need to work on the logic. Clubbing all the oks are not something difficult. Then first check that all the conditions are met for a user inside a foeach loop and then create a final pscustomobject as status OK for that specific user. In that way, you will get only single ok per user

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.