arrays - Powershell Nested objects filtering empty values -


the following workflow generates list of printers on server:

workflow get-printersinstalledhc {     param (         [string[]]$servers     )     foreach -parallel ($s in $servers) {         inlinescript {             $s = $using:s             write-verbose "server: $s"              try {                 if ($printers = get-printer -computername $s -full -ea stop) {                     $params = @{                         computername = $s                         property     = '*'                         erroraction  = 'stop'                     }                      $config = get-ciminstance @params -classname win32_printerconfiguration                     $ports = get-ciminstance @params -classname win32_tcpipprinterport                      foreach ($p in $printers) {                         write-verbose "printer: $($p.name)"                         foreach($c in $config) {                             if ($p.name -eq $c.name) {                                 $porthostaddress = $ports | {$_.name -eq $p.portname} |                                                          select -expandproperty hostaddress                                 $prop = @{                                     porthostaddress     = $porthostaddress                                     driverversion       = $c.driverversion                                     collate             = $c.collate                                     color               = $c.color                                     copies              = $c.copies                                     duplex              = $c.duplex                                     papersize           = $c.papersize                                     orientation         = $c.orientation                                     printquality        = $c.printquality                                     mediatype           = $c.mediatype                                     dithertype          = $c.dithertype                                     retrievaldate       = (get-date -format 'dd/mm/yyyy hh:mm')                                 }                                 $p | add-member -notepropertymembers $prop -typename noteproperty                                 break                             }                         }                     }                     [pscustomobject]@{                         computername   = $s                         computerstatus = 'ok'                         retrievaldate  = (get-date -format 'dd/mm/yyyy hh:mm')                         printers       = $printers                     }                 }             }             catch {                 if (test-connection $s -count 2 -ea ignore) {                     [pscustomobject]@{                         computername   = $s                         computerstatus = "error: $($error[0].exception.message)"                          retrievaldate  = (get-date -format 'dd/mm/yyyy hh:mm')                         printers       = $null                     }                 }                 else {                     [pscustomobject]@{                         computername   = $s                         computerstatus = 'offline'                         retrievaldate  = (get-date -format 'dd/mm/yyyy hh:mm')                         printers       = $null                     }                 }             }         }     } } $printers = get-printersinstalledhc server1 

what having trouble filtering out empty values in $printers.printers.porthostaddress. discussed here should suffice do:

$printers.printers.porthostaddress | {$_} | % -begin{$i = 0} -process{$i; $_; $i++} 

other attempts not working either, as:

$printers.printers.porthostaddress | {$_ -ne $null} | % -begin{$i = 0} -process{$i; $_; $i++} $printers.printers.porthostaddress | {$_ -ne ''} | % -begin{$i = 0} -process{$i; $_; $i++} 

unfortunately, when printer's name example microsoft xps document writer , value in porthostaddress stays empty, it's still not filtered out of list can see numbers.

is there way filter out porthostaddress when there no value?


Comments