How expressive can I get with a PowerShell Expression in Format-Table? -


i have following script outputs color coded folder hierarchy of user's exchange mailbox. output line in red if it's on threshold (20 mb in case) , gray if not.

#get folder size breakdown table color coding get-mailbox $username | get-mailboxfolderstatistics | ft @{         name="name"         expression=         {             $prefix=""             foreach($c in $_.folderpath.tochararray())             {                 if($c -eq '/'){$prefix+='-'}             }             if($_.foldersize -gt 20mb)             {                 $host.ui.rawui.foregroundcolor = "red"             } else             {                  $host.ui.rawui.foregroundcolor = "gray"             }             $prefix + $_.name         }     },     foldersize,     folderandsubfoldersize 

there few problems script.

if last folder processed larger 20 mb, console text remains red after runs. script assumes original console text gray. if it's not gray, i've changed user's console text. both of these easy resolve if you're not in context of format-table expression, can't life of me figure out if it's possible resolve these issues in particular case. here's gist of i've tried doesn't work. (in reality i've tried 20 different variations).

get-mailbox $username | get-mailboxfolderstatistics | ft @{         name="name"         expression=         {             $prefix=""             $originalcolor = $host.ui.rawui.foregroundcolor             foreach($c in $_.folderpath.tochararray())             {                 if($c -eq '/'){$prefix+='-'}             }             if($_.foldersize -gt 20mb)             {                 $host.ui.rawui.foregroundcolor = "red"             }             $prefix + $_.name             $host.ui.rawui.foregroundcolor = $originalcolor         }     },     foldersize,     folderandsubfoldersize 

note: purpose of compress down one-liner. know can store variable before start pipeline , restore color after pipeline finished, takes fun/aggravation out of it. i'm more curious whether or not can accomplish without altering basic structure of pipeline.

i don't think possible. essentially, every time format-table reads expression name foreground color change. format-table doesn't write out value expression immediately, can't reset color in expression.

i think you're going have wrap pipeline:

$originalcolor = $host.ui.rawui.foregroundcolor  get-mailbox $username | get-mailboxfolderstatistics | ft @{         name="name"         expression=         {             $prefix = " " * (($_.folderpath -split '/').length)             $host.ui.rawui.foregroundcolor = if($_.foldersize -gt 20mb) { "red" } else { $originalcolor }             $prefix + $_.name         }     },     foldersize,     folderandsubfoldersize  $host.ui.rawui.foregroundcolor = $originalcolor 

another option write own formatting code finds maximum size of each column uses write-host write things out:

$stats = get-mailbox $username |     get-mailboxfolderstatistics |  $namemaxwidth = 0 $sizemaxwidth = 0 $subfoldersizemaxwidth = 0 $stats | foreach-object {      if( $_.name.length -gt $namemaxwidth )     {         $namemaxwidth = $_.name.length + (($_.folderpath -split '/').length - 1)     }      $sizewidth = $_.foldersize.tostring().length     if( $sizewidth -gt $sizemaxwidth )     {         $sizemaxwidth = $sizewidth     }      $subsizewidth = $_.folderandsubfoldersize.tostring().length     if( $subsizewidth -gt $subfoldersizemaxwidth )     {         $subfoldersizemaxwidth = $subsizewidth     } }  $stats | foreach-object {     $colorparam = @{ }     if( $_.foldersize -gt 20mb )     {         $colorparam.foregroundcolor = 'red'     }      $prefix = ' ' * (($_.folderpath -split '/').length - 1)     write-host ("{0}{1,$namemaxwidth}" -f $prefix,$_.name) -nonewline @colorparam     write-host "  " -nonewline     write-host ("{0,-$sizemaxwidth}" -f $_.foldersize) -nonewline     write-host "  " -nonewline     write-host ("{0,-$subfoldersizemaxwidth}" -f $_.folderandsubfoldersize) } 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -