简体   繁体   中英

Creating new PsObject

I must be doing something wrong here results are empty, I tried converting html with PsObject it requires -Append that creates multiple html tables and not suited to send an email, any help appreciated.

Foreach($sender in $senders){

    $users=Get-TransportServer|Get-MessageTrackingLog  -Start (Get-Date).AddHours(-4) -ResultSize Unlimited -Sender $sender.PrimarySmtpAddress |?{$_.Recipients -notlike "*@domain.us" -and $_.RecipientCount -eq "1" -and $_.RecipientStatus -notlike "*,*" -and $_.eventid -eq 'RECEIVE' } 
    }
    $users | % {
    $t = New-Object PSObject -Property @{
        Sender = $_.Sender
        Receiver = $_.Recipients
        Messagesubject=$_.Messagesubject
        RecipientCount =$_.RecipientCount 
        TimeStamp=$_.TimeStamp
      }
      $outtbl += $t
    }
    $outtbl 

Why do you need $users , $outtbl , or $t ?

foreach ( $sender in $senders ) {
  Get-TransportServer |
    Get-MessageTrackingLog -Start (Get-Date).AddHours(-4) -ResultSize Unlimited -Sender $sender.PrimarySmtpAddress |
      Where-Object { ($_.Recipients -notlike "*@domain.us") -and
      ($_.RecipientCount -eq 1) -and
      ($_.RecipientStatus -notlike "*,*") -and
      ($_.eventid -eq 'RECEIVE') } | ForEach-Object {
      [PSCustomObject] @{
        Sender = $_.Sender
        Receipients = $_.Recipients
        MessageSubject = $_.MessageSubject
        RecipientCount = $_.RecipientCount
        TimeStamp = $_.TimeStamp
      }
    }
}

(Not tested - this is just an example of how to eliminate unnecessary variables and write clearer code.)

This code sample requires PowerShell 3.0 or newer because it uses [PSCustomObject] .

This was it

foreach ( $sender in $senders ) {
  Get-TransportServer |
    Get-MessageTrackingLog -Start (Get-Date).AddHours(-4) -ResultSize Unlimited -Sender $sender.PrimarySmtpAddress |
      Where-Object { ($_.Recipients -notlike "*@domain.us") -and
      ($_.RecipientCount -eq 1) -and
      ($_.RecipientStatus -notlike "*,*") -and
      ($_.eventid -eq 'RECEIVE') } | ForEach-Object {
        $results += New-Object PSObject -Property @{
        Sender = $_.Sender
        Receiver = $_.Recipients
        MessageSubject = $_.MessageSubject
        RecipientCount = $_.RecipientCount
        TimeStamp = $_.TimeStamp
      }
    }


    }
$results| ConvertTo-Html -Head $style| Out-File $reportpath

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM