EWS | Powershell:加载日历项扩展属性

时间:2015-02-05 06:20:52

标签: powershell exchangewebservices

我试图将一组扩展属性加载到日历约会对象中以报告和操作它们。但是,我一直遇到麻烦。每当它到达需要加载扩展属性的行时,我都会收到此错误:" 异常调用"加载"用" 1"参数:"无法使用属性标记"指定自定义属性。必须使用GUID和ID /名称组合"

我遇到问题的一条线是: 的 $ 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
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您定义的两个属性$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语法:))