none
Omit null properties of a powershell cmdlet query return RRS feed

  • Question

  • Hello,

    I'm trying to parse the results of a cmdlet and was wondering if there is a way I could do it dynamically.

    If you do a get-inboxrule and return all the properties, 50% are blank depending on the how complex the creation of the rule was.  All the rules will not be the same, so the fields would change.

    Is there a way to save the results without creating an array of all the properties and then do a check on each one to see if there is a value?

    IE:

    $mbx = get-inboxrule -Mailbox test.mailbox@microsoft.com -identity "BigInboxRule"
    
    #Get-inboxrule has a bunch of properties...  I want to #just filter out all the null ones and present only the #ones that have attributes.
    
    #Am I stuck having to do something like this?
    $properties = "isvalid","mailboxownerid","stopprocessingrules","redirectto","markasread" etc. etc.. until all properties for get-inboxrule are part of an array
    
    $report = @()
    
    foreach ($entry in $properties){
          if ($mbx.entry -eq $null){
              continue}
         else{
             $report += $mbx.entry}
    

    Now, the example may not work, I just typed up a quick example, is this all that is available?  Or is that the hard way?  Any help is appreciated!


    • Edited by ibjames31 Monday, December 17, 2018 9:31 PM
    • Moved by Bill_Stewart Friday, March 15, 2019 3:21 PM Abandoned
    Monday, December 17, 2018 9:27 PM

All replies

  • $report = $properties| 
         Where-Object( $mbx.Entry} | 
         select -Expand  Entry

    What you are asking is not clear.  A mailbox rule set will not be returned if it does 't exist.


    \_(ツ)_/


    • Edited by jrv Monday, December 17, 2018 9:58 PM
    Monday, December 17, 2018 9:56 PM
  • You can do this:

    (get-inboxrule user@domain.net).PsObject.Properties | 
         Where{$_.Value} |
         select name, Value


    \_(ツ)_/

    Monday, December 17, 2018 10:02 PM
  • You can also refine the returned properties like this:

    $rules = 'isvalid', 'mailboxownerid', 'stopprocessingrules', 'redirectto', 'markasread' #etc.
    (Get-InboxRule user@domain.net).PsObject.Properties |
        Where-Object{$_.Name -in $rules -and $_.Value } |
        Select-Object name, Value


    \_(ツ)_/


    • Edited by jrv Monday, December 17, 2018 10:08 PM
    Monday, December 17, 2018 10:05 PM