Question

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 "user@tld.com" -Sender "sender@tld.com" -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!

Était-ce utile?

La solution 2

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

Autres conseils

Rather than creating a new PSCustom object my answer amends your and returns a string return ("TotalKB: " + $totalKB) as your final total. I also moved the entire select Timestamp.. block into the body of main function.

function Get-ExchangeMessagetrackinglog {
.Synopsys
.Description
.Example
Get-ExchangeMessagetrackinglog -Recipient "user@tld.com" -Sender "sender@tld.com" -Begin "01/04/2014" -Ende "05/05/2014"

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 } | `
$results = 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}

$totalKB = 0

foreach($entry in $results)  { 
    $totalKB += $entry.totalbytes
}

$totalKB = $totalKB/1kb

$results | select Timestamp,Sender,Recipients,Messagesubject,@{label="Kilobytes";Expression={[int]($_.totalbytes/1kb)} }| ft -auto

return ("TotalKB: " + $totalKB)

}

Let me know how you get on as it's untested.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top