rdefino
asked on
Need to export a users calendar from 0365 to csv in Power shell
I need to be able to exports some individual users calendars from 0365 to a csv in powershell.
I found the below and modified it for my environment, or at least I think I did. I;m not much of a scripter.
these are the errors I;m getting and cannot figure out whats wrong. I'm test against my own mail, so I have full access.
Not sure if anyone can see my mistake.
errors.....
PS C:\script> .\calendar-export.ps1
Exception calling "Bind" with "2" argument(s): "The request failed. The remote server returned an error: (404) Not
Found."
At C:\script\calendar-export. ps1:14 char:1
+ $Calendar = [Microsoft.Exchange.WebSer vices.Data .Folder]:: Bind($serv i ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ServiceRequestException
Exception calling "FindAppointments" with "2" argument(s): "The element at position 0 is invalid
Parameter name: parentFolderIds"
At C:\script\calendar-export. ps1:26 char:1
+ $fiItems = $service.FindAppointments( $Calendar. Id,$Calend arView)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentException
script.................
#Declare Variables
$EWSDLL = "C:\Program Files\Microsoft\Exchange\W eb Services\2.2\Microsoft.Exc hange.WebS ervices.dl l"
$MBX = "rdefino@company.com"
$EWSURL = "https://Ex2013Svr1.domain.com/EWS/Exchange.asmx"
$StartDate = (Get-Date).AddDays(-60)
$EndDate = (Get-Date)
#Binding of the calendar of the Mailbox and EWS
Import-Module -Name $EWSDLL
$mailboxname = $MBX
$service = new-object Microsoft.Exchange.WebServ ices.Data. ExchangeSe rvice([Mic rosoft.Exc hange.WebS ervices.Da ta.Exchang eversion]: :exchange2 013)
$service.Url = new-object System.Uri($EWSURL)
$folderid= new-object Microsoft.Exchange.WebServ ices.Data. FolderId([ Microsoft. Exchange.W ebServices .Data.Well KnownFolde rName]::Ca lendar,$Ma ilboxName)
$Calendar = [Microsoft.Exchange.WebSer vices.Data .Folder]:: Bind($serv ice,$folde rid)
$Recurring = new-object Microsoft.Exchange.WebServ ices.Data. ExtendedPr opertyDefi nition([Mi crosoft.Ex change.Web Services.D ata.Defaul tExtendedP ropertySet ]::Appoint ment, 0x8223,[Microsoft.Exchange .WebServic es.Data.Ma piProperty Type]::Boo lean);
$psPropset= new-object Microsoft.Exchange.WebServ ices.Data. PropertySe t([Microso ft.Exchang e.WebServi ces.Data.B asePropert ySet]::Fir stClassPro perties)
$psPropset.Add($Recurring)
$psPropset.RequestedBodyTy pe = [Microsoft.Exchange.WebSer vices.Data .BodyType] ::Text;
$RptCollection = @()
$AppointmentState = @{0 = "None" ; 1 = "Meeting" ; 2 = "Received" ;4 = "Canceled" ; }
#Define the calendar view
$CalendarView = New-Object Microsoft.Exchange.WebServ ices.Data. CalendarVi ew($StartD ate,$EndDa te,1000)
$fiItems = $service.FindAppointments( $Calendar. Id,$Calend arView)
if($fiItems.Items.Count -gt 0){
$type = ("System.Collections.Gener ic.List"+' `'+"1") -as "Type"
$type = $type.MakeGenericType("Mic rosoft.Exc hange.WebS ervices.Da ta.Item" -as "Type")
$ItemColl = [Activator]::CreateInstanc e($type)
foreach($Item in $fiItems.Items){
$ItemColl.Add($Item)
}
[Void]$service.LoadPropert iesForItem s($ItemCol l,$psProps et)
}
foreach($Item in $fiItems.Items){
$rptObj = "" | Select StartTime,EndTime,Duration ,Type,Subj ect,Locati on,Organiz er,Attende es,Appoint mentState, Notes,HasA ttachments ,IsReminde rSet
$rptObj.StartTime = $Item.Start
$rptObj.EndTime = $Item.End
$rptObj.Duration = $Item.Duration
$rptObj.Subject = $Item.Subject
$rptObj.Type = $Item.AppointmentType
$rptObj.Location = $Item.Location
$rptObj.Organizer = $Item.Organizer.Address
$rptObj.HasAttachments = $Item.HasAttachments
$rptObj.IsReminderSet = $Item.IsReminderSet
$aptStat = "";
$AppointmentState.Keys | where { $_ -band $Item.AppointmentState } | foreach { $aptStat += $AppointmentState.Get_Item ($_) + " "}
$rptObj.AppointmentState = $aptStat
$RptCollection += $rptObj
foreach($attendee in $Item.RequiredAttendees){
$atn = $attendee.Address + "; "
$rptObj.Attendees += $atn
}
foreach($attendee in $Item.OptionalAttendees){
$atn = $attendee.Address + "; "
$rptObj.Attendees += $atn
}
foreach($attendee in $Item.Resources){
$atn = $attendee.Address + "; "
$rptObj.Resources += $atn
}
$rptObj.Notes = $Item.Body.Text
#Display on the screen
"Start: " + $Item.Start
"Subject: " + $Item.Subject
}
#Export to a CSVFile
$RptCollection | Export-Csv -NoTypeInformation -Path "c:\temp\$MailboxName-Cale ndarCSV.cs v"
I found the below and modified it for my environment, or at least I think I did. I;m not much of a scripter.
these are the errors I;m getting and cannot figure out whats wrong. I'm test against my own mail, so I have full access.
Not sure if anyone can see my mistake.
errors.....
PS C:\script> .\calendar-export.ps1
Exception calling "Bind" with "2" argument(s): "The request failed. The remote server returned an error: (404) Not
Found."
At C:\script\calendar-export.
+ $Calendar = [Microsoft.Exchange.WebSer
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ServiceRequestException
Exception calling "FindAppointments" with "2" argument(s): "The element at position 0 is invalid
Parameter name: parentFolderIds"
At C:\script\calendar-export.
+ $fiItems = $service.FindAppointments(
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentException
script.................
#Declare Variables
$EWSDLL = "C:\Program Files\Microsoft\Exchange\W
$MBX = "rdefino@company.com"
$EWSURL = "https://Ex2013Svr1.domain.com/EWS/Exchange.asmx"
$StartDate = (Get-Date).AddDays(-60)
$EndDate = (Get-Date)
#Binding of the calendar of the Mailbox and EWS
Import-Module -Name $EWSDLL
$mailboxname = $MBX
$service = new-object Microsoft.Exchange.WebServ
$service.Url = new-object System.Uri($EWSURL)
$folderid= new-object Microsoft.Exchange.WebServ
$Calendar = [Microsoft.Exchange.WebSer
$Recurring = new-object Microsoft.Exchange.WebServ
$psPropset= new-object Microsoft.Exchange.WebServ
$psPropset.Add($Recurring)
$psPropset.RequestedBodyTy
$RptCollection = @()
$AppointmentState = @{0 = "None" ; 1 = "Meeting" ; 2 = "Received" ;4 = "Canceled" ; }
#Define the calendar view
$CalendarView = New-Object Microsoft.Exchange.WebServ
$fiItems = $service.FindAppointments(
if($fiItems.Items.Count -gt 0){
$type = ("System.Collections.Gener
$type = $type.MakeGenericType("Mic
$ItemColl = [Activator]::CreateInstanc
foreach($Item in $fiItems.Items){
$ItemColl.Add($Item)
}
[Void]$service.LoadPropert
}
foreach($Item in $fiItems.Items){
$rptObj = "" | Select StartTime,EndTime,Duration
$rptObj.StartTime = $Item.Start
$rptObj.EndTime = $Item.End
$rptObj.Duration = $Item.Duration
$rptObj.Subject = $Item.Subject
$rptObj.Type = $Item.AppointmentType
$rptObj.Location = $Item.Location
$rptObj.Organizer = $Item.Organizer.Address
$rptObj.HasAttachments = $Item.HasAttachments
$rptObj.IsReminderSet = $Item.IsReminderSet
$aptStat = "";
$AppointmentState.Keys | where { $_ -band $Item.AppointmentState } | foreach { $aptStat += $AppointmentState.Get_Item
$rptObj.AppointmentState = $aptStat
$RptCollection += $rptObj
foreach($attendee in $Item.RequiredAttendees){
$atn = $attendee.Address + "; "
$rptObj.Attendees += $atn
}
foreach($attendee in $Item.OptionalAttendees){
$atn = $attendee.Address + "; "
$rptObj.Attendees += $atn
}
foreach($attendee in $Item.Resources){
$atn = $attendee.Address + "; "
$rptObj.Resources += $atn
}
$rptObj.Notes = $Item.Body.Text
#Display on the screen
"Start: " + $Item.Start
"Subject: " + $Item.Subject
}
#Export to a CSVFile
$RptCollection | Export-Csv -NoTypeInformation -Path "c:\temp\$MailboxName-Cale
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.