MS Graph API to access SPO with Powershell

Brass Contributor

Hi,
I want to automate one task in Office 365 by using powershell and for this I registered an app in AAD (V2 endpoint https://apps.dev.microsoft.com) and given access to MS Graph API with permissions "Have full access to all site collections", "Read and Write all groups" (and so on) access to the app.
I am able to get the O365 group details via below PnP powershell commands but I am not sure how to get the context of SharePoint site or how to connect to a SP site within the same PS script by utilizing the same App credentials.

 

Connect-PnPOnline -AppId '<App Id>' -AppSecret '<App Secret>' -AADDomain 'tenant.onmicrosoft.com'
$group = Get-PnPUnifiedGroup -Identity 'hello team'
$spSiteUrl = $group.SiteUrl


Any tips how can I connect to SPO site?

2 Replies

to get the clientcontext you would do something like this:

$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
    $clientContext.Credentials = $credentials
    
    if (!$clientContext.ServerObjectIsNull.Value)
    {
        Write-Host "Connected to SharePoint Online site: '$siteUrl'" -ForegroundColor Green
    }

Thanks but I am expecting to authenticate to SPO with the same App principal. In your example I have to pass the credentials which will not work in my case because I want to use the app id and secret.