Powershell 将输出重新定向到用于邮寄的变量

Powershell 将输出重新定向到用于邮寄的变量,powershell,Powershell,我试图使用下面提到的脚本片段从系统事件日志中获取特定事件。现在我需要将输出重定向到我的邮件。但不确定如何重新引导此输出。有人能帮我做到这一点吗 foreach ($server in $Servers){ Write-Host "Host Name :"$Server -fore "yellow" -nonewline try { Get-WinEvent -ComputerName $Server -FilterHashtable @{logname="system"; i

我试图使用下面提到的脚本片段从系统事件日志中获取特定事件。现在我需要将输出重定向到我的邮件。但不确定如何重新引导此输出。有人能帮我做到这一点吗

foreach ($server in $Servers){ 
  Write-Host "Host Name :"$Server -fore "yellow" -nonewline

  try {
    Get-WinEvent -ComputerName $Server -FilterHashtable @{logname="system"; id=1135,1038,1065,1069; StartTime="09/12/2015"} -ErrorAction Stop | fl id, LevelDisplayName, TimeCreated, ProviderName, Message
  } catch [Exception] {
    if ($_.Exception -match "No events were found that match the specified selection criteria") {
      Write-Host " :  NO EVENTS FOUND`n" -fore "Green"
    }
  }
}

$smtpServer = "smtpserver name"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

#E-mail setup
$msg.From = "roji.rajan@test.com"
$msg.To.Add("roji.rajan@test.com")
$msg.Subject = "Eventlog Report"
$msg.Body = "Hi Roji, `n 
Please find the report below.."

$smtp.Send($msg)

在使用变量时,不要考虑输出重定向-您希望分配:

然后将其附加到您的身体上:

# after first assignment to Body, but before Send()
$msg.Body += $myevents | Out-String 

在使用变量时,不要考虑输出重定向-您希望分配:

然后将其附加到您的身体上:

# after first assignment to Body, but before Send()
$msg.Body += $myevents | Out-String