Git Bash 中的 Azure CLI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42972086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Azure CLI in Git Bash
提问by Pectus Excavatum
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
我正在尝试在 Windows 上使用 bash (sh) 脚本来运行测试部署。我正在从 gitbash 控制台运行脚本,以便我拥有 bash 的副本,但这样做意味着 azure clie 不可用(即未找到 azure 命令)。有谁知道我如何让 Azure cli 在 GitBash 中工作(我假设我只是将它安装在其他地方)或者我应该更改为使用 bash 的不同方式
回答by whindes
Sometimes commands in windows git bash need .cmd appended. Also, another way of installing the Azure-Cli is through Chocolatey https://chocolatey.org/
有时 windows git bash 中的命令需要附加 .cmd。此外,另一种安装 Azure-Cli 的方法是通过 Chocolatey https://chocolatey.org/
Try this command after Azure-Cli is installed:
安装 Azure-Cli 后试试这个命令:
az.cmd --version
Echoing mscrivoyou can run the line below in CMD not PowerShell(elevated/admin)
回应mscrivo,您可以在 CMD而非 PowerShell(提升/管理员)中运行以下行
echo "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" > "C:\Program Files\Git\mingw64\bin\az"
Now you should be able to run in Git bash:
现在你应该可以在 Git bash 中运行了:
az --version
回答by mscrivo
artberri noted the best solution in a comment:
artberri 在评论中指出了最佳解决方案:
Add the following to your %USERPROFILE%\.bashrc
or %USERPROFILE%\.profile
将以下内容添加到您的%USERPROFILE%\.bashrc
或%USERPROFILE%\.profile
alias az='az.cmd'
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:
但是,如果您希望能够在 bash 脚本中使用 az,则需要更激烈的操作,请从 cmd 提示符运行以下命令:
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"
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.
这实际上将在您的 windows 文件夹中创建一个 passthrough az 命令,该命令可以从任何地方运行并将参数传递给 az.cmd。
回答by Hussein Salman
回答by blndev
You have to install the CLI to your computer. There are multiple ways to do that.
您必须将 CLI 安装到您的计算机上。有多种方法可以做到这一点。
I'm a friend of NodeJS so i use npm for the installation:
我是 NodeJS 的朋友,所以我使用 npm 进行安装:
npm install -g azure-cli
npm install -g azure-cli
More details here: https://www.npmjs.com/package/azure-cli
更多细节在这里: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。有来自 Microsoft 的容器,其中预装了 Azure CLI 版本。
docker run -it --name azure microsoft/azure-cli
On Windows 10 with the ubuntu bash you can use:
在带有 ubuntu bash 的 Windows 10 上,您可以使用:
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
或者作为一个 python 爱好者运行
pip install --user azure-cli
Most important is that the "az"/"az.bat" or "azure" bin is available via your path variable.
最重要的是“az”/“az.bat”或“azure”bin 可通过您的路径变量获得。
回答by citleon
In other words Azure CLI for Windows is not compatible with Git Bash for Windows
换句话说,适用于 Windows 的 Azure CLI 与适用于 Windows 的 Git Bash 不兼容
回答by Piotr Gwiazda
In your ~/bin
directory (in Windows it means c:\Users\<username>\bin
) I've just created a file named az
with:
在您的~/bin
目录中(在 Windows 中,这意味着c:\Users\<username>\bin
)我刚刚创建了一个名为的文件az
:
"C:/Program Files (x86)/Microsoft SDKs/Azure/CLI2/python.exe" -IBm azure.cli "$@"
Then make the file executable
chmod a+x az
然后使文件可执行
chmod a+x az
The content is "borrowed" from az.cmd.
内容是从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."
在尝试之前提到的别名方法时,我遇到了长命令的问题,很多参数抛出错误“'C:\Program' 不是内部或外部命令,也不是可运行的程序或批处理文件。”
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.
我已经通过 WLS 结束了使用 Ubuntu。适用于所有工具,如 az、terraform、kubectl、istioctl。az 工具在交互模式下也运行良好。
回答by GuyPaddock
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:
我以前的方法只是将 Azure CLI Scripts 文件夹添加到$PATH
我的~/.bashrc
文件中。但是,在2.2.0
通过 MSI将 Azure CLI 更新为之后,这种方法开始失败并出现以下错误:
C:\Program: can't open file 'Files': [Errno 2] No such file or directory
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):
所以,为了解决这个问题,我有我$PATH
的~/.bashrc
包含~/bin
,然后我创建了一个包含以下内容的文件~/bin/az
(不要忘记chmod 0755
新文件):
#!/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:
关闭我的 GIT Bash 窗口并重新打开它后,我现在可以az
再次运行:
$ 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": {}
}
回答by vipes
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
因此,我来到这里寻找一种方法,可以在 Azure DevOps Linux 和 Windows (git bash) 构建代理上的 bash shell 脚本中运行相同的 az 命令,以便我可以在两种类型的代理之间共享相同的代码。这也适用于 Windows 10 上的 git bash shell。我更长的答案在这里:Azure DevOps Build Pipeline can't get secrets from Key Vault when using 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.
基本上只需在引导程序代码执行后替换"$azcmd"
您通常使用的任何地方az
。
回答by István
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.
根本不要使用 MSI 安装程序。由于 Azure CLI 是用 Python 实现的,所以使用 @blndev 写的 Python 安装方法。这样az.cmd
你就不用getaz.bat
和az
shell脚本了,安装路径也不会有空格。
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/
有关此方法的更多详细信息,请访问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
回答by Nelis
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
我尝试了@mscrivo 的解决方案。但是,在 shell 脚本中使用 az 命令时,由于路径中的空格,您仍然会遇到问题。因此我在 %SYSTEMROOT% 中创建了一个 azproxy.cmd 包含
@echo off
"C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" %*
And then create the mklink to that file
然后创建指向该文件的 mklink
mklink "%SYSTEMROOT%\az" "SYSTEMROOT%\azproxy.cmd"
PS the expanded value of %SYSTEMROOT% should not contain spaces of course
PS %SYSTEMROOT% 的扩展值当然不应该包含空格