4

敲指令管 Azure,該用 Azure CLI 還是 Azure PowerShell?

 1 year ago
source link: https://blog.darkthread.net/blog/azure-cli-or-azure-powershell/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

敲指令管 Azure,該用 Azure CLI 還是 Azure PowerShell?-黑暗執行緒

上回提過,這些年我已成為 CLI 的愛好者。所以學 Azure 我還是先從 Azure Portal GUI 操作開始,上手做到第三遍第四遍多半會忍不住改用 CLI 下指令。像是之前介紹 Azure Key Vault建立磁碟快照與還原時,我都順帶筆記了 PowerShell 寫法。

不過眼尖的讀者可能有發現,在最近的 AD Forest 實驗,我不是用 Azure PowerShell Cmdlet 批次關機,而是用上了 Azure CLI

az vm deallocate --ids $(az vm list --resource-group ad-forest-lab --query "[].id" -o tsv)

上一篇講批次更新 RDP/SSH 開放來源 IP更是搞了一手,Azure CLI JSON 轉 PowerShell 的串燒式玩法。讀者 Kuo Chin-Yuan 問起,我才發現忘了交代 Azure PowrShell 換 Azure CLI 心路歷程。

針對 Azure 指令管理,微軟同時提供兩種解決方案: Azure CLI 跟 Azure PowerShell,二者功能幾乎相同,使用者可以擇一使用,微軟甚至有篇官方文件教你如何選擇

thumbnail

對我而言,同時學兩套太累,腦子也裝不下,得二者擇一。經過評估,我最終選了 Azure CLI,下面來說理由。

第一,同樣動作,Azure CLI 比較簡短,可少打幾個字,也比較好記:

Fig1_638130550933238333.png

第二,Azure CLI 跟 Azure PowerShell 都能跨平台在 Windows、macOS、Linux 執行。但 Azure CLI 在 Cmd、Bash 也能直接使用,若熟悉 Bash 腳本語法,甚至可以取代 PowerShell 寫 if、使用變量、跑迴圈... 等等複雜邏輯 參考
(所以,若你對 Bash 比 PowerShell 熟,Azure CLI 絕對是首選。我只會 PowerShell 還不懂 Bash 腳本,但是我未來想學會的技能)

if [ $(az group exists --name $resourceGroup) = true ]; then 
   az group delete --name $resourceGroup -y # --no-wait
else
   echo The $resourceGroup resource group does not exist
fi

var=$(az group list --query "[? contains(name, '$resourceGroup')].name" --output tsv)
case $resourceGroup in
$var)
echo The $resourceGroup resource group already exists.;;
*)
az group create --name $resourceGroup --location "$location";;
esac

for i in `seq 1 3`; do
    echo $randomIdentifier > container_size_sample_file_$i.txt
done

最後一點其實最關鍵。最近這陣子看了一些文件、討論,Azure CLI 出現的機率比 Azure PowerShell 多。即便在 PowerShell 環境,不少人也習慣寫 az vm stop 例 子 例 2 而非 Stop-AzVM。 微軟官方文件大多會 Azure CLI 與 Azure PowerShell 並列,但在 Azure 較進階的教學文件裡,可能只有 Azure CLI 示範 例子,我好像嗅到了什麼... 有點像當初面臨 VB.NET 跟 C# 的選擇,即便策略上二者並進,但會由使用者與社群決定故事如何發展,想擁獲得更多資源,選比較多人用跟高手愛用的那個就對了!

結論:最終我選擇 Azure CLI 當我的主要 Azure 指令工具,在需要延伸應用時,Azure CLI 也能完美串接 PowerShell,邏輯再複雜也不怕,等未來學會 Bash 腳本,還能實現雙語皆通,左右開弓,應該蠻酷的。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK