使用 powershell 从 Outlook 获取今天的约会:不需要的结果 [英] Get todays appointments from outlook with powershell: Unwanted results

查看:113
本文介绍了使用 powershell 从 Outlook 获取今天的约会:不需要的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码提取今天的约会:

I'm extracting todays appointments with the following code:

$olFolderCalendar = 9
$ol = New-Object -ComObject Outlook.Application
$ns = $ol.GetNamespace('MAPI')
$Start = (Get-Date).AddDays(-1).ToShortDateString() + " 00:00"
$End = (Get-Date).AddDays(+1).ToShortDateString() + " 00:00"

$Filter = "[MessageClass]='IPM.Appointment' AND [Start] > '$Start' AND [End] < '$End'"

$Appointments = $ns.GetDefaultFolder($olFolderCalendar).Items
$Appointments.Sort("[Start]")
$Appointments.IncludeRecurrences = $false

foreach ($Appointment in $Appointments.Restrict($Filter) ) {
    ...
}

列出了所有今天的约会,但也列出了许多今天没有发生的重复约会(生日、每周约会……).知道如何避免这种情况吗?

All todays appointments are listed but also a lot of recurring appointments that are NOT taking place today (birthdays, weekly appointments, ...). Any idea how to avoid that?

似乎所有这些不需要的约会最初都是从我的手机同步到 Outlook 的.我将在干净"的 PC 上试用该脚本.

Seems like all this unwanted appointments are originally from my mobile synced to outlook. I'll try the script on a 'clean' PC.

我在另一台没有同步元素的 PC 上尝试了该脚本,结果是一样的:所有重复出现的元素都会显示,无论它们是否在今天.AND [IsRecurring] = '$False'也没有帮助.

I tried the script on another PC without synced elements and it's the same: All recurring elements are display whether they are today or not. AND [IsRecurring] = '$False' is not helping either.

推荐答案

初始查询必须包含系列的原始约会,所以如果系列开始于 3 个月前,约会集合 ($folder.items) 日期范围必须相应地设置.

The initial query must include the original appointment of the series, so if the series started 3 months ago, the collection of appointments ($folder.items) date range must be set accordingly.

之后您可以过滤所需的日期范围.

Afterwards you can filter for the desired date range.

此代码有效:

Function Get-OutlookCalendar
{
 echo starting...
 Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
 $olFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type]
 $outlook = new-object -comobject outlook.application
 $namespace = $outlook.GetNameSpace("MAPI")
 $folder = $namespace.getDefaultFolder($olFolders::olFolderCalendar)

 $a = Get-Date -Hour 0 -Minute 00 -Second 00
 $b = (Get-Date -Hour 0 -Minute 00 -Second 00).AddDays(7)
 echo From $a To $b

 $Appointments = $folder.Items
 $Appointments.IncludeRecurrences = $true
 $Appointments.Sort("[Start]")

 $Appointments | Where-object { $_.start -gt $a -AND $_.start -lt $b } | Select-Object -Property IsRecurring, RecurrenceState, Subject, Start, Location

} #end function Get-OutlookCalendar

运行这个 - 对于傻瓜,就像昨天的我一样:)

To run this -for dummies, like myself yesterday :)

cmd
powershell
PS C:\Users\Jose\Documents\WindowsPowerShell> Import-Module -Name Outlook\expcal.psm1 -Force
PS C:\Users\Jose\Documents\WindowsPowerShell> Get-OutlookCalendar

这篇关于使用 powershell 从 Outlook 获取今天的约会:不需要的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