在Powershell中,如何仅打印今天(或特定日期范围)的数据?

时间:2015-03-10 18:45:38

标签: powershell

我有一个共享的Outlook文件夹,我需要从电子邮件主题行中提取6位数字。所以我使用以下脚本:

$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
}

但是这给我带来了一个奇怪的错误:

enter image description here

有趣的是,如果我只是将所有数字转储到文本文件中,它就没有此权限错误。也就是说,我可以正常运行这个脚本:

$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

我目前正在尝试使用Outlook的离线模式。

任何提示赞赏,谢谢

1 个答案:

答案 0 :(得分:1)

这只是一个假设,但也许尝试复制整个项目需要Outlook在线。(也许Outlook不会缓存整个项目)

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

我没有Outlook保存为缓存的属性列表,但可能只是处理tasksubject和senton可能在缓存中可用。 还修改了for循环以使其更简单。

$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
       } 
    }
}