gpt4 book ai didi

powershell - 发送多行正文和多个附件

转载 作者:行者123 更新时间:2023-12-02 23:44:52 27 4
gpt4 key购买 nike

我有两个包含两个文本文件的不同文件夹,目标是使用PowerShell在电子邮件中发送两个txt文件。
第一个文件存储在日志中,另一个文件存储在错误中。我找到了一个有用的PowerShell脚本来执行此操作,但是我不确定如何在同一封电子邮件中以及在每个附件都有两行的正文中添加两个附件。以下是脚本,可以提供任何帮助:

# Date values to find related log file for the day and hour or the run
$y = Get-Date -format yyyy
$m = Get-Date -format MM
$d = Get-Date -format dd
$h = Get-Date -format 05
# Modify the log path
$LogPath = "C:\Program Files\Logs\"
$LogFile = $LogPath + "Log_" + $y + $m + $d + "_" + $h + "*.txt"
$LogFileName = $LogFile | ? {Test-Path $LogFile} | Get-ChildItem | Where-Object { $_ -is [System.IO.FileInfo] }

$LogPath1 = "C:\Program Files\ERROR\"
$LogFile1 = $LogPath1 + "Log_" + $y + $m + $d + "_" + $h + "*.txt"
$LogFileName1 = $LogFile1 | ? {Test-Path $LogFile} | Get-ChildItem | Where-Object { $_ -is [System.IO.FileInfo] }

$MessageBodyNA = "Email from scheduler. No log file found " + $LogFile
$MessageBodyA = "Email from scheduler. File" + $LogFileName + " found and attached"

$MessageBodyNA1 = "Email from scheduler. No log file found " + $LogFile1
$MessageBodyA1 = "Email from scheduler. File(s) " + $LogFileName1 + " found and attached"

$FromAddress = "test@gmail.com"

$ToAddress = "test1@gmail.com"
$Subject = "test"

$SMTPserver = "SMTPServerName.com"


if ( $LogFileName | Where-Object { $_ -is [System.IO.FileInfo] })
{
send-mailmessage -from $FromAddress -to $ToAddress -subject $Subject -body $MessageBodyA -smtpServer $SMTPserver -Attachments $LogFileName
}
else
{
send-mailmessage -from $FromAddress -to $ToAddress -subject $Subject -body $MessageBodyNA -smtpServer $SMTPserver
}

if ( $LogFileName1 | Where-Object { $_ -is [System.IO.FileInfo] })
{
send-mailmessage -from $FromAddress -to $ToAddress -subject $Subject -body $MessageBodyA1 -smtpServer $SMTPserver -Attachments $LogFileName1
}
else
{
send-mailmessage -from $FromAddress -to $ToAddress -subject $Subject -body $MessageBodyNA1 -smtpServer $SMTPserver
}

最佳答案

如有疑问,请阅读documentation-Attachment参数接受一个字符串数组,因此您可以像这样附加多个文件:

Send-MailMessage -From $FromAddress -To $ToAddress -Subject $Subject `
-Body $MessageBodyA -SmtpServer $SMTPserver `
-Attachments $LogFileName, $logFileName1

通过使用多行字符串可以轻松创建多行主体:
$MessageBody = @"
This is line 1.

This is line 2.
"@

或通过串联多行:
$line1 = "This is line 1."
$line2 = "This is line 2."

$MessageBody = "$line1`n`n$line2"

关于powershell - 发送多行正文和多个附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27487575/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com