我试图从电子邮件正文中提取链接并将附件保存在该链接中。
$Folder = "InBox"
Add-Type -AssemblyName microsoft.office.interop.outlook
$Outlook = New-Object -ComObject Outlook.Application
$Namespace = $Outlook.GetNameSpace("MAPI")
$Email = $NameSpace.Folders.Item(1).Folders.Item($Folder).Items
$extracts = $Email | Where-Object {$_.subject -match "PITM*"}
$extracts | ForEach-Object {
$email = $_.Body
#$lines = $email | Measure-Object -Line
if($email -match '<(.*?)>'){
"Match Found"
$url = $matches[1]
}
$url
$output = "H:\foo.DAT"
$Process = new-Object System.Diagnostics.Process
$Process.StartInfo.WindowStyle="Hidden"
$Process.StartInfo.FileName="chrome.exe"
$Process.StartInfo.Arguments=$url
$Process.Start()
}
$Process.Dispose()
电子邮件正文如下:
请求截止日期:
完成日期:
完成日期:
附件:报告foo.txt https://a.com/sys_attachment.do?sys_id=829893456f90a60002da186e6b3ee402
以下是我遇到的两个问题:
请帮忙。
答案 0 :(得分:1)
这个怎么样?
#sample data
$email = "Number: PITM0008575
<https://a.com/nav_to.do?uri=foo%3Fsys_id=58689b056f90a60002da186e6b3ee484%26sysparm_stack=u_production_item_list.do%3Fsysparm_query=active=true>
Requested due date:
Fulfilled date:
Completed date:
Attachments: Report foo.txt
<https://a.com/sys_attachment.do?sys_id=829893456f90a60002da186e6b3ee402>"
$regex = [regex]"<(.*?)>"
$URLmatches = $regex.Matches($email)
$reportURL = $URLmatches.Groups[1].Value
$downloadURL = $URLmatches.Groups[2].Value
$output = "$env:USERPROFILE\Desktop\foo.dat"
(New-Object System.Net.WebClient).DownloadFile($downloadURL, $output)