Home > Mobile >  How do I exclude a group from the output once a group has been compiled in PowerShell?
How do I exclude a group from the output once a group has been compiled in PowerShell?

Time:09-16

I am using some code I found to group a log by sessionid's then format them and display them into a new file. However, part of the fields within some groups are not available and appear in the groupings in the output as "null". How would I exclude an entire group if one of the fields contains null or incomplete group fields?

| Group sessionid | Foreach-Object {
$jsonTemplate = [pscustomobject]@{
    time = [pscustomobject]@{ start = ''; duration = '' }
    sessionid = ''
    client = '' 
    messageid = ''
    address = [pscustomobject]@{from = ''; to = ''}
    status = ''
}
$start = ($_.Group | where key -eq 'message-id').time
$end = ($_.Group | where key -eq 'status').time -as [datetime]
$jsonTemplate.time.start = $start
$jsonTemplate.time.duration = ($end - ($start -as [datetime])).ToString()
$jsonTemplate.sessionid = $_.Name
$jsonTemplate.client = ($_.Group | where key -eq 'client').data
$jsonTemplate.messageid = ($_.Group | where key -eq 'message-id').data
$jsonTemplate.address.from = ($_.Group | where key -eq 'from').data
$jsonTemplate.address.to = ($_.Group | where key -eq 'to').data
$jsonTemplate.status = ($_.Group | where key -eq 'status').data
[regex]::Unescape(($jsonTemplate | convertTo-Json))

For this output I Want it to check if any of the fields are missing information and exclude it from the end output.

CodePudding user response:

Not the most beautiful piece of code, but something like this should work

If(($_.Name -ne $Null -or $_.Name -ne "") -or (($_.Group | where key -eq 'client').data -ne $Null -or ($_.Group | where key -eq 'client').data -ne "") -or (($_.Group | where key -eq 'message-id').data -ne $Null -or ($_.Group | where key -eq 'message-id').data -ne "") -or (($_.Group | where key -eq 'from').data -ne $Null -or ($_.Group | where key -eq 'from').data -ne "") -or (($_.Group | where key -eq 'to').data -ne $Null -or ($_.Group | where key -eq 'to').data -ne "") -or (($_.Group | where key -eq 'status').data -ne $Null -or ($_.Group | where key -eq 'status').data -ne "") -or )
{
    $jsonTemplate.time.start = $start
    $jsonTemplate.time.duration = ($end - ($start -as [datetime])).ToString()
    $jsonTemplate.sessionid = $_.Name
    $jsonTemplate.client = ($_.Group | where key -eq 'client').data
    $jsonTemplate.messageid = ($_.Group | where key -eq 'message-id').data
    $jsonTemplate.address.from = ($_.Group | where key -eq 'from').data
    $jsonTemplate.address.to = ($_.Group | where key -eq 'to').data
    $jsonTemplate.status = ($_.Group | where key -eq 'status').data
    [regex]::Unescape(($jsonTemplate | convertTo-Json))
}

It checks each for null or empty

CodePudding user response:

This might work. Sorry, I don't have data to test it.

| $Groups = Group-Object -Property sessionid

# Create an empty array to fill
$Array = @()

# Loop over each group
foreach ($Group in $Groups) {
   # Create an object
   $jsonTemplate = [pscustomobject]@{
      time = [pscustomobject]@{ start = ''; duration = '' }
      sessionId = ''
      client = ''
      messageId = ''
      address = [pscustomobject]@{from = ''; to = ''}
      status = ''
   }

   # Get the group's data
   $Data = $Group.Group
   
   # messageId
   $messageId = $Data | Where-Object {$_.key -eq 'messageId'}
   if ($messageId) {
      $jsonTemplate.messageId = $messageId.data
      [datetime]$start = $messageId.Time
   } else {continue}

   # status
   $status = $Data | Where-Object {$_.key -eq 'status'}
   if ($status) {
      $jsonTemplate.status = $status.data
      [datetime]$end = $status.Time
   } else {continue}

   # sessionid
   if ($Group.Name) {$jsonTemplate.sessionid = $Group.Name} else {continue}

   # client
   $client = $Data | Where-Object {$_.key -eq 'client'}
   if ($client) {$jsonTemplate.client = $client.data} else {continue}

   # from
   $from = $Data | Where-Object {$_.key -eq 'from'}
   if ($from) {$jsonTemplate.address.from = $from.data} else {continue}

   # to
   $to = $Data | Where-Object {$_.key -eq 'to'}
   if ($to) {$jsonTemplate.address.to = $to.data} else {continue}

   # Add to array
   $Array  = $jsonTemplate
}

# Convert the array
$json = $Array | ConvertTo-Json -Depth 10

# Do stuff with the json
[regex]::Unescape($json)
  • Related