Need Help with Custom Object Output

I want to review the maximum access level that a person with title X has based on the roles assigned to all people with title x.

I tried most of the weekend without success to tackle this one!

Here’s what I have

foreach($key in $titlehash.keys) {
$val = $titlehash[$key]
$Title = $null

#Write-output $val
#Write-Output $val."Job Title"
#Write-Output $val.role

if($val."job title".GetType() -match 'System.object') {
$Title = New-Object psobject
$Title | Add-Member -MemberType NoteProperty -Name "$($val."Job Title"[0])" -Value $ref #moved this
Write-host $val."job title"[0] -ForegroundColor yellow
$val."role" | select -Unique
$roles = ($val."role" | select -Unique)

write-output $roles.count -Verbose

#write-verbose "$roles found" -Verbose

foreach($role in $roles) {
[int]$count = $count + 1 #lazy way to count how many time I've gone through the loop for a title
#write-verbose "Editing $role" -Verbose

#Update Hashtable
$Title | Add-Member -MemberType NoteProperty -Name "$($role)" -Value $import.$role

#$Title | Out-GridView
if($count -eq $roles.count)
{write-host "$count matches!" -ForegroundColor Green
$count = $null
#$Title #| Format-Table | Out-File $env:USERPROFILE\desktop\role\TESTING123.txt # ConvertTo-Json | Out-File $env:USERPROFILE\desktop\role\TESTING123.json #| Export-Csv $env:USERPROFILE\desktop\role\TESTING123.csv
#$Title | ConvertTo-Json | out-file $env:USERPROFILE\desktop\role\$($val."job title"[0])_sum.json -Force #closest I have come so far

#I am now pivoting to work with strings directly where I just use the role names to query the master list rather than build it each time.

pause #I used this to give me a stopping point where I had a title and multiple roles in variables to work with outside of the script loop
}

}

}

else {
#Single values whose type is different
write-host $val."job title".ToString() -ForegroundColor Red
$val."role"
#someday these should export too
}

}

#$import | select permission, "$($roles[0])"
#$import | select permission, "$($roles[0..$role.count])"
<#switch ($count)

{
$roles.count {Write-Output "'$roles[$($roles.count)]'"}
0 {Write-Output "permission"}
($count -lt $roles.count) {write-output "'$roles[$($roles.count)], '"}
}

#>
<#
$stringmanipulation =
foreach($try in $roles) {
if($try -eq $roles[$($roles.count -1)]) { write-output "`'$try`'"} else{
write-output `'$try`'', '}
}
#>

This is an example of desired output by title, I think I should be able to figure out the comparison piece afterwards - where I select the maximum access level into a new “max” column.

I created this by exiting my script at the first multi-role title and typing this:

$import | select permission, “$($roles[0])”, “$($roles[1])” | Export-Csv $env:userprofile\Desktop\role$($val.“job title”[0]).csv

 

#TYPE Selected.System.Management.Automation.PSCustomObject
Permission Director Client Relations Practice Director
Accounts None None
Accounts Payable Register None None
Accounts Receivable Register View None
Bank Account Registers None None
Billing Schedules None None
Bins None None
Calendar Full Full
Campaign History Edit Edit
Cases Edit Edit

You provided the code that you are using trying to parse and the expected output, but not what you are starting or parsing. Making an assumption that you are trying to reformat Powershell output for reporting.

#Create a mock object
$object = @()
$object += [pscustomobject]@{
    Title = 'Director Client Relations'
    Permission = 'Accounts'
    PermissionLevel = 'Edit'
}
$object += [pscustomobject]@{
    Title = 'Director Client Relations'
    Permission = 'Billing'
    PermissionLevel = 'Full'
}
$object += [pscustomobject]@{
    Title = 'Director Client Relations'
    Permission = 'Cases'
    PermissionLevel = 'Edit'
}
$object += [pscustomobject]@{
    Title = 'Practice Director'
    Permission = 'Cases'
    PermissionLevel = 'Edit'
}

#Get all unique roles
$roles = $object | Select-Object -ExpandProperty Permission -Unique
#Group the object by titles
$grpObject = $object | Group-Object -Property Title
 
$results = foreach ( $role in $roles ) { #foreach row
    $props = @{}
    $props.Add('Permission', $role)
    foreach ( $grp in $grpObject ) { #foreach column
        #Use the grouping name (e.g. Title) and do a lookup on the role to get a permission level
        $props.Add($grp.Name,  ($grp.Group | Where{$_.Permission -eq $role} | Select -ExpandProperty PermissionLevel) )       
    }
    
    New-Object -TypeName PSObject -Property $props

}

$results

First a basic object to work with:

Title                     Permission PermissionLevel
-----                     ---------- ---------------
Director Client Relations Accounts   Edit           
Director Client Relations Billing    Full           
Director Client Relations Cases      Edit           
Practice Director         Cases      Edit     

Then we group the object by title:

PS C:\WINDOWS\system32> $grpObject

Count Name                      Group                                                                                                                                                                                                                            
----- ----                      -----                                                                                                                                                                                                                            
    3 Director Client Relations {@{Title=Director Client Relations; Permission=Accounts; PermissionLevel=Edit}, @{Title=Director Client Relations; Permission=Billing; PermissionLevel=Full}, @{Title=Director Client Relations; Permission=Cases; PermissionL...
    1 Practice Director         {@{Title=Practice Director; Permission=Cases; PermissionLevel=Edit}}                                                                                                                                                             

Now you get the results after we flip the columns around:

PS C:\WINDOWS\system32> $results

Permission Director Client Relations Practice Director
---------- ------------------------- -----------------
Accounts   Edit                                       
Billing    Full                                       
Cases      Edit                      Edit             

If this isn’t what you’re asking, please provide a CSV or something that we can use to see the current state to assist in getting to the completed state:

"Title","Permission","PermissionLevel"
"Director Client Relations","Accounts","Edit"
"Director Client Relations","Billing","Full"
"Director Client Relations","Cases","Edit"
"Practice Director","Cases","Edit"

Thank-you Rob this worked perfectly!