Home > Blockchain >  Get SPO sites using MS Graph API powershell not working
Get SPO sites using MS Graph API powershell not working

Time:09-22

I'm trying to get all SharePoint Online sites' name and url via PowerShell using MS Graph API, but it's not seem to be working. That's all I get from the request:

@{@odata.context=https://graph.microsoft.com/v1.0/$metadata#sites; value=System.Object[]}

The application I use have all the needed Application type API permissions (Sites.Read, Sites.ReadWrite.All) with admin consent.

Do you have any idea why my script not working?

The code:

$TenantID = 'xxxxxxxxx.ONMICROSOFT.COM'
$ApplicationId = "xxxxx-xxxxxx-xxxx-xxxx"
$ApplicationSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

$body = @{
    'resource'      = 'https://graph.microsoft.com'
    'client_id'     = $ApplicationId
    'client_secret' = $ApplicationSecret
    'grant_type'    = "client_credentials"
    'scope'         = "openid"
}

$ClientToken = Invoke-RestMethod -Method post -Uri "https://login.microsoftonline.com/$($tenantid)/oauth2/token" -Body $body -ErrorAction Stop
$headers = @{ "Authorization" = "Bearer $($ClientToken.access_token)" }


$AllSites = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/sites?search=*" -Headers $headers -Method Get 

Write-Host $AllSites

I've also tried these URIs:

https://graph.microsoft.com/v1.0/sites?search=* https://graph.microsoft.com/v1.0/sites https://graph.microsoft.com/v1.0/sites$select=siteCollection,webUrl&$filter=siteCollection/root ne null

CodePudding user response:

The Write-Host cmdlet's primary purpose is to produce for-(host)-display-only output, such as printing colored text like when prompting the user for input in conjunction with Read-Host. Write-Host uses the ToString() method to write the output. By contrast, to output data to the pipeline, use Write-Output or implicit output.

reference

This mean that your output is transformed for display purposes. Where you see System.Object[], there is actually data in there just waiting for you.

Based on your current results, your query look good. Just do not use Write-Host and dig into the object as needed. To get the site names, just use $AllSites.Value.Name

$AllSites = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/sites?search=*" -Headers $headers -Method Get

# Return site names
$AllSites.Value.Name

If you need to get additional information from each return you can loop into them, like this to do whatever you need. Here is a custom display of the site name along with an arbitrary index and the associated URL


$index = 0
# Will display results like
# 0: SiteName - www.contoso.sharepoint.com/SiteUrl
foreach ($Site in $AllSites.Value) {
    Write-Host "$($index.ToString().PadRight(3,' ')): $($Site.Name) - " -NoNewline 
    Write-Host $site.webUrl -ForegroundColor Cyan
    $index  = 1
}

Also, here is an additional reference when working with Azure Graph API that will confirm your requests are working as expected: https://developer.microsoft.com/en-us/graph/graph-explorer

  • Related