我想发送一封电子邮件,其中包含Power Shell中Windows计划程序中设置的计划任务的更新状态。
我可以通过以下命令在Powershell中获取计划任务的更新状态:
Get-ScheduledTask -TaskPath "\" | Get-ScheduledTaskInfo | Export-Csv -NoTypeInformation -Path C:\Lakshmen\schedu
ledTasksResults.csv
这会将数据导出到csv中。相反,我希望在电子邮件中获得此信息。但是我不确定如何通过电子邮件发送。
尝试过:
Get-ScheduledTask -TaskPath "\" | Get-ScheduledTaskInfo | Send-MailMessage -To "lakesh@outlook.com" -From "lakesh@outlook.com" -Subject "ScheduledTasks" -SmtpServer "smtp.mail.outlook.com"
出现这样的错误:
Send-MailMessage : Illegal characters in path.
At line:1 char:59
+ ... dTaskInfo | Send-MailMessage -To "lakesh@outlook.com" -From ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Send-MailMessage], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.SendMailMessage
对此需要一些指导。
答案 0 :(得分:2)
一种方法是附上您的结果:
Get-ScheduledTask -TaskPath "\" | Get-ScheduledTaskInfo | Export-Csv -NoTypeInformation -Path C:\Lakshmen\scheduledTasksResults.csv
编辑是由于@LotPing的正确评论,应使用spaltting(越少越好)而不是变量(谢谢)。
然后发送邮件:
$Arguments = @{
From = "lakesh@outlook.com"
To = "lakesh@outlook.com"
Attachment = "C:\Lakshmen\scheduledTasksResults.csv"
Subject = "Here are my scheduled tasks"
Body = "See the attachments for the tasks results"
SMTPServer = "smtp.mail.outlook.com"
SMTPPort = "587"
}
Send-MailMessage @Arguments -UseSsl -Credential (Get-Credential) –DeliveryNotificationOption OnSuccess
或者您可以简单地将其放入体内:
$Arguments = @{
From = "lakesh@outlook.com"
To = "lakesh@outlook.com"
Subject = "Here are my scheduled tasks"
Body = Get-ScheduledTask -TaskPath "\" | Get-ScheduledTaskInfo | Out-String
SMTPServer = "smtp.mail.outlook.com"
SMTPPort = "587"
}
Send-MailMessage @Arguments -UseSsl -Credential (Get-Credential) –DeliveryNotificationOption OnSuccess
注意:由于Get-Credential
,它会提示您输入密码。如果您希望在没有用户交互的情况下使用它,则需要将密码存储为SecureString
。
答案 1 :(得分:1)
首先创建一个凭证对象。然后确保将对象转换为字符串以便能够发送。我个人将CSV作为附件发送,但这是一个示例:
$emailAddress = "lakesh@outlook.com"
$secpasswd = ConvertTo-SecureString "yourPassword" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("$emailAddress", $secpasswd)
$tasks = Get-ScheduledTask -TaskPath "\" | Get-ScheduledTaskInfo | Out-String
Send-MailMessage -To $emailAddress -From $emailAddress -Subject "ScheduledTasks" -SmtpServer "smtp.mail.outlook.com" -Credential $mycreds -Body $tasks