Search code examples
pythonpowershellazure-cli

Az.cmd closes immediately


When I Run az.cmd on my windows 10 screen some information flashes by and the screen closes. When I right click the shortcut and choose Edit I see

:: :: Microsoft Azure CLI - Windows Installer - Author file components script :: Copyright (C) Microsoft Corporation. All Rights Reserved. ::

@IF EXIST "%~dp0..\python.exe" ( SET AZ_INSTALLER=MSI
"%~dp0..\python.exe" -IBm azure.cli %* ) ELSE ( echo Failed to load python executable. exit /b 1 )

I reinstalled python and rebooted but this did not help.

I managed to capture this as it flashed by Screen capture


Solution

  • Executing az.cmd is a direct way of running the Azure CLI tool, which you can see if you run:

    PS C:\Users\joe.bloggs> get-command az
    
    CommandType     Name      Version    Source
    -----------     ----      -------    ------
    Application     az.cmd    0.0.0.0    C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd
    

    Since the screenshot you grabbed is the help text for the az CLI tool, it's doing exactly what it should: it checks for the presence of a Python executable where it expects it, and if it can find it, it executes Python with the azure.cli module.

    That's what this line does:

    "%~dp0..\python.exe" -IBm azure.cli %*
    

    It starts python.exe from the parent folder of the script, running it in isolated mode (-I), not writing any bytecode like .pyo or .pyc files (-B), and running a module as a script (-m) with azure.cli being the module to run.

    The %* ensures that any arguments passed to az on the command line are passed to the script / module.

    Since you're not giving it any arguments when you just double-click it, it just prints the help text and then terminates. If you open a Command Prompt or PowerShell window, and run az, you'll see the same text.

    If you pass it some useful arguments, it can do pretty much anything on Azure from the CLI. You could create a shortcut that runs az.cmd with some useful arguments, if you need a shortcut that runs a specific command on Azure. If you just want to interact with Azure from the CLI, you can just start a Command Prompt or PowerShell and run az commands from there.