简体   繁体   English

Powershell邮件脚本

[英]Powershell mail message script

I'm having a problem with a script. 我的脚本有问题。

Every night, the server task scheduler runs a batch that creates a log on which PCs it couldn't update. 每天晚上,服务器任务计划程序都会运行一批,在无法更新的PC上创建一个日志。 This PS script then pulls the error log and sends an email notifying us of the PCs. 然后,此PS脚本提取错误日志并发送电子邮件,通知我们有关PC的信息。 The error log file is a .txt file and the name of the file changes everyday to reflect the date, for example, 10172016.txt. 错误日志文件是.txt文件,并且文件名每天都会更改以反映日期,例如10172016.txt。

I can get the script to email and it used to pull the log file and place it into its body; 我可以将脚本发送到电子邮件中,并用于提取日志文件并将其放入正文中。 but now, it's blank and shows an error regarding "+". 但现在为空白,并显示有关“ +”的错误。

Code: 码:

clear

$smtpserver = "test.test.org"

$from="noreply@test.org"

$to="adamh@test.org"

$subject="Update Error Log for" + " " + (get-date -format d)

$message = (Get-Content -path "\\testserver\Departments\IT\Private\test\logs" + "(get-date -f mmddyyyy)" + ".txt" | Out-String )

$mailer = new-object Net.Mail.SMTPclient($smtpserver)

$msg = new-object Net.Mail.MailMessage($from,$to,$subject,$body)

$msg.IsBodyHTML = $false

$mailer.send($msg)

Please help. 请帮忙。

There is (at least) a problem with your code, namely (command simplified): 您的代码存在(至少)一个问题,即(简化的命令):

Get-Content -path "\path\to\" + "(get-date -f MMddyyyy)" + ".txt"

It needs to be (note the enclosing parentheses and the absence of double quotes around the get-date command): 它必须是(请注意括号和get-date命令周围没有双引号):

Get-Content -path ("\path\to\" + (get-date -f MMddyyyy) + ".txt")

PowerShell has two fundamental parsing modes - argument mode and expression mode , and understanding which is applied when is important : see Get-Help about_Parsing . PowerShell具有两种基本的解析模式 - 参数模式表达模式 ,以及了解在重要时应用的 模式 :请参阅Get-Help about_Parsing

In short: Without (...) , PowerShell sees "\\path\\to\\" + (get-date -f MMddyyyy) + ".txt" as 5 separate arguments rather than a single, synthesized string. 简而言之:如果不使用(...) ,PowerShell "\\path\\to\\" + (get-date -f MMddyyyy) + ".txt"视为5个单独的参数,而不是单个合成字符串。


Alternatively, you could use a single double-quoted string with string interpolation that uses the subexpression operator, $(...) , to embed the get-date call: 或者,您可以在字符串内插中使用单个双引号字符串,该字符串使用子表达式运算符$(...)嵌入get-date调用:

Get-Content -path "\path\to\$(get-date -f MMddyyyy).txt"

Additional observations: 其他观察:

  • A native Send-MailMessage is available since PowerShell version 3. 自PowerShell版本3起提供本机Send-MailMessage

  • The format string for 2-digit months is MM , not mm (the latter is minutes ). 2位数字月份的格式字符串是MM ,而不是mm (后者是minutes )。

  • While using pseudo method syntax new-object Net.Mail.MailMessage($from,$to,$subject,$body) happens to work, it's better to avoid it, because it can mask a misconception of how New-Object works: The proper form is New-Object Net.Mail.MailMessage -ArgumentList $from,$to,$subject,$body ; 当使用伪方法语法时, new-object Net.Mail.MailMessage($from,$to,$subject,$body)可以起作用,最好避免使用它,因为它可以掩盖对New-Object工作原理的误解:适当的形式是New-Object Net.Mail.MailMessage -ArgumentList $from,$to,$subject,$body ; as you can see, because you're dealing with a cmdlet , it uses argument mode. 如您所见,因为您正在处理cmdlet ,所以它使用参数模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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