使用包括MB转换在内的新功能简化Get-Messagetrackinglog

时间:2014-05-07 06:39:33

标签: powershell exchange-server powershell-v2.0 exchange-server-2010

我想创建一个自定义函数来简化get-messagetrackinglog命令行开关。 它没什么复杂的,但稍微简化了查询。

该函数正常工作,但我想在函数中将totalbytes转换为Kilobyte,如果需要的话。

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

我的问题: 是否可以简化函数调用(.example)? 我对创建自定义对象并不熟悉,但可以创建一个totalkilobytes对象。

谢谢!

2 个答案:

答案 0 :(得分:0)

我的答案不是创建一个新的PSCustom对象,而是修改你的并返回一个字符串return ("TotalKB: " + $totalKB)作为你的最终总数。我还将整个select Timestamp..块移动到主函数体中。

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)

}

让我知道你是如何进行的,因为它未经测试。

答案 1 :(得分:0)

您如何看待这个?

    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