Search code examples
powershellexchange-serverpowershell-2.0exchange-server-2010

Simplify Get-Messagetrackinglog with a new function including MB Conversion


i want to create a custom function to simplify the get-messagetrackinglog commandlet. It's nothing complicated, but simplifies the query a little bit.

The function works correctly, but i want to convert the totalbytes to Kilobyte in the function, if desired.

function Get-ExchangeMessagetrackinglog {
.Synopsys
.Description
.Example
Get-ExchangeMessagetrackinglog -Recipient "[email protected]" -Sender "[email protected]" -Begin "01/04/2014" -Ende "05/05/2014" | select Timestamp,Sender,Recipients,Messagesubject,@{label="Kilobytes";Expression={[int]($_.totalbytes/1kb)} }| ft -auto

param( 
 [String]$ExchangeConnector = "*",
 [String]$Begin=(get-date).AddDays(-120),
 [Datetime]$Ende=(get-date -uformat "%m/%d/%y %T"),
 [String]$Recipient = "*",
 [String]$Sender = "*",
 [String]$EventID = "Receive",
 [String]$Source = "SMTP"
)

Get-Exchangeserver | `
where { $_.isHubTransportServer -eq $True -or $_.isMailboxServer -eq $True } | `
get-messagetrackinglog  -Start $Begin -End $Ende -ResultSize Unlimited | `
where-object { `
$_.recipients -like $Recipient -and `
$_.sender -like $Sender -and `
$_.EventID -eq $EventID -and `
$_.Source -like $Source -and `
$_.connectorID -like $ExchangeConnector} 
}

My Question: Is it possible to simplify the function call (.example) ? I'm not familiar in creating custom objects, but it is possible to create an totalkilobytes object.

Thanks!


Solution

  • What do you think about this?

        function Get-ExchangeMessagetrackinglog {
    
    param( 
     [String]$ExchangeConnector = "*",
     [String]$Begin=(get-date).AddDays(-120),
     [Datetime]$Ende=(get-date -uformat "%m/%d/%y %T"),
     [String]$Recipient = "*",
     [String]$Sender = "*",
     [String]$EventID = "Receive",
     [String]$Source = "SMTP"
    )
    
    #Get-Exchangeserver | where { $_.isHubTransportServer -eq $True -or $_.isMailboxServer -eq $True } | 
    $Return= get-messagetrackinglog  -Start $Begin -End $Ende -ResultSize Unlimited | where-object { $_.recipients -like $Recipient -and $_.sender -like $Sender -and $_.EventID -eq $EventID -and $_.Source -like $Source -and $_.connectorID -like $ExchangeConnector} 
    foreach ($returnvalue in $return) { $Returnvalue | add-member -MemberType Noteproperty -Name TotalKB -Value ([math]::round($returnvalue.totalbytes/ 1kb,2 )) 
                                         $Returnvalue | add-member -MemberType Noteproperty -Name TotalMB -Value ([math]::round($returnvalue.totalbytes/ 1MB,2 ))
                                       }
    $return 
    
    
    
    
    }
    Get-ExchangeMessagetrackinglog -Begin "01/05/2014" -Ende "05/05/2014" | select timestamp,totalkb,sender,recipients,messagesubject | sort totalkb | ft -auto