如何在文件夹

时间:2016-05-03 12:58:52

标签: csv powershell attachment

我正在尝试编写一个脚本,该脚本在文件夹中查找最新文件并通过电子邮件发送给用户,运行以下脚本后我没有收到任何错误,但我没有收到任何电子邮件。不确定如何检查文件夹中的最新文件。

$outlook = New-Object -comObject Outlook.Application 
$message = $outlook.CreateItem(0)
$message.Recipients.Add("test@test.com")  #obviously this is not the right   email
$message.Subject = "test"  
$message.Body = "this is test email"


$file = "P:\test\test.csv"
$message.Attachments.Add($file)

您好我已尝试在互联网上搜索并整理一个脚本以列出最新文件现在我只需要附加文件并通过电子邮件发送

$dir = "P:\Source\"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending   
| Select-Object -First 1
$newfile = $latest.name



$FilePath = Join-Path $dir $newfile
$FileExists =  test-path $FilePath 

If ($FileExists -eq $True)

{email bit should come here}

不确定如何发送发送消息(我是PowerShell的新手)

1 个答案:

答案 0 :(得分:0)

我所有的工作如下

$dir = "p:\test\"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending 
| Select-Object -First 1
$newfile = $latest.name



$FilePath = Join-Path $dir $newfile
$FileExists =  test-path $FilePath 

If ($FileExists -eq $True) {
$outlook = New-Object -comObject Outlook.Application 
$message = $outlook.CreateItem(0)
$message.Recipients.Add("test@test.com")  #obviously this is not the right    
email
$message.Subject = "test"  
$message.Body = "this is test email"


$file = $FilePath
$message.Attachments.Add($file)
$message.Send()
Write-Host "File is emailed "
}
else
{write-host "No File Found"}