Align to left

This topic contains 9 replies, has 2 voices, and was last updated by Profile photo of turbo mcp turbo mcp 2 years ago.

  • Author
    Posts
  • #20676
    Profile photo of turbo mcp
    turbo mcp
    Participant

    Hi there
    have a quick question
    I have a script that checks for messages send/received on servers
    pretty simple and it works fine.
    I collect all the data to a custom object(server,send,receive)
    then output it like this:
    $data |Ft -autosize
    (I tried other format table kind of views)
    the output I get is like this:

    Server Send Receive
    server1 2 5
    server2 10 15

    my question is is there away to align the numbers to the left instead of to the right?
    of course it isn't a big deal I just want to learn new things so im interested to know if
    its something simple
    Thanks

  • #20677
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Yep, you can do that, but I can't seem to find where this is documented to give you a reference:

    $properties = @(
        'Server'
    
        @{ Label = 'Send';    Expression = { $_.Send };    Alignment = 'Left' }
        @{ Label = 'Receive'; Expression = { $_.Receive }; Alignment = 'Left' }
    )
    
    $data | Format-Table -Property $properties -AutoSize
    
    

    Most people only use Name (or Label) and Expression when using the hashtable syntax for constructed properties in Format-Table, but you can also set Width and Alignment in there as well.

  • #20678
    Profile photo of turbo mcp
    turbo mcp
    Participant

    amazing stuff:)
    learn something every day and that's what I like about it
    Thanks again

  • #20679
    Profile photo of turbo mcp
    turbo mcp
    Participant

    only one other small question
    how would this fit into something like this:

    # Load psssnapin

    if (! (Get-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction:SilentlyContinue) )

    {Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction:Stop}

    $starttime=(Get-Date).addhours(-2).ToString()

    $endtime=(Get-Date).ToString()

    $Data = @()

    $servers=Get-TransportServer

    # The servers are processed

    ForEach ($server in $servers) {

    # The commands run on each server.

    $Received=($server|invoke-command {get-messagetrackinglog -resultsize unlimited -EventId "receive" -Start $starttime -End $endtime} |?{$_.ConnectorId -match "Anonymous"} |Measure-Object)

    $Sent=($server|invoke-command {get-messagetrackinglog -resultsize unlimited -EventId "send" -Start $starttime -End $endtime} |Measure-Object)

    $Data +=New-Object PSObject -Property @{

    Server = $server

    ReceivedMessages = $Received.count

    SentMessages = $Sent.count

    }

    $Data |FT -AutoSize -Wrap

  • #20680
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    I'm not sure what you want me to be focusing on, there. The only difference I see (as related to the Format-Table command) is the use of the -Wrap switch, and different property names on the input objects (ReceivedMessage and SentMessages, instead of Receive and Send.) The same idea should work; you just need to pass something to the -Property parameter of Format-Table, and the objects passed to that parameter can be hashtables which define the Alignment and/or Width of a column. (Width is pointless if you're using -AutoSize, but you get the idea.)

  • #20681
    Profile photo of turbo mcp
    turbo mcp
    Participant

    im just not sure if the properties part of the script should go inside the loop(after the custom object)
    or out of the loop

  • #20682
    Profile photo of turbo mcp
    turbo mcp
    Participant

    got it:)
    I just used it at end(out of loop)
    like this:

    $properties = @(
        'Server'
     
        @{ Label = 'Send';    Expression = { $_.sentmessages
     };    Alignment = 'Left' }
        @{ Label = 'Receive'; Expression = { $_.receivedmessages }; Alignment = 'Left' }
    )
     
    $data | Format-Table -Property $properties -AutoSize

    Thanks again
    I check this forum daily and its just amazing stuff and challenges

  • #20683
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    That can go just before the call to Format-Table; no need to put it inside a loop.

    BTW, found the documentation I was looking for: http://technet.microsoft.com/en-us/library/hh849892.aspx . It just doesn't show up when you run Get-Help Format-Table -ShowWindow, due to a slight problem with that command (only showing the first paragraph of each property's description.) If you run Get-Help Format-Table -Full , the documentation does show up in the console. In addition to the Alignment and Width keys, there's one other that I didn't mention earlier: FormatString. Example:

    $data = @(
        [pscustomobject] @{
            Date = Get-Date -Year 2008 -Month 12 -Day 31
        }
    
        [pscustomobject] @{
            Date = Get-Date -Year 2012 -Month 6 -Day 13
        }
    )
    
    Write-Verbose -Verbose 'Default date / time formatting for current culture'
    $data | Format-Table -AutoSize | Out-Host
    
    Write-Verbose -Verbose 'Specifying format string ("D" for long date pattern of current culture)'
    $properties = @{ Label = 'Date'; Expression = { $_.Date }; FormatString = 'D' }
    $data | Format-Table -Property $properties -AutoSize | Out-Host
    
    Write-Verbose -Verbose 'Specifying custom format string'
    $properties = @{ Label = 'Date'; Expression = { $_.Date }; FormatString = 'ddd, MMM d yyyy' }
    $data | Format-Table -Property $properties -AutoSize | Out-Host
    
    
  • #20684
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Another bit of useful information from that documentation... the Expression can just be a string, if all you're doing is accessing a property on the input object. No need to write { $_.Date } when just 'Date' will do:

    $properties = @{ Label = 'Date'; Expression = 'Date'; FormatString = 'ddd, MMM d yyyy' }
    
  • #20687
    Profile photo of turbo mcp
    turbo mcp
    Participant

    Thanks
    very interesting

You must be logged in to reply to this topic.