Trying to grab graph data into variable but its always empty

Hi there
I’m trying to collect data output from invoke-restmethod to graph but the variable/array is always empty at end,very weird since i remeber i had the same issue before and i forgot what i did to solve it :slight_smile:

$allteamsusingteams = @()

Define your Microsoft Graph API endpoint and access token

$graphApiEndpoint = “https://graph.microsoft.com/beta
$accessToken = “sometoken”

Make a GET request to retrieve teams data

$uri = “$graphApiEndpoint/teams”
$headers = @{
“Authorization” = “Bearer $accessToken”
“Content-Type” = “application/json”
}
$response = Invoke-RestMethod -Uri $uri -Headers $headers
$allteamsusingteams += $response.value
if ($response.‘@odata.nextLink’) {
do {
$response = (Invoke-RestMethod -Uri $response.‘@odata.nextLink’ -Headers $Headers -Method Get -ContentType “application/json”)
$allteamsusingteams += $response.value
} until (
!$response.‘@odata.nextLink
)

}
$data=@()
$allteamsusingteams|select -First 20|Foreach-Object -ThrottleLimit 20 -Parallel{
#Action that will run in Parallel. Reference the current object via $PSItem and bring in outside variables with $USING:varname
$groupinfouri = “$using:graphApiEndpoint/groups/$($psitem.id)”
$headers = @{
“Authorization” = “Bearer $using:accessToken”
“Content-Type” = “application/json”
}
#write-host $groupinfouri# this was just to verify i am getting the right url created and i do
$Response = Invoke-RestMethod -Uri $groupinfouri -Headers $headers
$allresponses += $response.value
}

the first piece collects all teams(basically to get the team/group id) that works without problems
the second part digs into each group by the id to collect extra info.
when i run that without the variable/array it output the data to screen without issues
any feedback will be welcome
thanks

Could you please format your code as code here in the forum? It really helps us distinguishing between text and code and makes it easier to copy your code easily.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )