Skip to content

Instantly share code, notes, and snippets.

@svarukala
Last active July 17, 2020 14:02
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 svarukala/0d8218d423ac47e48457fb44eb54cd38 to your computer and use it in GitHub Desktop.
Save svarukala/0d8218d423ac47e48457fb44eb54cd38 to your computer and use it in GitHub Desktop.
clear
# Azure AD(AAD) Application (client) ID, tenant Name and secret
$tenantName = "YOURTENANT.onmicrosoft.com"
$clientId = "AAD APP ID";
$clientSecret = "AAD APP CLIENT SECRET";
$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
$authHeader = @{
#"Content-Type"= "application\json"
"Authorization"="Bearer $AccessToken"
#"Accept" = "*/*"
}
$Tokenresponse.access_token | clip
#Get files from SPO library
$apiUrl = 'https://graph.microsoft.com/v1.0/sites/root/lists/45c12593-c895-478c-916c-15c6368a40dc/items'
try {
$spoResult = Invoke-RestMethod -Headers @{Authorization = "Bearer $($Tokenresponse.access_token)"} -Uri $apiUrl -Method Get
Write-output "Received SPO lib items"
Write-output $spoResult.value.count
}
catch {
Write-Output "Failed to get files from SPO"
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
}
#Get files from ODB
$apiUrl = 'https://graph.microsoft.com/v1.0/me/drive/root/children'
try {
$odbResult = Invoke-RestMethod -Headers @{Authorization = "Bearer $($Tokenresponse.access_token)"} -Uri $apiUrl -Method Get
Write-output "Received ODB items"
Write-output $odbResult.value.count
}
catch {
Write-Output "Failed to get files from ODB"
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
}
#Create file in ODB
$apiUrl = 'https://graph.microsoft.com/v1.0/me/drive/root:/DemoFile.txt:/content'
$body = "This is sample text that goes into the text file"
try {
$odbData = Invoke-RestMethod -Headers @{Authorization = "Bearer $($Tokenresponse.access_token)"} -Uri $apiUrl -Method Put -Body $body -ContentType "text/plain" -ResponseHeadersVariable odbRespHeaders
Write-output "Created ODB file"
Write-output $odbData.webUrl
#Write-Output $odbRespHeaders
}
catch {
Write-Output "Failed to create file in ODB"
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
}
#Create file in SPO
$apiUrl = 'https://graph.microsoft.com/v1.0/sites/root/drive/items/root:/DemoFile.txt:/content'
$body = "This is sample text that goes into the text file"
try {
$spoData = Invoke-RestMethod -Headers @{Authorization = "Bearer $($Tokenresponse.access_token)"} -Uri $apiUrl -Method Put -Body $body -ContentType "text/plain" -ResponseHeadersVariable spoRespHeaders
Write-output "Created SPO file"
Write-output $spoData.webUrl
#Write-Output $spoRespHeaders
}
catch {
Write-Output "Failed to create file in SPO"
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