I'm trying to access my own calendar via powershell but am getting an error:
Install-Module Microsoft.Graph
Import-Module Microsoft.Graph.Calendar
$creds = Get-Credential -UserName "<my user name>"
# Get list of available graph scopes
Connect-MgGraph
Get-MgContext | Select-Object -ExpandProperty Scopes
$userId = (Get-MgUser -Filter "Mail eq '$($creds.UserName)'").Id
# Get calendar permissions
Find-MgGraphCommand -command Get-MgUserCalendar | Select-Object -ExpandProperty Permissions
Get-MgUserCalendar -UserId $userId
Results:
Get-MgUserCalendar_List: The specified object was not found in the store.
Status: 404 (NotFound)
ErrorCode: ErrorItemNotFound
The Get-MgContext | Select-Object -ExpandProperty Scopes
returns:
Application.Read.All
Application.ReadWrite.All
Calendars.Read.Shared
DelegatedPermissionGrant.ReadWrite.All
openid
Policy.ReadWrite.PermissionGrant
profile
User.Read
User.ReadWrite.All
email
The Find-MgGraphCommand
returned this:
Name IsAdmin Description FullDescription
---- ------- ----------- ---------------
Calendars.ReadBasic False Consent name unavailable Consent description unavailable
Calendars.Read.Shared False Read calendars you can access Allows the app to read events in all calendars…
Calendars.ReadWrite False Read and write calendars in all mailboxes Allows the app to create, read, update, and de…
Calendars.Read False Read calendars in all mailboxes Allows the app to read events of all calendars…
It seems I have the access, but the error isn't helpful. Any suggestions?
Updates:
Graph calendar results:
Get-MgUserCalendar
results:
When I ran below query in Graph Explorer, I got the response successfully with signed-in user's calendars details like this:
GET https://graph.microsoft.com/v1.0/me/calendars
Response:
To get the same details via Microsoft Graph PowerShell, try including scopes parameter like Connect-MgGraph -Scopes Calendars.Read.Shared
and sign in interactively with your account like this:
Connect-MgGraph -Scopes Calendars.Read.Shared
Get-MgContext | Select-Object -ExpandProperty Scopes
$userId = (Get-MgUser -Filter "Mail eq 'sri@xxxxxxxxxx.onmicrosoft.com'").Id
Find-MgGraphCommand -command Get-MgUserCalendar | Select-Object -ExpandProperty Permissions
Get-MgUserCalendar -UserId $userId
Running above script opens browser and asks user to pick an account like this:
While signing in, I got below consent screen to allow user for accessing calendars:
After successful authentication, it gives below screen in browser saying "Authentication complete" like this:
When I checked the output now, I got the response successfully with signed-in user's calendars details like this: