I am trying to create a calendar sync using EWS. This functionen is trying to mess with me though: It works just fine if run as script (and returns all the appointments in the calendar), but returns $null if run after packaging (PowerShell 2, standard manifest for elevation). To be more accurate, it returns $null if "try" succedes and an array of $nulls if I have to catch the error. The number of $nulls in that array is dependant on the number of iterations, not the number of calendar appointments.
PowerShell Code
Double-click the code block to select all.function Get-EWSAppointments([Microsoft.Exchange.WebServices.Data.CalendarFolder]$calendarfolder,[System.DateTime]$from,[System.DateTime]$to,$service) { # Interrupt if no time-span exists if ($from -eq $to) { Write-Error -Message "Timespan can't be 0 seconds" } else { # Switch time if in wrong order if (($from.CompareTo($to)) -eq 1) { $Temp = $from $from = $to $to = $Temp } # Get Appointments and return them try { $Calendarview = new-object Microsoft.Exchange.WebServices.Data.CalendarView($from,$to) $Calendarview.PropertySet = [Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties $testcv = $Calendarview $Error.Clear() $results = $calendarfolder.FindAppointments($Calendarview) if (($service -ne $null) -and ($results.TotalCount -gt 0)) { $schema = [Microsoft.Exchange.WebServices.Data.ItemSchema]::Body $property = New-Object Microsoft.Exchange.WebServices.Data.PropertySet($schema) $property.BasePropertySet = [Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties $results = $service.LoadPropertiesForItems($results,$property) $script:tempResults = $results $ResultSet = @() if ($tempResults.Count -gt 0) { $i = 0 while ($i -lt $tempResults.Count) { $Temp = $tempResults.get_Item($i) if ($Temp.Result.value__ -eq 0){$ResultSet += $Temp.Item} $i++ } } return $ResultSet } else { return $results } } catch { $temp2 = $Error[0] if ($temp2.Exception.Message.Contains("Sie haben die maximale Anzahl von Objekten überschritten")) { # Calculate middle value between dates $middle = $to - $from $middle = $from.Add([System.TimeSpan]::FromSeconds(($middle.TotalSeconds / 2))) # Do the same call again twice, but this time with each only half the time $ResultSet = @() $ResultSet += Get-EWSAppointments -calendarfolder $calendarfolder -from $from -to $middle -service $service $ResultSet += Get-EWSAppointments -calendarfolder $calendarfolder -from $middle -to $to -service $service return $ResultSet } } } }Any help appreciated.
Regards,
Bosparan