Gathering O365 calendar information via Powershell

Today I want to share with you my experience with gathering O365 calendar information via Powershell.
Office 365 is currently the most popular service to provide access to Microsoft Office tools including mail account.
Sometimes information from calendar are very usefull, for instance when work time registration is included in Office 365 calendar
If we want to create report with details about user tasks, we can take it directly from O365 calendar.

Below script will show you how to take this information from shared calendar (to access your private account using another account, special permission must be granted in Office 365).
At he beginnning you must define your O365 credentials, mail from which you want take the information and time range from which you want to check meetings. Variables for dates can be adjusted for your needs. In my example I set $StartDate to 4 days back and $EndDate to 2 days ahead.

### Provide needed information ###
$username = "name@mail.com"
$password = "SuperSecretPassword"
$mail = "team@mail.com"
$StartDate = (Get-Date).AddDays(-4)
$EndDate = (Get-Date).AddDays(2)

$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$Credentials = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr

### Connect to O365 REST API ###
$CalendarInfo = Invoke-RestMethod -Uri "https://outlook.office365.com/api/v1.0/users/$mail/calendarview?startDateTime=$StartDate&endDateTime=$EndDate" -Credential $Credentials
$CalendarInfoArray = $CalendarInfo.value

### Process meetings from specific time range ###
$MeetingInfo = @()
foreach($meeting in $CalendarInfoArray)
{
    $meeting
    foreach($Atendee in $meeting.Attendees)
    {
        $MeetingResult = New-Object PSObject
        $MeetingResult  | Add-member Noteproperty MeetingName $meeting.Subject
        $MeetingResult  | Add-Member Noteproperty UserID $Atendee.EmailAddress.Address
        $MeetingResult  | Add-Member Noteproperty Start $meeting.Start
        $MeetingResult  | Add-Member Noteproperty End $meeting.End
        $MeetingInfo += $MeetingResult
    }
}

$MeetingInfo

As a result you will receive variable $MeetingInfo – array with all meetings from specific time range.
For those who wants to check more scripts and laern usefull commands for Office 365, I strongly recommend to visit site http://powershell.office.com/.

If you will find any bug don’t hesitate to leave a comment and correct me.
I hope that will be usefull for some of you 🙂
Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.