Microsoft Fabric CLI

Microsoft Fabric CLI

Fabric Command Line Interface is a new addition to Microsoft Fabric ecosystem to enable developers easily interact with fabric items and it opens up a channel to do lot of automation.

It is a developer-first command-line tool that brings a familiar, file-system-like experience to working with Microsoft Fabric artifacts.

This blog post covers, some of the features of Fabric CLI. This feature is already generally available so you can consider Fabric CLI for production use cases too.

Please check the announcement from Microsoft Fabric blog, Fabric CLI: explore and automate Microsoft Fabric from your terminal (Generally Available) | Microsoft Fabric Blog | Microsoft Fabric It has lot of information so please go through and get a complete understanding.

You might have used multiple command line interface tools in your experience example .NET CLI, Azure CLI etc. Fabric CLI is to access Fabric artifacts in command line with simple commands. It helps you move faster, stay focused and do lot with Fabric from command line interface.

To show how easy to use Fabric CLI in reality, added a image from Microsoft blog below. It helps to understand how you can use simple command to see results. To see all the workspaces, you can simply use “ls” command same as list items in windows command prompt.

Image Credit: Microsoft Fabric Blog – Introducing the Fabric CLI (Preview) | Microsoft Fabric Blog | Microsoft Fabric

To use you need to install python library

pip install ms-fabric-cli

Once you installed the library then you can start used Fabric cli command. We have detailed documentation on all the supported commands from Microsoft Fabric team. Check here – Microsoft Fabric CLI | fabric-cli I would recommend you go through all commands.

Let us focus on some of the commands from Microsoft Fabric CLI.

It supports multiple authentication methods.

fab auth login

If you want to login with your credentials then simply choose the first option “ Interactive with a web browser” it pop up a login page where you need to login with your credentials (same one which you use to login to Microsoft Fabric / Power BI platform in browse)

Once logged in then you can start use all supported commands but you need to add fab in front of every command. If you want to avoid typing fab on every new command then you can use below command and it will take you to inside fab where you can simply type any command without fab keyword after re authentication.

fab config set mode interactive

Config set mode supports 2 values – Interactive & Command_line

If you want to change back to previous view where you want to add fab for every commend then you can use below command

config set mode command_line

Let me focus on few commands which will be really useful

Scenario : If you want to copy your artifacts from one workspace to another then use the below command.

Use “ls” to see list of workspaces. It gives you the list of workspaces with extension as .Workspace to differentiate the item type.

cp ws1.Workspace ws2.Workspace

Scenario : If you want to export the artifacts, specifically the Power BI semantic models and reports (only metadata) then you can use the export command.

export ws1.Workspace -o /tmp -a

/tmp – replace with you file path where you want to export.

Fabric CLI also supports call APIs. If you want find there is no CLI command available for any of the task then you can use APIs in CLI, if you have a API option available for that tasks

It supports Fabric API, OneLake API, ARM API for fabric capacities and Power BI API. Fabric API is defaulted.

If we want to get the list of workspaces then use the below command. It call the fabric API to list all workspaces.

api workspaces

The above result is same as what you get using API. You can use GET and POST methods on the command to handle multiple API requests.

To get all semantic models from workspace, I can use below command and this results same response as this api – GET https://api.fabric.microsoft.com/v1/workspaces/{workspaceId}/semanticModels

api -X get workspaces/<workspaceID>/semanticmodels --show_headers

Scenario: You need to do POST method. Example execute a DAX query against a semantic model. We have POWER BI api available.

To use Power BI, need to add – A powerbi

API – POST https://api.powerbi.com/v1.0/myorg/groups/{groupId}/datasets/{datasetId}/executeQueries

Use the below command. replace workspace and dataset IDs

api -A powerbi -X post groups/3c043093-31cd-438f-8f84-f57f0cde6c6f/datasets/2f3f45dd-193f-4a59-8243-9350f01c7f5d/executeQueries -i {

  "queries": [

    {

      "query": "EVALUATE VALUES(Product)"

    }

  ]

}

-i denotes inline JSON body. You can simply paste the JSON body at the end of the POST command and execute.

So far, showed some examples to use one line command. What is you want to run multiple commands at the same time. May be performing some activities using commands and If that needs to loop through every item in workspace then you can build a looping logic in python fine with all CLI commands and execute the python file.

Let me show you a simple example to run 2 different commands. Identify a specific workspace and list all items from that workspace

import subprocess
import json

#subprocess.run(['fab', 'config', 'set','mode', 'interactive'])
#subprocess.run(['fab', 'auth','login'])
result = subprocess.run(['fab', 'ls'], capture_output=True, text=True)
# Print stdout and stderr
workspaces = result.stdout.strip().splitlines()

# Filter for a specific workspace
target = 'W3.Workspace'
if target in workspaces:
    items=subprocess.run(['fab', 'ls', target])
    
    print(f"Found workspace: {target}")
    print(items.stdout)
else:
    print(f"Workspace '{target}' not found.")

Conclusion

Fabric CLI enables lot of opportunities for developers to work with Fabric ecosystems easily. We also have support APIs which brings all API capabilities natively in CLI itself. Lot of other MVPs and experts have created lot of useful content around Fabric CLI. Listed some of them. Please do check and share you feedback for this article. Let me try add more scenarios with CLI in later posts.

Fabric CLI in Fabric Notebook from Sandeep – Using Fabric CLI in Fabric Notebook

Power BI CICD from Rui Romano – GitHub – RuiRomano/fabric-cli-powerbi-cicd-sample

Hariharan

My name is Hariharan Rajendran. I am a Microsoft MVP and I have more than 14 years of experience in Data and BI technologies. This site help me to share some of my BI experience and Information about the Microsoft products, how to access the tools, how to improve your productivity with using the Microsoft tools. I am also running a Youtube Channel. If you are interested, then check at Hari’s BI - https://www.youtube.com/@HariBI/videos

Leave a Reply