Author Posts

November 19, 2014 at 9:19 am

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

November 19, 2014 at 9:40 am

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.

November 19, 2014 at 9:54 am

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

November 19, 2014 at 10:03 am

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

November 19, 2014 at 10:06 am

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.)

November 19, 2014 at 10:09 am

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

November 19, 2014 at 10:15 am

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

November 19, 2014 at 10:16 am

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

November 19, 2014 at 10:21 am

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' }

November 19, 2014 at 11:01 am

Thanks
very interesting