Param
(
[Parameter(Mandatory = $true,Position=0)][string]$userName,
[Parameter(Mandatory = $true,Position=1)][string]$password
)
$ErrorActionPreference = 'Stop'
$1hour = New-TimeSpan -Minutes 1
$password = [System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($password))
$userName = [System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($userName))
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential($userName, $securePassword)
$session = New-PSSession -ComputerName localhost -Credential $Credentials
$incoming = [int](Invoke-Command -Session $session -ScriptBlock {Get-NspMessageTrack -Age $USING:1hour -Directions FromExternal -CountOnly})
$outgoing = [int](Invoke-Command -Session $session -ScriptBlock {Get-NspMessageTrack -Age $USING:1hour -Directions FromLocal -CountOnly})
$DeliveryPending = [int](Invoke-Command -Session $session -ScriptBlock {Get-NspMessageTrack -Age $USING:1hour -Status DeliveryPending -CountOnly})
$inboundRejects = [int](Invoke-Command -Session $session -ScriptBlock {Get-NspMessageTrack -Age $USING:1hour -Directions FromExternal -Status PermanentlyBlocked, TemporarilyBlocked -CountOnly})
$localRejects = [int](Invoke-Command -Session $session -ScriptBlock {Get-NspMessageTrack -Age $USING:1hour -Directions FromLocal -Status PermanentlyBlocked, TemporarilyBlocked -CountOnly})
$inboundDeliveryFailure = [int](Invoke-Command -Session $session -ScriptBlock {Get-NspMessageTrack -Age $USING:1hour -Directions FromExternal -Status DispatcherError -CountOnly})
$outboundDeliveryFailure = [int](Invoke-Command -Session $session -ScriptBlock {Get-NspMessageTrack -Age $USING:1hour -Directions FromLocal -Status DispatcherError -CountOnly})
$NDRsSendTotalDE = [int](Invoke-Command -Session $session -ScriptBlock {Get-NspMessageTrack -Subject "unzustellbar:*" -CountOnly})
$NDRsSendTotalEN = [int](Invoke-Command -Session $session -ScriptBlock {Get-NspMessageTrack -Subject "undeliverable:*" -CountOnly})
Remove-PSSession -Session $session
$totalTraffic = $incoming + $outgoing
$totalRejects = $inboundRejects + $localRejects
$NDRsSendReceived = $NDRsSendTotalDE + $NDRsSendTotalEN
$totalDeliveryFailed = $inboundDeliveryFailure + $outboundDeliveryFailure
#$totalDeliveryFailed = 307
$totalRejectRate = 0
$inboundRejectRate = 0
$localRejectRate = 0
if($totalTraffic -ne 0) {
$totalRejectRate = [Math]::Round($totalRejects / $totalTraffic * 100,0)
if($incoming -ne 0) {
$inboundRejectRate = [Math]::Round($inboundRejects / $incoming * 100,0)
}
if($outgoing -ne 0) {
$localRejectRate = [Math]::Round($localRejects / $outgoing * 100,0)
}
}
$Data = @{
# TotalMailTraffic = $totalTraffic
IncomingMailTraffic = $incoming
OutgoingMailTraffic = $outgoing
DeliveryPendingTotal = $DeliveryPending
# TotalRejectedMails = $totalRejects
IncomingRejectedMails = $inboundRejects
InboundDeliveryFailed = $inboundDeliveryFailure
# TotalDeliveryFailed = $totalDeliveryFailed
OutboundDeliveryFailed = $outboundDeliveryFailure
NDRsSendReceivedTotal = $NDRsSendReceived
OutgoingRejectedMails = $localRejects
# TotalRejectRate = $totalRejectRate
# IncomingRejectRate = $inboundRejectRate
# OutgoingRejectRate = $localRejectRate
}
$Data | ConvertTo-Json -Compress
$Data | ConvertTo-Json | out-file C:\temp\test.json