【问题标题】:Unable to format Powershell foreach output and attach it to email body and subject无法格式化 Powershell foreach 输出并将其附加到电子邮件正文和主题
【发布时间】:2019-12-26 07:10:55
【问题描述】:

如果从给定列表中禁用了 Windows 计划任务,我正在计划脚本,然后向我发送电子邮件,其中包含电子邮件主题中的任务名称和正文中的任务信息。

但我无法格式化电子邮件正文并将禁用的任务名称添加到主题中。

请帮我格式化电子邮件正文并将禁用的任务名称添加到主题。

这是我的脚本-

$tasknamelist= Import-Csv "C:\Documents\task.csv"

foreach ($task in $tasknamelist) {
 $service=Get-ScheduledTask -TaskName "$taskname" | select -ExpandProperty State | Out-String
    if ($task.State -eq "Disabled") {
    $Body ="$service is not running"
    }
else {

Write-Host "$Body Task is enabled" | Out-Null

}

} $Body
$From = "xxx@outlook.com"
$To = "xxx@outlook.com"
$Cc = "xxxx@outlook.com"
#$Attachment = "C:\temp\Some random file.txt"
$Subject = ""
$Body = "$Body"
$computer = $env:computername
$SMTPServer = "outlook.office365.com"
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Cc $Cc -Subject "Task Scheduler is disabled on $computer" `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential $cred

【问题讨论】:

  • 三个结尾的反引号有什么用?您无需在续行中添加它们。
  • 在发这个问题的时候格式化代码时添加的。
  • 并且端口参数接受int32值而不是字符串。
  • 电子邮件部分工作正常,唯一的问题是禁用的任务名称和信息未正确进入正文和主题。发件人: 日期:2019 年 12 月 25 日,星期三晚上 7:58 主题:DESKTOP-1 上禁用了任务计划程序 收件人: 抄送:
  • 问题是逗号。你必须通过在它前面添加一个反引号来逃避它们。

标签: powershell powershell-2.0 powershell-3.0 powershell-4.0


【解决方案1】:

在结束 foreach 循环后,您已将 $body 声明为空变量。固定代码为:

$tasknamelist = Import-Csv "C:\Documents\task.csv"

foreach ($task in $tasknamelist) {
 $service = Get-ScheduledTask -TaskName "$taskname" | select -ExpandProperty State | Out-String
    if ($task.State -eq "Disabled") {
    $Body = "$service is not running"
    }
else {

Write-Host "$Body Task is enabled" | Out-Null

}

} 
$From = "xxx@outlook.com"
$To = "xxx@outlook.com"
$Cc = "xxxx@outlook.com"
#$Attachment = "C:\temp\Some random file.txt"
$Subject = ""
$computer = $env:computername
$SMTPServer = "outlook.office365.com"
$SMTPPort = 587
Send-MailMessage -From $From -to $To -Cc $Cc -Subject "Task Scheduler is disabled on $computer" `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential $cred

而且你没有在声明变量时添加空格。

【讨论】:

    【解决方案2】:
    $tasknamelist= Import-Csv "C:\Documents\task.csv"
    
    foreach ($task in $tasknamelist) 
    {
        $service=Get-ScheduledTask -TaskName $task
        if ($service.State -eq "Disabled") 
        {
            $Body ="$service is not running"
            $From = "xxx@outlook.com"
            $To = "xxx@outlook.com"
            $Cc = "xxxx@outlook.com"
            $computer = $env:computername
            $Subject = $task + " disabled on " + $computer
            $SMTPServer = "outlook.office365.com"
            $SMTPPort = "587"
            Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential $cred
        }
        else
        {
            Write-Host "$Body Task is enabled" | Out-Null
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-26
      • 2023-02-18
      • 2020-04-23
      • 2023-03-09
      • 2015-08-30
      • 2011-04-29
      • 1970-01-01
      • 2011-06-02
      相关资源
      最近更新 更多