预警系统 | Powershell:加载日历项扩展属性

Posted

技术标签:

【中文标题】预警系统 | Powershell:加载日历项扩展属性【英文标题】:EWS | Powershell: Load Calendar Item Extended Property 【发布时间】:2015-02-05 06:20:52 【问题描述】:

我正在尝试将一组扩展属性加载到其他日历约会对象中以报告和操作它们。但是,我一直在这方面遇到麻烦。每当它到达需要加载扩展属性的行时,我都会收到此错误:“Exception calling “Load” with “1” argument(s): “Custom properties cannot be specified using property tags”。必须改用 GUID 和 Id/Name 组合"

我遇到的问题是: $apApointment.Load($psPropset);

整个代码如下。任何帮助表示赞赏。顺便说一句,我还是 EWS 的初学者。谢谢

Report = @()
$MailboxList = Read-Host "Enter path to txt file where users are saved."


$StartDate = Get-Date 1/1/2013
$EndDate = Get-Date 4/1/2013

#$StartDate = new-object System.DateTime(2014, 08, 27)
#$EndDate = new-object System.DateTime(2015, 02, 28)
	

#Logon to Exchange Web Service with default credentials	
	Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
    $sid = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value
    $user = [ADSI]"LDAP://<SID=$sid>"        
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList ([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2)
    $service.AutodiscoverUrl($user.Properties.mail)
	
Write-Progress -Activity "Preparing" -Status "Retrieving mailbox list" -PercentComplete 0

$Mailboxes = cat $MailboxList | get-mailbox

$Count = $Mailboxes.Count


#Go through each users found
ForEach ($Mailbox in $Mailboxes)
		$DisplayName = $Mailbox.DisplayName
#		$i = $i + 1
#		$pct = $i/$Count * 100
#		Write-Progress -Activity "Collecting mailbox details" -Status "Processing mailbox $i of $Count - $DisplayName" -PercentComplete $pct

		Try 
		$Ok = $true
		$Mailbox = (Get-Mailbox $mailbox.WindowsEmailAddress -ErrorAction Stop ).PrimarySMTPAddress
		catch [System.Exception]
		$Ok = $false
		
		if ($Ok)
		#Set EWS up for impersonation of all users
		$ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId -ArgumentList ([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress),$Mailbox
		$service.ImpersonatedUserId = $ImpersonatedUserId	

		#Open user folder and bind calendar folder to the EWS service. Then, set each calendar default view to 1000
		$folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar,$Mailbox)
		$CalendarFolder = [Microsoft.Exchange.WebServices.Data.CalendarFolder]::Bind($service,$folderid)
		$cvCalendarview = new-object Microsoft.Exchange.WebServices.Data.CalendarView($StartDate,$EndDate,1000)


		#Query the calendar and return the appointments
		$cvCalendarview.PropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
		$frCalendarResult = $CalendarFolder.FindAppointments($cvCalendarview)

		foreach ($apApointment in $frCalendarResult.Items)

			#Go through each calendar items and collect thier attributes
			
					
			$psPropset = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
			
			#Create extended properties
			$PR_SENT_REPRESENTING_NAME = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x42,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String)
			$PR_SENDER_NAME = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0xc1a,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String)
			$dispidApptTZDefStartDisplay = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x825E,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String)
			$dispidApptTZDefEndDisplay = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x825F,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String)
			$ptagSentRepresentingSimpleDispName = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x4031,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String)
			
			#Add extended properties to properties set
			$psPropset.Add($PR_SENDER_NAME);
			$psPropset.Add($PR_SENT_REPRESENTING_NAME);
			$psPropset.Add($dispidApptTZDefStartDisplay);
			$psPropset.Add($dispidApptTZDefEndDisplay);
			$psPropset.Add($ptagSentRepresentingSimpleDispName);

			#Add properties to calendar view
			$cvCalendarview.PropertySet = $psPropset;
			
			#Load properties into current appointment
			**$apApointment.Load($psPropset);**
			
			$SENDER_NAME = @()
			$SENT_REPRESENTING_NAME = @()
			$TZDefStartDisplay = @()
			$TZDefEndDisplay = @()
			$ptagSentRepSimpleName = @()
			
			$apApointment.TryGetProperty($PR_SENT_REPRESENTING_NAME, [ref] $SENT_REPRESENTING_NAME)
			$apApointment.TryGetProperty($PR_SENDER_NAME, [ref] $SENDER_NAME)
			$apApointment.TryGetProperty($dispidApptTZDefStartDisplay, [ref] $TZDefStartDisplay)
			$apApointment.TryGetProperty($dispidApptTZDefEndDisplay, [ref] $TZDefEndDisplay)
			$apApointment.TryGetProperty($ptagSentRepresentingSimpleDispName, [ref] $ptagSentRepSimpleName)

			$app = $apApointment.Subject
			$start = $apApointment.Start
			$End = $apApointment.End
			$WhenCreated = $apApointment.DateTimeCreated
			$Organizer = ($apApointment.Organizer).Address
			$Required = $apApointment.RequiredAttendees.Count
			$Recurring = $apApointment.IsRecurring

			#Prepare objects needed for reports
			$Obj = New-Object -TypeName PSObject
			$Obj | Add-Member -MemberType NoteProperty -Name MeetingSubject -Value $app
			$Obj | Add-Member -MemberType NoteProperty -Name MeetingStartTime -Value $start
			$Obj | Add-Member -MemberType NoteProperty -Name MeetingEndTime -Value $End
			$Obj | Add-Member -MemberType NoteProperty -Name WhenCreated -Value $WhenCreated
			$Obj | Add-Member -MemberType NoteProperty -Name ReoccuringMeeting -Value $Recurring
			$Obj | Add-Member -MemberType NoteProperty -Name MeetingOrganizer -Value $Organizer
			$Obj | Add-Member -MemberType NoteProperty -Name SentRepresentingName -Value $SENT_REPRESENTING_NAME
			$Obj | Add-Member -MemberType NoteProperty -Name SenderName -Value $SENDER_NAME
			$Obj | Add-Member -MemberType NoteProperty -Name RequiredAttendeeCount -Value $Required
			$Obj | Add-Member -MemberType NoteProperty -Name MailboxOwner -Value $Mailbox
			$Obj | Add-Member -MemberType NoteProperty -Name MailboxOwnerDisplay -Value $DisplayName
			$Report += $Obj
			
			Write-Host "$Mailbox Calendar is being processed"
			
			 
		


