PowerShell 电子邮件附件:找不到文件 'D:201124131044\TRACS-20201124034849\error.log environment.log RACS-20201124034849.sterr.stdout
PowerShell Email Attachment: Could not find file 'D:\20201124131044\TRACS-20201124034849\error.log environment.log RACS-20201124034849.sterr.stdout
我正在尝试发送一封电子邮件,其中仅包含目录中存在的附件。我正在监视三组文件:error.log、environment.log 和一个 .stdout。我想检查每个文件是否存在,如果存在,则将其添加到列表中并作为附件发送出去。如果文件不存在,我不想做任何事情。我试图遍历文件列表(在 windows 中)然后如果文件存在则将它们发送出去。我的代码如下:
$PSEmailServer = "xxxxxxxxxxx"
$Password ="xxxxxxxxx"
$mypassword = ConvertTo-SecureString $Password -AsPlainText -Force
$mycredential = New-Object System.Management.Automation.PSCredential ("xxxxx", $mypassword)
$EmailSubject ="test"
$filepath ="D:201124131044\TRACS-20201124034849\"
$files = @('error.log','environment.log','TRACS-20201124034849.sterr.stdout')
$FromEmail ="xxxxxxxxxx"
if(($files | foreach{Test-path $filepath }) -contains $True){
$newfile = $filepath + $files
Send-MailMessage -From $FromEmail -To "xxxxxxx" -Subject $EmailSubject -Body "test" -Credential $mycredential -Attachments $newfile
}
else {
echo "nothing"
exit
}
当我 运行 以上时,我得到以下错误:
Send-MailMessage : Could not find file 'D:201124131044\TRACS-20201124034849\error.log environment.log TRACS-20201124034849.sterr.stdout'. At C:\Users\Downloads\powershell.ps1:22 char:1
+ Send-MailMessage -From $FromEmail -To "xxxxxxx" -Subje ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Send-MailMessage], FileNotFoundException
+ FullyQualifiedErrorId : > System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.SendMailMessage
我想我需要迭代列表以将每个文件名附加到它的文件路径,然后将其存储在列表中,然后发送电子邮件?请指教
尝试
$PSEmailServer = "xxxxxxxxxxx"
$Password ="xxxxxxxxx"
$mypassword = ConvertTo-SecureString $Password -AsPlainText -Force
$mycredential = New-Object System.Management.Automation.PSCredential ("xxxxx", $mypassword)
$EmailSubject ="test"
$attachments = Get-ChildItem "D:201124131044\TRACS-20201124034849" | Where-Object {$_.Name -match "error.log|environment.log|TRACS-20201124034849.sterr.stdout"}
$FromEmail ="xxxxxxxxxx"
if($attachments){
Send-MailMessage -From $FromEmail -To "xxxxxxx" -Subject $EmailSubject -Body "test" -Credential $mycredential -Attachments $attachements
}
else {
echo "nothing"
exit
}
您可以简单地使用 -contains
或 -in
在数组中查找文件名。
另外,我建议使用 splatting 作为 Send-MailMessage:
的参数
$filepath = "D:201124131044\TRACS-20201124034849"
$files = 'error.log','environment.log','TRACS-20201124034849.sterr.stdout'
# find files with names that can be found in the $files array
$attachments = @(Get-ChildItem -Path $filepath -File | Where-Object { $files -contains $_.Name })
# if we have any files to send
if ($attachments.Count) {
$Password = "xxxxxxxxx"
$mypassword = ConvertTo-SecureString $Password -AsPlainText -Force
$mycredential = New-Object System.Management.Automation.PSCredential ("xxxxx", $mypassword)
# use splatting for better readability
$mailParams = @{
To = "xxxxxxx"
From = "xxxxxxxxxx"
Subject = "test"
Body = "test"
Credential = $mycredential
Attachments = $attachments.FullName
SmtpServer = "xxxxxxxxxxx"
# more parameters can be added here
}
# send the email
Send-MailMessage @mailParams
}
else {
Write-Host 'Nothing to send..'
}
我正在尝试发送一封电子邮件,其中仅包含目录中存在的附件。我正在监视三组文件:error.log、environment.log 和一个 .stdout。我想检查每个文件是否存在,如果存在,则将其添加到列表中并作为附件发送出去。如果文件不存在,我不想做任何事情。我试图遍历文件列表(在 windows 中)然后如果文件存在则将它们发送出去。我的代码如下:
$PSEmailServer = "xxxxxxxxxxx"
$Password ="xxxxxxxxx"
$mypassword = ConvertTo-SecureString $Password -AsPlainText -Force
$mycredential = New-Object System.Management.Automation.PSCredential ("xxxxx", $mypassword)
$EmailSubject ="test"
$filepath ="D:201124131044\TRACS-20201124034849\"
$files = @('error.log','environment.log','TRACS-20201124034849.sterr.stdout')
$FromEmail ="xxxxxxxxxx"
if(($files | foreach{Test-path $filepath }) -contains $True){
$newfile = $filepath + $files
Send-MailMessage -From $FromEmail -To "xxxxxxx" -Subject $EmailSubject -Body "test" -Credential $mycredential -Attachments $newfile
}
else {
echo "nothing"
exit
}
当我 运行 以上时,我得到以下错误:
Send-MailMessage : Could not find file 'D:201124131044\TRACS-20201124034849\error.log environment.log TRACS-20201124034849.sterr.stdout'. At C:\Users\Downloads\powershell.ps1:22 char:1
+ Send-MailMessage -From $FromEmail -To "xxxxxxx" -Subje ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Send-MailMessage], FileNotFoundException
+ FullyQualifiedErrorId : > System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.SendMailMessage
我想我需要迭代列表以将每个文件名附加到它的文件路径,然后将其存储在列表中,然后发送电子邮件?请指教
尝试
$PSEmailServer = "xxxxxxxxxxx"
$Password ="xxxxxxxxx"
$mypassword = ConvertTo-SecureString $Password -AsPlainText -Force
$mycredential = New-Object System.Management.Automation.PSCredential ("xxxxx", $mypassword)
$EmailSubject ="test"
$attachments = Get-ChildItem "D:201124131044\TRACS-20201124034849" | Where-Object {$_.Name -match "error.log|environment.log|TRACS-20201124034849.sterr.stdout"}
$FromEmail ="xxxxxxxxxx"
if($attachments){
Send-MailMessage -From $FromEmail -To "xxxxxxx" -Subject $EmailSubject -Body "test" -Credential $mycredential -Attachments $attachements
}
else {
echo "nothing"
exit
}
您可以简单地使用 -contains
或 -in
在数组中查找文件名。
另外,我建议使用 splatting 作为 Send-MailMessage:
$filepath = "D:201124131044\TRACS-20201124034849"
$files = 'error.log','environment.log','TRACS-20201124034849.sterr.stdout'
# find files with names that can be found in the $files array
$attachments = @(Get-ChildItem -Path $filepath -File | Where-Object { $files -contains $_.Name })
# if we have any files to send
if ($attachments.Count) {
$Password = "xxxxxxxxx"
$mypassword = ConvertTo-SecureString $Password -AsPlainText -Force
$mycredential = New-Object System.Management.Automation.PSCredential ("xxxxx", $mypassword)
# use splatting for better readability
$mailParams = @{
To = "xxxxxxx"
From = "xxxxxxxxxx"
Subject = "test"
Body = "test"
Credential = $mycredential
Attachments = $attachments.FullName
SmtpServer = "xxxxxxxxxxx"
# more parameters can be added here
}
# send the email
Send-MailMessage @mailParams
}
else {
Write-Host 'Nothing to send..'
}