39

I am trying to use a bash (sh) script on windows to run a test deployment. I am running the script from the gitbash console so that I have a copy of bash, but doing so means that the azure clie is not available (i.e. azure command is not found). Does anyone know how I can get the Azure cli working in GitBash (I am assuming I just install it somewhere else) or should I change to a different way of using bash

Pectus Excavatum
  • 3,593
  • 16
  • 47
  • 68

10 Answers10

59

Sometimes commands in windows git bash need .cmd appended. Also, another way of installing the Azure-Cli is through Chocolatey https://chocolatey.org/

Try this command after Azure-Cli is installed:

az.cmd --version

Echoing mscrivo you can run the line below in CMD not PowerShell (elevated/admin)

echo "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15} > "C:\Program Files\Git\mingw64\bin\az" 

Now you should be able to run in Git bash:

az --version
whindes
  • 690
  • 5
  • 8
  • 8
    There is also the option to create a bash alias as is stated in the issue link provided by @citleon https://github.com/Azure/azure-cli/issues/3445 . Adding `alias az='az.cmd'` to your .bashrc file will do the trick. – artberri Jun 10 '19 at 08:27
31

artberri noted the best solution in a comment:

Add the following to your %USERPROFILE%\.bashrc or %USERPROFILE%\.profile

alias az='az.cmd'

However, if you want to be able to use az in bash scripts, you'll need something a little more drastic, run the following from cmd prompt:

echo "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15} > "%SYSTEMROOT%\az"

That will essentially create a passthrough az command in your windows folder that can be run from anywhere and passes parameters through to az.cmd.

mscrivo
  • 1,107
  • 12
  • 14
  • This is the only thing that worked for me. The solution above did not. – emirhosseini Mar 25 '19 at 18:25
  • This worked. Just a little change to get mklink to work - cmd /c mklink "%SYSTEMROOT%\az" "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" – J K Apr 25 '19 at 11:13
  • 3
    `$SYSTEMROOT` was pointing to `c:\windows` in my computer. I didn't want to have the `az` link there. Instead you can also put it in `\mingw64\bin` which is also in your git bash path. – atavio May 12 '19 at 09:55
  • 1
    I could not write wiql queries with that solution. Either `az: error: unrecognized arguments: [Id],[Title] FROM WorkItems WHERE [AssignedTo]=@Me` showed up when I used `"` or `'C:\Program' is not recognized as an internal or external command` using `'` to write the query in argument – veritas Jan 20 '20 at 20:29
  • This works nicely unless one of our arguments is a multiline string., then the quoting is all messed up and this is why @veritas code fails. Anny suggestions on how to fix it? – TheMP Apr 29 '20 at 13:43
10

In case using Git bash, navigate to the following directory:

C:\Program Files\Git\etc\profile.d

Windows Explorer of Git bash Directory

Edit aliases.sh, then add a new alias for az as below:

alias az='az.cmd'

example of aliases.sh

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Hussein Salman
  • 7,806
  • 15
  • 60
  • 98
  • 1
    Worked for me! Strange that somehow "az.cmd" is already "known" by Git Bash but not "az" – Valentin Calomme Mar 20 '20 at 09:32
  • 1
    Not really that strange as Windows uses .bat, .cmd and .exe for executables unlike Linux that uses no extensions for executables. So adding an alias for the az.cmd makes perfect sense. Also if you take a peek in the wbin folder of the az cli installation you will see that it only contains the file az.cmd. – Michael Apr 28 '20 at 11:48
6

You have to install the CLI to your computer. There are multiple ways to do that.

I'm a friend of NodeJS so i use npm for the installation:

npm install -g azure-cli

More details here: https://www.npmjs.com/package/azure-cli

But you can do it also in other ways. A very nice way is to use docker. There are containers from Microsoft with a preinstalled version of Azure CLI.

docker run -it --name azure microsoft/azure-cli

On Windows 10 with the ubuntu bash you can use:

echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ wheezy main" | \
     sudo tee /etc/apt/sources.list.d/azure-cli.list

sudo apt-key adv --keyserver packages.microsoft.com --recv-keys 417A0893
sudo apt-get install apt-transport-https
sudo apt-get update && sudo apt-get install azure-cli

Or as a python enthusiast run

pip install --user azure-cli

Most important is that the "az"/"az.bat" or "azure" bin is available via your path variable.

