Home > Blockchain >  Add NoteProperty and populate object for eventual CSV export
Add NoteProperty and populate object for eventual CSV export

Time:10-18

Full question below, here is the setup:

# Gets all room mailboxes
$rooms = Get-Mailbox -RecipientTypeDetails RoomMailbox -ResultSize Unlimited

if ( !$places ){
    $places = @()

    # Gets Places for Room Mailboxes
    foreach ( $room in $rooms ){
        $places  = Get-Place $room.PrimarySmtpAddress
    }
}

$populatedTags = $places | Where-Object { $_.Tags -notlike $null }

$populatedTags | Select-Object Identity, Tags

Identity                     Tags
--------                     ----
[email protected]            {Microsoft Teams Room}
[email protected]            {BYOD}
[email protected]            {Microsoft Surface Hub}
[email protected]            {Microsoft Teams Room, Microsoft Surface Hub, BYOD}

$populatedTags[3].Tags.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     ArrayList                                System.Object

So this is the code I use to obtain data. What I am trying to achieve is a CSV that would look like this:

Identity,TAGS_Microsoft Teams Room,TAGS_BYOD,TAGS_Microsoft Surface Hub
[email protected],TRUE,,
[email protected],,TRUE,
[email protected],,,TRUE
[email protected],TRUE,TRUE,TRUE

I can add the Tags as NoteProperty's by using the below code:

$uniqueTags = $places | Select-Object Tags -Unique
$tags = $uniqueTags.Tags -join ","
$tagsArray = $tags.Split( "," )
$definitiveTags = $tagsArray | Select-Object -Unique

foreach ( $definitiveTag in $definitiveTags ){
    if ( $definitiveTag -notlike $null ){
        $places | Add-Member -MemberType NoteProperty -Name "TAGS_$definitiveTag" -Value $Null
    }
}

Now I can do:

$places[0] | Select-Object Identity, TAGS_*

Identity              : [email protected]
Microsoft Teams Room  :
BYOD                  :
Microsoft Surface Hub :

Anyone have any ideas on how I populate those fields? I've been scratching my head for far too long.

CodePudding user response:

One simple way is to calculate the value of all the tag properties, and add them to each place object at the same time:

# Gets all room mailboxes
$rooms = Get-Mailbox -RecipientTypeDetails RoomMailbox -ResultSize Unlimited

# Gets Places for Room Mailboxes
$places = foreach ( $room in $rooms ){ Get-Place $room.PrimarySmtpAddress }

# get the list of all used tags
$populatedTags = $places | Where-Object { $_.Tags -notlike $null }
$uniqueTags = $populatedTags | Select-Object Tags -Unique | Where Tags -NotLike $null

# iterate over places
$result = foreach ($place in $places) {
  $entry = $place | select Identity  #, other, properties, etc

  # populate true/null values for each unique tag
  foreach ($tagname in $uniqueTags) {
    # calculate property value
    $value = if ($place.tags -contains $tagname) {'TRUE'} else {$null}
    # add property
    $entry | Add-Member -MemberType NoteProperty -Name "TAGS_$tagname" -Value $value
  }

  # add to $result
  Write-Output $entry
}

# view a table of places with any tag
$result | 
  # you cannot filter on a wildcard property, so have to awkwardly check like this
  Where {($_.psobject.Properties|where Name -like 'TAGS_*').Value -like 'TRUE'} | 
  Select Identity,TAGS_*

This is good enough for reports without much data, where the calculations are very simple.

  • Related