SOLVED

Graph API and Powershell

%3CLINGO-SUB%20id%3D%22lingo-sub-1460592%22%20slang%3D%22en-US%22%3EGraph%20API%20and%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1460592%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20looking%20to%20use%20the%20Graph%20API%20to%20get%20some%20Teams%20stats%20using%20PowerShell%20-%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fgraph%2Fapi%2Freportroot-getteamsuseractivitycounts%3Fview%3Dgraph-rest-1.0%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eref%3C%2FA%3E%20.%20The%20content%20seems%20to%20return%20values%20but%20just%20a%20long%20list%20of%20numbers%2C%20no%20rows%20or%20columns%2C%20and%20there%20is%20nothing%20to%20identify%20which%20Team%20the%20stats%20belong%20to.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAre%20there%20any%20examples%20of%20this%20working%20with%20PowerShell%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24ADALpath%20%3D%20'C%3A%5CProgram%20Files%5CWindowsPowerShell%5CModules%5CAzureADPreview%5C2.0.2.89%5CMicrosoft.IdentityModel.Clients.ActiveDirectory.dll'%20%0A%24tenantID%20%3D%20%22xxx%22%0A%24appID%20%3D%20%22xxx%22%20%0A%24client_secret%20%3D%20%22xxx%22%20%0A%0A%0Atry%20%7B%20Add-Type%20-Path%20%24ADALpath%20-ErrorAction%20Stop%20%7D%0Acatch%20%7B%20Write-Error%20%22Unable%20to%20load%20ADAL%20binaries%2C%20make%20sure%20you%20are%20using%20the%20correct%20path!%22%20-ErrorAction%20Stop%20%7D%0A%0A%0A%24authContext%20%3D%20New-Object%20%22Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext%22%20-ArgumentList%20%22https%3A%2F%2Flogin.windows.net%2F%24tenantID%22%0A%24ccred%20%3D%20New-Object%20Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential%20-ArgumentList%20%24appID%2C%24client_secret%0A%0A%24authenticationResult%20%3D%20%24authContext.AcquireTokenAsync(%22https%3A%2F%2Fgraph.microsoft.com%22%2C%20%24ccred)%0Aif%20(!%24authenticationResult.Result.AccessToken)%20%7B%20Write-Error%20%22Failed%20to%20aquire%20token!%22%3B%20return%20%7D%0A%0A%0A%24authHeader%20%3D%20%40%7B'Authorization'%3D%24authenticationResult.Result.CreateAuthorizationHeader()%7D%0A%0A%0A%0A%24uri%20%3D%20%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Freports%2FgetTeamsUserActivityCounts(period%3D'D7')%22%0A%24result%20%3D%20Invoke-WebRequest%20-Headers%20%24AuthHeader%20-Uri%20%24uri%0A%24teams%20%3D%20(%24result.Content%20%7C%20ConvertFrom-Json).Value%0A%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1460592%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMicrosoft%20Teams%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1462733%22%20slang%3D%22en-US%22%3ERe%3A%20Graph%20API%20and%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1462733%22%20slang%3D%22en-US%22%3E%3CP%3EHi%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F587571%22%20target%3D%22_blank%22%3E%40dafo43%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EReport%20only%20provides%20the%20%3CSPAN%3ETeams%20activities%20by%20activity%20type%2C%20we%20can't%20extract%20TeamsName%20from%20this%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EPlease%20use%20below%20lines%20to%20your%20script%20to%20export%20the%20data%20into%20csv%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E%24resultarray%20%3D%20ConvertFrom-Csv%20-InputObject%20%24result%3CBR%20%2F%3E%24resultarray%20%7C%20Export-Csv%20%22C%3A%5Coutput.csv%22%20-NoTypeInformation%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1462953%22%20slang%3D%22en-US%22%3ERe%3A%20Graph%20API%20and%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1462953%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%2C%20I%20can%20see%20the%20column%20names%20with%20the%20exported%20CSV%2C%20so%20that's%20enough%20to%20work%20with.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor

I'm looking to use the Graph API to get some Teams stats using PowerShell - ref . The content seems to return values but just a long list of numbers, no rows or columns, and there is nothing to identify which Team the stats belong to.

 

Are there any examples of this working with PowerShell?

 

$ADALpath = 'C:\Program Files\WindowsPowerShell\Modules\AzureADPreview\2.0.2.89\Microsoft.IdentityModel.Clients.ActiveDirectory.dll' 
$tenantID = "xxx"
$appID = "xxx" 
$client_secret = "xxx" 


try { Add-Type -Path $ADALpath -ErrorAction Stop }
catch { Write-Error "Unable to load ADAL binaries, make sure you are using the correct path!" -ErrorAction Stop }


$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList "https://login.windows.net/$tenantID"
$ccred = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential -ArgumentList $appID,$client_secret

$authenticationResult = $authContext.AcquireTokenAsync("https://graph.microsoft.com", $ccred)
if (!$authenticationResult.Result.AccessToken) { Write-Error "Failed to aquire token!"; return }


$authHeader = @{'Authorization'=$authenticationResult.Result.CreateAuthorizationHeader()}



$uri = "https://graph.microsoft.com/v1.0/reports/getTeamsUserActivityCounts(period='D7')"
$result = Invoke-WebRequest -Headers $AuthHeader -Uri $uri
$teams = ($result.Content | ConvertFrom-Json).Value

 

2 Replies
Highlighted
Solution

Hi@dafo43 

 

Report only provides the Teams activities by activity type, we can't extract TeamsName from this

 

Please use below lines to your script to export the data into csv 

$resultarray = ConvertFrom-Csv -InputObject $result
$resultarray | Export-Csv "C:\output.csv" -NoTypeInformation

 

Highlighted

Thanks, I can see the column names with the exported CSV, so that's enough to work with.