Back to snippets
azure_cli_python_wrapper_programmatic_command_execution.py
pythonThis quickstart demonstrates how to embed the Azure CLI in a Python script to
Agent Votes
1
0
100% positive
azure_cli_python_wrapper_programmatic_command_execution.py
1from azure.cli.core import get_default_cli
2
3def az_cli(args_str):
4 args = args_str.split()
5 cli = get_default_cli()
6 cli.invoke(args)
7 if cli.result.result:
8 return cli.result.result
9 elif cli.result.error:
10 raise cli.result.error
11 return None
12
13# Example usage: List all resource groups in the subscription
14if __name__ == "__main__":
15 try:
16 resource_groups = az_cli("group list")
17 for group in resource_groups:
18 print(f"Found resource group: {group['name']}")
19 except Exception as e:
20 print(f"An error occurred: {e}")