Powershell 搜索与关键字匹配的文件,然后将其附加到电子邮件
我编写了一个PowerShell脚本,它在文件夹中搜索与关键字匹配的文件,例如Powershell 搜索与关键字匹配的文件,然后将其附加到电子邮件,powershell,Powershell,我编写了一个PowerShell脚本,它在文件夹中搜索与关键字匹配的文件,例如Japan,然后将该文件作为附件添加到电子邮件中 电子邮件发送正确,但文件未附加 Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -ErrorAction SilentlyContinue $dir = "C:\Users\user\Desktop" $file = Get-ChildItem -Path $dir | -Filter "$ke
Japan
,然后将该文件作为附件添加到电子邮件中
电子邮件发送正确,但文件未附加
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -ErrorAction
SilentlyContinue
$dir = "C:\Users\user\Desktop"
$file = Get-ChildItem -Path $dir | -Filter "$keyword" -Recurse | Select-Object
$keyword = "Japan"
$mailboxdata = (Get-MailboxStatistics | select DisplayName,TotalItemSize,TotalDeletedItemSize, ItemCount, LastLoggedOnUserAccount, LastLogonTime)
$mailboxdata | Export-Csv "$file"
$smtpServer = "192.168.1.100"
$att = New-Object Net.Mail.Attachment($file)
$msg = New-Object Net.Mail.MailMessage
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$msg.From = "email@mail.com"
$msg.To.Add("email@othermail.com")
$msg.Subject = "Notification from email server"
$msg.Body = "Attached is the email server mailbox report for Japan"
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()
您修改了来自另一个源(从Microsoft Exchange发送邮箱统计信息)的脚本,并保留了其中不需要的部分
$dir = "C:\Users\user\Desktop"
$keyword = "Japan"
$smtpServer = "192.168.1.100"
$file = Get-ChildItem -Path $dir -Filter "*$keyword*" -Recurse
$att = New-Object Net.Mail.Attachment($file)
$msg = New-Object Net.Mail.MailMessage
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$msg.From = "email@mail.com"
$msg.To.Add("email@othermail.com")
$msg.Subject = "Notification from email server"
$msg.Body = "Attached is the email server mailbox report for Japan"
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()
我会使用
sendmailmessage
,因为它的语法更易于使用:
$dir = "C:\Users\user\Desktop"
$keyword = "Japan"
$Attachment = Get-ChildItem -Path $dir -Filter "*$keyword*" -Recurse
$From = "email@mail.com"
$To = "email@othermail.com"
$Subject = "Files matching: $keyword"
$Body = "Attached is the file for: $keyword"
$SMTPServer = "192.168.1.100"
Send-MailMessage -From $From -To $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Attachments $Attachment.FullName
您修改了来自另一个源(从Microsoft Exchange发送邮箱统计信息)的脚本,并保留了其中不需要的部分
$dir = "C:\Users\user\Desktop"
$keyword = "Japan"
$smtpServer = "192.168.1.100"
$file = Get-ChildItem -Path $dir -Filter "*$keyword*" -Recurse
$att = New-Object Net.Mail.Attachment($file)
$msg = New-Object Net.Mail.MailMessage
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$msg.From = "email@mail.com"
$msg.To.Add("email@othermail.com")
$msg.Subject = "Notification from email server"
$msg.Body = "Attached is the email server mailbox report for Japan"
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()
我会使用
sendmailmessage
,因为它的语法更易于使用:
$dir = "C:\Users\user\Desktop"
$keyword = "Japan"
$Attachment = Get-ChildItem -Path $dir -Filter "*$keyword*" -Recurse
$From = "email@mail.com"
$To = "email@othermail.com"
$Subject = "Files matching: $keyword"
$Body = "Attached is the file for: $keyword"
$SMTPServer = "192.168.1.100"
Send-MailMessage -From $From -To $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Attachments $Attachment.FullName
将
$keyword=“Japan”
移到$file=[…]
将$keyword=“Japan”
移到$file=[…]
上方我会删除-Filter
$Attachment=Get ChildItem-Path$dir-Filter“$keyword”前面的管道符号-递归此操作仍然无效,我无法计算出来您不需要任何这些行,您加载的管理单元用于管理Exchange,不需要发送电子邮件。您还从过滤器中删除了两个星号。你真的试过我的例子吗?如果是,您收到了什么错误消息?发送邮件消息:找不到文件“C:\WINDOWS\system32\Janpan.xlsx”。在C:\Users\Users\Documents\Projects\u user\Email24try.ps1:12字符:1+发送邮件-从$From-到$To-Subject$Subject-Body$Body-S…+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~CategoryInfo:NotSpecified:(:)[Send MailMessage],FileNotFoundException+FullyQualifiedErrorId:System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.SendmailMessage似乎正在查找文件的脚本所在文件夹,而不是目录。我要删除-Filter
$Attachment=Get ChildItem-Path$dir-Filter“$keyword”前面的管道符号-递归此操作仍然无法工作,我无法确定您不需要任何这些行,您加载的管理单元用于管理Exchange,不需要发送电子邮件。您还从过滤器中删除了两个星号。你真的试过我的例子吗?如果是,您收到了什么错误消息?发送邮件消息:找不到文件“C:\WINDOWS\system32\Janpan.xlsx”。在C:\Users\Users\Documents\Projects\u user\Email24try.ps1:12字符:1+发送邮件-从$From-到$To-Subject$Subject-Body$Body-S…+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~CategoryInfo:NotSpecified:(:)[Send MailMessage],FileNotFoundException+FullyQualifiedErrorId:System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.SendMailMessage似乎正在查找脚本所在的文件夹,而不是目录