#Get all reports and save them into respective paths
$Report | Export-Csv c:\CorruptCalender.csv -NoTypeInformation

【问题讨论】:

【参考方案1】:

您定义的两个属性$dispidApptTZDefStartDisplay$dispidApptTZDefEndDisplay 是问题所在。您已使用 0x8000 范围内的常量属性标记值定义它们。该范围内的 MAPI 标记是命名属性,它们的实际标记值未设置(它们从邮箱更改为邮箱)。您需要根据属性集 GUID 和属性 ID 来定义它们。

您拥有的值实际上是属性 ID 值,而不是标签值。因此,您需要将它们与属性集 GUID 结合起来才能让它们工作。托管 API 实际上为 Appointment 属性集定义了一个常量,这些属性属于这些属性,因此您可以将这些行更改为:

$dispidApptTZDefStartDisplay = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition([Microsoft.Exchange.WebServices.Data.DefaultExtendedPropertySet]::Appointment, 0x825E,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$dispidApptTZDefEndDisplay = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition([Microsoft.Exchange.WebServices.Data.DefaultExtendedPropertySet]::Appointment, 0x825F,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)

它应该会清除该错误(除非我破坏了我的 Powershell 语法:))

【讨论】:

谢谢杰森。您的建议使我接近最终结果。但是,我遇到了另一个错误。似乎 EWS 不希望我组合不同类型的扩展属性。错误如下:使用“1”参数调用“加载”的异常:“扩展的属性属性组合无效。” 啊,是的,另一个问题。这两个道具不是字符串,它们是二进制的。编辑了我的答案。 谢谢你,杰森。我在发布后不久就发现了它。

以上是关于预警系统 | Powershell:加载日历项扩展属性的主要内容,如果未能解决你的问题,请参考以下文章

powershell 安装-Excel的加载项

powershell 扫描Outlook日历条目(Powershell)

比较好的风险预警系统都有哪些?

powershell 从日历中删除约会

powershell 授予O365日历访问权限

添加日历视图Web部件PowerShell脚本