Data Bear

Fabric CLI Automation: Setup Workspaces Fast

Semantic Model in Power BI

Fabric CLI automation is a powerful way to streamline repetitive admin tasks in Microsoft Fabric. If you’re managing Power BI or Fabric workspaces manually, it’s time to simplify that process. In this guide, you’ll learn how to use Fabric CLI to automate workspace creation and user role assignments boosting efficiency and ensuring consistency across your organization.

In this blog post, inspired by a training scenario from a Fabric Boot Camp, we’ll walk through how to automate:

  • Workspace creation in Microsoft Fabric
  • Assigning contributor access based on Azure AD group membership
  • Leveraging Python and Notebooks to orchestrate these tasks using Fabric CLI

You can follow along using this Microsoft Fabric CLI documentation and explore Power BI training options at Data Bear’s Power BI Training page.

Scenario: Boot Camp Workspace Setup Problem

During a multi-day Fabric Boot Camp, each participant needs their own Fabric workspace. Manually creating accounts, workspaces, and assigning access quickly becomes inefficient  especially at scale.

To solve this, we’ll automate workspace provisioning using:

  • Azure Active Directory groups
  • A downloadable CSV of group members
  • Fabric Notebooks
  • Microsoft Fabric CLI
  • Python functions for workflow automation
Step 1: Preparing the Environment

Start by downloading your Azure AD group members as a CSV file from the Azure portal. Load this file into your OneLake environment and access it via a Fabric Notebook.

Example File:
fabric-bootcamp-group-members.csv

Install the Fabric CLI in your notebook environment:

!pip install microsoft-fabric-cli

This makes all CLI commands available directly within the notebook.Preparing the Environment

Step 2: Load Group Members into a DataFrame

Use a simple Python script to load the CSV:

df = spark.read.option("header", "true").csv("Files/fabric-bootcamp-group-members.csv")
df.display()

Make sure the dataframe includes at least two key columns:

  • Display Name
  • ID (Entra ID or Object ID)Load Group Members into a DataFrame Fabric CLI automation
Step 3: Create a User Dictionary

To automate workspace naming and access control, transform your data into a Python dictionary:

user_info = {row['DisplayName']: row['Id'] for row in df.select("DisplayName", "Id").collect()}

Now you’ll have a user_info dictionary like:

{
    "John Smith": "abc123entraID",
    "Jane Doe": "xyz456entraID"
}Create a User Dictionary Fabric CLI automation
Step 4: Format Workspace Names

Let’s generate consistent, branded workspace names based on users:

formatted_user_info = {}

for name in list(user_info.keys()):
    first_initial = name[0].lower()
    last_name = name[name.find(" ") + 1:].lower()
    workspace_name = f"fabric-{first_initial}{last_name}"
    
    formatted_user_info[workspace_name] = user_info.pop(name)

This ensures every workspace follows the naming convention: fabric-jdoe, fabric-jsmith, etc.

Step 5: Authenticate with Fabric CLI in Notebooks

Before executing CLI commands, authenticate your notebook:

import os
from notebookutils import mssparkutils

token = mssparkutils.credentials.getToken('PowerBI')
os.environ["FABRIC_TOKEN"] = token
os.environ["ONELAKE_TOKEN"] = token

Tip: In PowerShell environments, you can also authenticate using MFA or managed identities.Authenticate with Fabric CLI in Notebooks Fabric CLI automation

Step 6: Automatically Create Fabric Workspaces

Define a function to create workspaces using the fab workspace create command:

def create_workspaces(workspace_names):
    for name in workspace_names:
        !fab workspace create --name {name} --capacity-name "pw-demo-trial-capacity"

Execute it with:

create_workspaces(list(formatted_user_info.keys()))

Your workspaces are now being created automatically.

Step 7: Assign Contributor Access to Workspaces

Define a function to assign each user Contributor access to their workspace using fab acl set:

def assign_access(workspace_info):
    for workspace, user_id in workspace_info.items():
        !fab acl set --workspace {workspace} --user {user_id} --role Contributor --force

Run the function:

assign_access(formatted_user_info)

This will grant each user contributor access to their assigned workspace.

Step 8: Validate Workspace and Role Assignment

You can verify that each workspace was created and that the correct users were assigned by checking:

  • Fabric UI: Go to the Workspaces section and inspect each.
  • Notebook output: Successful CLI messages should show assigned roles.

If a user already has a role, the CLI will notify you (e.g., if the creator is already an admin).

Using CLI Help for Fabric Automation

Fabric CLI includes helpful in-line documentation. Just run:

fab acl set --help

Or use it for any command:

fab --help

This makes scripting much easier as you explore the vast capabilities of Fabric CLI.

Final Thoughts: Streamline Your Admin Workflows

This tutorial showcases how Fabric CLI can dramatically improve your efficiency when provisioning Microsoft Fabric workspaces. With a few functions and some smart scripting, you can:

  • Scale workspace creation
  • Automate user access
  • Improve governance and consistency across your tenant
Try it yourself:
  • Explore the full Fabric CLI documentation
  • Join the Power BI Training to level up your skills