Windows 成功执行powershell脚本后发送邮件
因此,我创建了以下脚本:Windows 成功执行powershell脚本后发送邮件,windows,email,powershell,Windows,Email,Powershell,因此,我创建了以下脚本: $target = "c:\$(get-date -F 'yyyy-MM')" if (!(Test-Path $target)) {md $target} gci 'c:\test\' -Filter *.xml -recurse | ?{!$_.PSIsContainer -and $_.CreationTime -ge (get-date "01.$((get-date).Month)")} | copy-item -Destination $target -Fo
$target = "c:\$(get-date -F 'yyyy-MM')"
if (!(Test-Path $target)) {md $target}
gci 'c:\test\' -Filter *.xml -recurse | ?{!$_.PSIsContainer -and $_.CreationTime -ge (get-date "01.$((get-date).Month)")} | copy-item -Destination $target -Force
有人能告诉我如何在成功执行此脚本后向特定地址发送电子邮件的正确方向吗?如果您正在使用outlook,下面是一个示例
[system.reflection.assembly]::loadwithpartialname("Microsoft.office.interop.outlook")
$OL=New-Object -ComObject OUTLOOK.APPLICATION
$ns =$OL.GETNAMESPACE("MAPI")
$mail =$ol.createitem(0)
$mail.recipients.add("something@live.nl")
$mail.CC=("something@ziggo.nl")
$MAIL.Subject= "$shortname"
#gets a signature
$mail.htmlbody= get-content $env:appdata\Microsoft\Signatures\*.txt
#for attachments..
$mail.Attachments.Add($file.FullName)
$lines=get-content $filename
$lines+=get-content $env:appdata\Microsoft\Signatures\*.txt
clear-variable text
foreach ($line in $lines)
{
$text += $line + "`r`n"
}
$mail.body= $text
$mail.display()
正如arco444在其评论中所指出的,只要您有可访问的SMTP服务器,它将允许您从运行Powershell v2或更高版本的任何计算机发送电子邮件。您可以在Powershell中尝试以下解决方案
$EmailFrom = "someone1@company.com"
$EmailTo = "someone2@company.com"
$ccUsers="someone3@company.com"
$SMTPServer = "smtp.office365.com"
$port=587
$smtp = New-Object Net.Mail.SmtpClient($SMTPServer,$port)
$smtp.EnableSsl = "true"
$smtp.Credentials =New-Object System.Net.NetworkCredential($userName, $password);
$msg = New-Object Net.Mail.MailMessage
$msg.From = $EmailFrom
$msg.To.Add($EmailTo)
$msg.CC.Add($ccUsers)
$msg.IsBodyHTML = $true
$msg.Subject = "Test Subject";
#if you want to send some html in your email
$html = "<html><body><table width='100%'><tr bgcolor='#CCCCCC'><td height='25' align='center'> <font face='tahoma' color='#003399' size='4'><strong>Test Email from PowerShell</strong></font></td> </tr> </table></body></html>" $msg.Body = $html; $smtp.Send($msg);
get help send mailmessageHi,此脚本在windows server 2012r2上运行,应向内部exchange服务器发送邮件。如何将其添加到脚本中?完全不需要仅为从PowerShell发送电子邮件而创建对Outlook的依赖关系。我已将此剪贴添加到前面提到的脚本中,它似乎可以工作:$PSEmailServer=mailserver.internal.lan send MailMessage-toname@domain.com-来自TESTUSER1-主体测试主体-主体测试主体