blndev
  • 494
  • 3
  • 13
2

In other words Azure CLI for Windows is not compatible with Git Bash for Windows

https://github.com/Azure/azure-cli/issues/3445

citleon
  • 378
  • 2
  • 7
2

In your ~/bin directory (in Windows it means c:\Users\<username>\bin) I've just created a file named az with:

"C:/Program Files (x86)/Microsoft SDKs/Azure/CLI2/python.exe" -IBm azure.cli "$@"

Then make the file executable chmod a+x az

The content is "borrowed" from az.cmd.

When trying an alias approach mentioned before I had a problem with long commands, with a lot of parameters throwing an error "'C:\Program' is not recognized as an internal or external command,operable program or batch file."

EDIT:

I've ended using Ubuntu through WLS. For all tools like az, terraform, kubectl, istioctl. The az tool runs good in interactive mode as well.

Piotr Gwiazda
  • 12,080
  • 13
  • 60
  • 91
1

So I arrived here looking for a way to run the same az commands in a bash shell script on Azure DevOps Linux and Windows (git bash) build agents, so I could share the same code across both types of agent. This also works for a git bash shell on Windows 10. My longer answer is here: Azure DevOps Build Pipeline can't get secrets from Key Vault when secured with vnet and firewall

The gist of it is:

if [[ $(uname -s) == "Linux" ]]; then
    azcmd="az"
else
    # If we're in a bash shell on Windows, az commands don't work, but we can call the az.cmd batch file directly from git Bash if we can find it...
    azcmd=$(where az.cmd)
fi

# Remember to specify CIDR /32 for removal
"$azcmd" keyvault network-rule remove -n <MyKeyVault> --ip-address 50.1.1.1/32

Basically just substitute "$azcmd" wherever you'd normally use az once the bootstrap code has executed.

vipes
  • 922
  • 1
  • 9
  • 17
1

My previous approach for this was just to add the Azure CLI Scripts folder to the $PATH inside my ~/.bashrc file. But, after updating Azure CLI to 2.2.0 via the MSI, this approach started to fail with this error:

C:\Program: can't open file 'Files': [Errno 2] No such file or directory

So, to fix this, I have my $PATH in ~/.bashrc include ~/bin and then I created a file with the following content as ~/bin/az (don't forget to chmod 0755 the new file):

#!/usr/bin/env bash

AZURE_CLI_PATH="/c/Program Files (x86)/Microsoft SDKs/Azure/CLI2"

export PATH="${PATH}:${AZURE_CLI_PATH}:${AZURE_CLI_PATH}/Scripts"
export PYTHONPATH="${AZURE_CLI_PATH}/src"
export PYTHON_CMD="$(which python)"

winpty "${PYTHON_CMD}" -m 'azure.cli' "${@}"

After closing out my GIT Bash window and re-opening it, I can now run az again:

$ az version
This command is in preview. It may be changed/removed in a future release.
{
  "azure-cli": "2.2.0",
  "azure-cli-command-modules-nspkg": "2.0.3",
  "azure-cli-core": "2.2.0",
  "azure-cli-nspkg": "3.0.4",
  "azure-cli-telemetry": "1.0.4",
  "extensions": {}
}
GuyPaddock
  • 2,233
  • 2
  • 23
  • 27
0

Don't use the MSI installer at all. Since the Azure CLI is implemented in Python, use the Python installation method as @blndev wrote. This way instead of az.cmd you get az.bat and az shell script, and the installation path will not contain spaces.

pip install --user azure-cli

More detailed info on this method can be found at https://blogs.msdn.microsoft.com/brijrajsingh/2017/03/02/installing-azure-cli-2-0-on-windows/

The symlink worked for me most of the times, but some commands are still failing, e.g.

az dls fs access set-entry ...
'C:\Program' is not recognized as an internal or external command, operable program or batch file
István
  • 69
  • 2
  • 5
-1

I tried @mscrivo's solution. When using the az command in a shell script however you still have problems due to the spaces in the path. Therefor I created a azproxy.cmd in %SYSTEMROOT% containing

@echo off
"C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" %*

And then create the mklink to that file

mklink "%SYSTEMROOT%\az" "SYSTEMROOT%\azproxy.cmd"

PS the expanded value of %SYSTEMROOT% should not contain spaces of course

Nelis
  • 54
  • 4