#Get-O365usagedata # # Connect to graph to query office 365 reports # # 20190207 initial Version param ( [string]$LoginUrl = "https://login.microsoft.com", # Graph API URLs. [string]$ResourceUrl = "https://graph.microsoft.com", # Ressource API URLs. [string]$AppID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", # App ID aus dem Azure Portal . [string]$AppKey = "xxxxxxxxxxxx", # App Key from Portal [string]$TenantName = "xxxxxxx.onmicrosoft.com", # tenant name. #[string]$GraphUrl = "https://graph.microsoft.com/v1.0/reports/getOffice365ActivationCounts", # The Graph URL to retrieve data. #[string]$GraphUrl = "https://graph.microsoft.com/v1.0/reports/getOffice365ActivationsUserCounts", # The Graph URL to retrieve data. [string]$GraphUrl = "https://graph.microsoft.com/v1.0/reports/getOffice365ActivationsUserDetail", # The Graph URL to retrieve data. #[string]$GraphUrl = "https://graph.microsoft.com/v1.0/me", #[string]$GraphUrl = "https://graph.microsoft.com/v1.0/reports/getSharePointActivityUserDetail(period='D7')", [string]$csvfilename = ".\report.csv" ) write-host "Get-O365UsageData: Start" # Get OAUTH Token write-host "Get-O365UsageData: Create OAUTh Request" $Body = @{ grant_type = "client_credentials"; resource = $ResourceUrl; client_id = $AppID; client_secret = $AppKey } write-host "Get-O365UsageData: Send OAuth Request" [object]$OAuth = Invoke-RestMethod -Method Post -Uri "$($LoginUrl)/$($TenantName)/oauth2/token?api-version=1.0" -Body $Body # Check if authentication is successfull. if ($null -eq $OAuth.access_token) { write-host "Get-O365UsageData: No Token received" -ForegroundColor red } else { # Perform REST call. write-host "Get-O365UsageData: prepare Authorization Header with TokenType $($OAuth.token_type)" $HeaderParams = @{ 'Authorization' = "$($OAuth.token_type) $($OAuth.access_token)" } try { write-host "Get-O365UsageData: Send WebRequest" Invoke-WebRequest -UseBasicParsing -Headers $HeaderParams -Uri $GraphUrl -OutFile $csvfilename write-host "Get-O365UsageData: Webrequest done: File $($csvfilename) for details" } catch { write-host "Get-O365UsageData: WebRequest failed" -ForegroundColor red $_.Exception $resultstream = $_.Exception.Response.GetResponseStream() $reader = New-Object System.IO.StreamReader($resultstream) $ErrorBody = $reader.ReadToEnd(); write-host " ResultError:" $ErrorBody } } write-host "Get-O365UsageData: End"