Caffeinated
Caffeinated

Reputation: 12484

In Powershell, how do I print out only the data from today(or a specific date range)?

I have a shared Outlook folder, where I need to extract 6-digit numbers from Email subject lines. So I use the following script :

$Outlook = New-Object -ComObject Outlook.Application
$Namespace = $outlook.GetNameSpace("MAPI")
$SharedMB = $NameSpace.Folders | Where{$_.Name -match "FDA UFMS User Provision"}


$OtherFldr = $SharedMB.Folders | Where{$_.Name -match "Inbox"}

[datetime]$StartDate = ([datetime]::now.ToShortDateString())

$TodaysMail = @()

for($i = ($OtherFldr.Items.count - 1);$i -ge 0;$i--){
  $Current = $OtherFldr.Items.item($i)
  if($Current.senton -lt (get-date $StartDate)){break}
  else { $OtherFldr.Items | %{ $RESULT=[Regex]::Match  ($_.TaskSubject, "Request\s\d{6}"); if  ($RESULT.Success){$RESULT.
Value}} | %{$Result=[Regex]::Match($_, "\d{6}"); if ($RESULT.Success){$RESULT.Value}} | Out-File C:\Temp\powerfish4.txt
-Append }
$TodaysMail += $Current
}

But this throws up a strange error to me :

enter image description here

The funny thing, is that if I simply dump all the numbers into a text file, it does not have this permission error. That is, I can run this script just fine :

$OtherFldr.Items | %{ $RESULT=[Regex]::Match($_.TaskSubject, "Request\s\d{6}"); if ($RESULT.Success)
{$RESULT.Value}} | %{$Result=[Regex]::Match($_, "\d{6}"); if($RESULT.Success){$RESULT.Value}} | Out-File C:\Temp\powerfish2.txt -Append

I'm currently trying to play with Outlook's offline-mode.

any tips appreciated , thanks

Upvotes: 0

Views: 104

Answers (1)

hysh_00
hysh_00

Reputation: 839

This is just an assumption but maybe trying to copy the entire item requires Outlook to be online.(Maybe Outlook is not caching the entire item)

$Current = $OtherFldr.Items.item($i)

I don't have a list of properties that Outlook holds as a cache but maybe just dealing with tasksubject and senton might be available in cache. Also modified the for loop to make it more simple.

$Outlook = New-Object -ComObject Outlook.Application
$Namespace = $outlook.GetNameSpace("MAPI")
$SharedMB = $NameSpace.Folders | Where{$_.Name -match "FDA UFMS User Provision"}
$OtherFldr = $SharedMB.Folders | Where{$_.Name -match "Inbox"}

[datetime]$StartDate = ([datetime]::now.ToShortDateString())

ForEach($Item in  $OtherFldr.Items){
  if($Item.senton -lt (get-date $StartDate)){break} 

    if($Item.TaskSubject -Match "Request\s\d{6}") 
    {
       $Result=[Regex]::Match($Item.TaskSubject, "\d{6}"); 
       if($RESULT.Success){
          $RESULT.Value | Out-File C:\Temp\powerfish4.txt -Append
       } 
    }
}

Upvotes: 1

Related Questions