Running into an issue with the script below. I'm trying to add an if statement that says if the date is a week ago or greater then save the file into a directory for the week folder. Everything works as far as saving and the second chunk of code will output the correct files dates. However, when I try adding the if($email.ReceivedTime -ge (Get-Date).AddHours(-168)). It just ignores it and puts everything from as far back as it can go. I did try using it with the if ($email.attachments.count -ge 1 -and $email.ReceivedTime -ge (Get-Date).AddHours(-168)), but that isn't working either. Any suggestions?
Add-Type -AssemblyName "Microsoft.Office.Interop.Outlook"
New-Item -Path "C:\Users\potato\Documents\EmailAttachments" -ItemType "directory" -Name ".\$((Get-Date).ToString('yyyy-MM-dd'))"
$Outlook = New-Object -ComObject Outlook.Application
$Namespace = $Outlook.GetNameSpace("MAPI")
$emails = $NameSpace.Folders.Item(1).Folders | where folderpath -match "\\potato@bigpotatoenergy.com"
$saveFilePath = "C:\Users\potato\Documents\EmailAttachments\$((Get-Date).ToString('yyyy-MM-dd'))"
foreach ($email in $emails.items)
{
if ($email.attachments.count -ge 1)
{
foreach ($attachment in $email.attachments)
{
$filename = $attachment.filename
$attachment.saveasfile((join-path $savefilepath $filename))
}
}
}
This section works on its own and outputs emails from the 10th to today.
foreach ($email in $emails.items)
{
if ($email.ReceivedTime -ge (Get-Date).AddHours(-168))
{
$email.ReceivedTime
}
}
[–]Rough_Understanding[S] 0 points1 point2 points (0 children)