I’m currently working on a solution at work which is ultimately a contribution to our process of trying to keep on top of our proof of concept environments usage of networking and in particular ip address ranges. We have a rolling set of Azure Virtual Networks that vary in size from a class C to the occasional class A when we have a silly scale HPC or Kubernetes CNI requirement for a gazillion addresses in a big subnet.
The solution is coming together in very small building blocks and this post is to provide me (and you interwebs folks) with a reference to the filter syntax for List all teams in Microsoft Teams using Microsoft Graph.
Although the automation method shouldn’t really matter for what is effectively a big REST API, you know how it is when you have to translate syntax and fiddle around with quotation marks and things. Anyway, to cut a long story short the rough PowerShell script for List all teams in Microsoft Teams in PowerShell is:
# PowerShell to list all teams in your tenant
# Assumes you have set up your certificate authentication
$appId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$tenantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$cert = Get-AutomationCertificate -Name 'AzureAutomationCertificate'
# Magic we are doing needs beta apis for the filter to work
Select-MgProfile -Name "beta"
# Authenticate to MS Graph
Connect-MgGraph -ClientID $appId -TenantId $tenantId -Certificate $cert
# Get list of Teams i.e. Groups with the special resource provisioning options set
$teams = Get-MgGroup -Filter "resourceProvisioningOptions/Any(x:x eq 'Team')"
$teamscount = @($teams).Count
Write-Verbose "The number of teams is $teamscount" -Verbose
# Close Connection to MS Graph
Disconnect-MgGraph
A few caveats and notes
- This isn’t a full working example, in my case I’m using Azure Automation Runbooks and they are very very particular about their outputs and object handling. I’m still working on my translation.
- It assumes you have done the work to create a self-signed certificate, create the app registration, uploaded the certificate to the app registration *and* set it up in your automation account. (I might do a meta post on this as I found one blog post that had the wrong parameters for the cert generation and generated a cert file of format cer with a pfx extension…)
- This is being written for an Azure Automation Account in PowerShell, remember to add the relevant modules that are needed. I was adding individual modules as I found them first but you will probably be quicker just using Microsoft.Graph – you will find it in the gallery. Otherwise for the above you will need Microsoft.Graph.Authentication, and Microsoft.Graph.Groups.
- If your tenant is anything like ours then you will always get 100 as the count of teams, due to the way that the apis manage their output length.
- The script doesn’t do anything useful but I thought it might help to see the filter syntax
References, Source Material and Inspiration
- List all teams in Microsoft Teams for an organization covers the important parts for doing this from MSGraph (rather than teams) in REST format.
- Navigating the Microsoft Graph PowerShell SDK is a very very useful page that covers how to translate the format of the majority of the MSGraph documentation in to something that you need for PowerShell work.
- Get-MgGroup (Microsoft.Graph.Groups) is the official documentation page on the PowerShell and while spot on from a descriptive point of view is light on examples (well it was for what I was after).
- A GitHub comment on a different issue by Darrel Miller which had a really really useful example of a filter in URL and PowerShell. This comes under the inspirational banner and got me to the solution above.
How life goes in circles
That a significant pointer would be found in a response on GitHub by Darrel Miller is quite fascinating. I met Darrel on the expo floor at Microsoft Ignite in Orlando in 2018 and only really because I was after some “Swag” and had to get a card stamped by various Product Managers and Architects on the Microsoft 365 stand. At the time I was up to my neck in Azure and trying my best to get away from SharePoint (and Microsoft 365) and my discussions with the people on those stands were all to try and get me to talk to Graph and get back in to SharePoint Development with the new SPFx thing.
So it’s taken me about 2 and a half years, but I’m finally getting there. Thankyou Darrel – check him out on twitter etc!