【问题标题】:How to get PowerShell to send email with file paths如何让 PowerShell 发送带有文件路径的电子邮件
【发布时间】:2014-06-28 18:03:18
【问题描述】:

我有以下脚本,我试图在其中获取通过电子邮件发送给我的一些文件的路径位置,但在将变量传递给函数时遇到问题。我收到了电子邮件,但正文是空的。

有人可以帮我吗?

谢谢,

拉马尔·托马斯

function sendMail($VirusLoc){

     Write-Host "Sending Email"

     #SMTP server name
     $smtpServer = "mailhost.maxor.com"

     #Creating a Mail object
     $msg = new-object Net.Mail.MailMessage

     #Creating SMTP server object
     $smtp = new-object Net.Mail.SmtpClient($smtpServer)

     #Email structure
     $msg.From = "me@xxxxxx.com"
     $msg.ReplyTo = "me@xxxxx.com"
     $msg.To.Add("me@xxxxx.com")
     $msg.subject = "CryptoLocker Virus Found On pcname"
     $msg.body = $VirusLoc

     #Sending email
     $smtp.Send($msg)

}


Clear-Host
$arg = ("\\mxfsa01\Supsys\Red River Wholesale-old")

Set-Location $arg

$Files = Get-ChildItem -Recurse |Where-Object {$_.name -Like "Decrypt_Instruction.txt"} | % {
     # Write-Host $_.FullName
     $VirusLoc = $_.FullName
     Write-Host $VirusLoc

     }

sendMail

【问题讨论】:

    标签: email powershell filepath


    【解决方案1】:

    您需要为函数的 VirusLoc 参数提供一个参数,例如:

    sendMain -VirusLoc $VirusLoc
    

    顺便说一句,该变量仅包含一个文件名。是否有多个文件您可能要更改此行:

    $VirusLoc = $_.FullName
    

    $VirusLoc += "`n" + $_.FullName
    

    【讨论】:

    • 谢谢基思!那行得通!有没有办法格式化它,让每个项目都在一个新行上?
    • 我试过... $Files = Get-ChildItem -Recurse | Where-Object {$_.name -Like "Decrypt_Instruction.txt"} | % { # 写入主机 $_.FullName $VirusLoc += "n", "r`n" + $_.FullName 写入主机 $VirusLoc
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    相关资源
    最近更新 更多