Write interactive scripts using the Office 365 CLI


Often, I need to check some particular property of an object in Office 365. Is a particular flow enabled, what is its ID, what is the ID of that Team, what’s the template of this list? Office 365 CLI is pretty convenient for doing this. It works on any platform and in any shell, so no matter if I’m on my mac or in my Windows VM, in Bash or PowerShell, I can get the information I need in the same consistent way. There is just one thing, that could’ve been better.

Inconvenient retrieving objects in Office 365

To retrieve a particular object in Office 365, you need to know something that uniquely identifies it: ID, title or URL. If you work with a particular environment a lot, you might know the title or the URL. More than often though, I catch myself forgetting some element of it, a special character, a word. IDs are even worse. In most cases they’re GUIDs. In other, they’re some other arbitrary values that you’re unlikely to remember. So in the end, I need multiple steps: list all objects of the particular type, lookup the one that I need, copy its ID and get its information. What if there was an easier way to do it?

iTerm on macOS showing commands for retrieving information about a specific Team using Office 365 CLI

Writing interactive scripts to retrieve objects using the Office 365 CLI

What if instead of manually typing the commands and copying the values, we could run an interactive script that would do it for us? It would start with retrieving all values of the given type, present us with a list to select the object that we’re interested in and then retrieve information about the object we selected.

Writing interactive Office 365 CLI scripts in Bash

In Bash, it would look like this:

#!/bin/bash

# requires jq: https://stedolan.github.io/jq/

echo "Retrieving sites..."
echo

sitesJson=$(o365 spo site list --query '[].Url' --output json)
sites=( $(jq -r '.[]' <<< "$sitesJson") )

echo "Sites in your tenant:"
echo
PS3='Select site to retrieve: '
select site in "${sites[@]}"
do
  echo
  echo "Retrieving site $site..."
  echo
  o365 spo site get --url $site
  break
done

We start with retrieving all sites using the spo site list command (line 8). Using the query option, we specify that we’re interested only in the URL. Because we’re building a script and will need to process the output later on, we set its format to JSON.

Next, we translate the retrieved site URLs into a Bash array (line 9). Because Bash doesn’t have a native way to process JSON strings, we do this using jq. The way we handle the different array elements assumes that the values have no spaces. If they would, we’d need to adjust the code.

Then, we use the Bash PS3 prompt (line 13) to prompt the user to select a site. The prompt is displayed after calling the select loop which prints the URLs of all retrieved sites (line 14). While the expected answer is the number of the site to retrieve, it’s automatically translated to the URL of the corresponding site which is assigned to the site variable.

Finally, with the URL of the selected site available to us, we can retrieve the information about the site using the spo site get command.

iTerm window on macOS showing the interactive script for retrieving information about a specific site using the Office 365 CLI

Writing interactive Office 365 CLI scripts in PowerShell

Writing interactive scripts using PowerShell is very similar.

Write-Host "Retrieving sites..."
Write-Host

$sites = o365 spo site list --query '[].Url' --output json | ConvertFrom-Json

Write-Host "Sites in your tenant:"
Write-Host
$i = 0;
$sites | ForEach-Object { "{0}: {1}" -f ++$i, $_ }
Write-Host
$site = Read-Host "Select site to retrieve"
Write-Host
Write-Host $("Retrieving site {0}..." -f $sites[$site-1])
Write-Host
o365 spo site get --url $sites[$site-1]

Because PowerShell supports parsing JSON objects natively so after retrieving the list of site URLs, we can transform them into an array by piping the output to the ConvertFrom-Json cmdlet (line 4).

Next, we print the list of retrieved URLs. To automatically increase the number of each site in the list we use string formatting (line 9).

After printing the list of retrieved URLs, we prompt the user to select a site (line 11).

After selecting the site, we pass its URL to the spo site get Office 365 CLI command to get its information.

Summary

Interactive scripts significantly simplify retrieving information about objects in Office 365. Instead of running the different commands manually and copying values between them, you can run the script and easily get to the information you need.

In the above example, I’ve shown you how to write a simple script that retrieves the information about the particular site in your tenant using the Office 365 CLI. You could use it as a starting point to build more complex scripts, like using search to get a wide range of objects, extend the script to show the meaningful title of the retrieved object but use its ID to retrieve its details or combine multiple levels in object hierarchy like retrieving apps installed in a specific site. And it’s not even limited to Office 365 CLI. You could do the same using the SPO or PnP PowerShell.

The possibilities are endless. So, what are you going to use this concept for?

Others found also helpful: