Skip to content

Instantly share code, notes, and snippets.

@pankajsurti
Created February 5, 2022 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pankajsurti/77c56761268d427bd21dc881c7886893 to your computer and use it in GitHub Desktop.
Save pankajsurti/77c56761268d427bd21dc881c7886893 to your computer and use it in GitHub Desktop.
clear
# Application (client) ID, secret, tenant name and site
$tenantPrefix = "M365x162783";
$clientId = "ca2a60c7-d09a-4875-841b-117a02b504fd"; #client-app
$clientSecret = "A------- blah- blah--eGX_soM7SEiKAhe"; TODO
$tenantName = $tenantPrefix +".onmicrosoft.com";
$tenantDomain = $tenantPrefix +".sharepoint.com";
$sitePath = "https://M365x162783.sharepoint.com/sites/lbtest1"
$siteName = $sitePath.Split("/")[4]
$resource = "https://graph.microsoft.com/"
$ReqTokenBody = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
client_Id = $clientID
Client_Secret = $clientSecret
}
$TokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token" -Method POST -Body $ReqTokenBody
$AccessToken = $TokenResponse.access_token
$apiUrl = 'https://graph.microsoft.com/v1.0/sites/'+ $tenantDomain +':/sites/'+ $siteName +'?$select=id,displayName'
try {
$spoResult = Invoke-RestMethod -Headers @{Authorization = "Bearer $($Tokenresponse.access_token)"} -Uri $apiUrl -Method Get
Write-Host "Site: " $spoResult.displayName
}
catch {
Write-Output "Failed to enumerate the site"
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
#Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
Exit
}
$apiUrl = 'https://graph.microsoft.com/v1.0/sites/'+ $spoResult.id +'/lists?$select=displayName'
try {
# $spoData = Invoke-RestMethod -Headers @{Authorization = "Bearer $($Tokenresponse.access_token)"} -Uri $apiUrl -Method Get -ContentType "text/plain" -ResponseHeadersVariable 'spoRespHeaders'
$spoData = Invoke-RestMethod -Headers @{Authorization = "Bearer $($Tokenresponse.access_token)"} -Uri $apiUrl -Method Get -ContentType "text/plain"
$spoData.Value | FT
}
catch {
Write-Output "Failed to add permissions the site"
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment