選擇正確的 Azure 命令列工具

在管理 Azure 方面,您有許多選項。 本文會比較 Azure CLI 和 Azure PowerShell 語言,並提供其執行所在的殼層環境比較。

Azure CLIAzure PowerShellAzure Cloud Shell 具有重疊的功能。 每個作業的運作方式都不同,語言有時會與環境混淆。 使用本指南來判斷哪一個是您的正確工具。

使用 Azure 命令行工具的優點為何?

Azure 會在自動化上執行。 您在入口網站內採取的每個動作都會轉譯為執行的程式代碼,以讀取、建立、修改或刪除資源。

將工作負載移至 Azure 可提升部分系統管理負擔,但並非全部。 隨著 Azure 採用率的提高,額外負荷也會增加。 即使您不需要擔心數據中心,您仍然需要修補和疑難解答 Azure VM、故障轉移資料庫,以及設定虛擬網路。

藉由使用執行 Azure 的現有自動化,命令行工具可減少該額外負荷。

什麼是 Azure 命令行工具?

Azure 命令行工具會將例行作業自動化、標準化資料庫故障轉移,以及提供強大見解的提取數據。 命令行工具不僅可讓您在 Azure 中調整工作,還能讓您更輕鬆地共用工作。 使用耗時的螢幕快照,共用腳本比冗長的Wiki頁面容易得多。

使用 Azure 命令行工具不一定是必要的,但具備的技能相當實用。

Azure CLI 與 Azure PowerShell

Azure CLI 和 Azure PowerShell 是命令行工具,可讓您建立和管理 Azure 資源。 這兩者都是跨平臺,可在 Windows、macOS 和 Linux 上安裝。

Azure CLI

  • 跨平台命令列介面,可在 Windows、macOS、Linux 上安裝
  • 在 Windows PowerShell、Cmd 或 Bash 和其他 Unix 殼層中執行。

Azure PowerShell

  • 跨平台 PowerShell 模組,在 Windows、macOS、Linux 上執行
  • 需要 Windows PowerShell 或 PowerShell

不同的殼層環境

殼層環境 Azure CLI Azure PowerShell
Cmd Yes
Bash Yes
Windows PowerShell Yes Yes
PowerShell Yes Yes

Windows PowerShell、PowerShell、Cmd 和 Bash 都是殼層環境。 您的殼層環境不僅會決定您可以使用哪些工具,也會變更您的命令列體驗。

例如,針對行接續字元,Bash 會使用反斜杠 \ ,而 Windows PowerShell 則使用反引號 `。 殼層環境中的差異不會變更 Azure CLI 和 Azure PowerShell 的運作方式。 不過,其確實會變更您的命令列體驗。

Azure CLI 具有安裝程式,可在所有四個殼層環境中執行其命令。

Azure PowerShell 是封裝為名為 Az之 PowerShell 模組的 Cmdlet 集合,而不是可執行檔。 Windows PowerShell 或 PowerShell 必須用來安裝 Az 模組。

Windows PowerShell 是預先安裝於大部分 Windows 作業系統的標準指令碼殼層。 PowerShell 是獨立式安裝,其使用 .NET Core 作為執行階段,允許其安裝在 macOS、Linux 和 Windows 上。

重點︰

  • AzureRM 是一個 PowerShell 模組,仍使用 PowerShell 針對 Azure 系統管理參考。 不過,Azure PowerShell 已取代它,並正式淘汰日期為 2024 年 2 月 29 日。

如果您使用 AzureRM,您可以將 Azure PowerShell 從 AzureRM 遷移至 Az

Azure Cloud Shell 呢?

Azure Cloud Shell 是在 Ubuntu 容器上執行的託管殼層環境。

Cloud Shell 提供兩個殼層環境:Bash(搭配 Azure CLI 預安裝)和 PowerShell(已預安裝 Azure PowerShell)。 Cloud Shell 視窗頂端的下拉式清單可讓您輕鬆地在兩個環境之間切換。

Cloud Shell 可在網頁瀏覽器中存取,並具有適用於 Windows 終端機 和 Visual Studio Code 的整合

Azure CloudShell Bash and PowerShell environments

注意

Azure Cloud Shell 不一定會 立即反映 最新的 Azure PowerShell 和 Azure CLI 版本,因為這三個工具的發佈排程都不同。 不過,一般認為 Azure Cloud Shell 一律包含這兩個工具的最新版本。

哪一項 Azure 命令列工具適合您使用?

挑選正確的工具時,請考慮過去經驗和目前的工作環境。

Azure CLI 語法類似於 Bash 指令碼的語法。 如果您主要使用 Linux 系統,Azure CLI 會讓您感覺更自然。

Azure PowerShell 是 PowerShell 模組。 如果您主要使用 Windows 系統,則 Azure PowerShell 自然適合。 命令遵循動詞-名詞命名配置,而資料以物件形式傳回。

選擇使用您所屬經驗的工具,縮短您的學習曲線。 利用 Microsoft 學習 ,在命令行上熟悉管理 Azure。

話說,開放只會提高你的能力。 條件適當時,請使用不同的工具。

重點︰

  • Azure 服務的功能同位不一定存在於 Azure CLI 與 Azure PowerShell 之間。

Azure CLI 與 Azure PowerShell:並存命令比較

登入、訂用帳戶和位置命令:

Command Azure CLI Azure PowerShell
使用網頁瀏覽器登入 az login Connect-AzAccount
取得可用的訂用帳戶 az account list Get-AzSubscription
設定訂用帳戶 az account set –-subscription SubscriptionId <> Set-AzContext -Subscription SubscriptionID <>
列出 Azure 位置 az account list-locations Get-AzLocation

尋找版本、取得說明和檢視命令說明:

Command Azure CLI Azure PowerShell
尋找版本 az --version Get-InstalledModule -Name Az
取得說明 az --help Get-Help
檢視命令說明 az vm --help Get-Help -Name New-AzVM

建立資源群組、VM 和 儲存體 帳戶:

Command Azure CLI Azure PowerShell
建立資源群組 az group create --name <ResourceGroupName> --location eastus New-AzResourceGroup -Name <ResourceGroupName> -Location eastus
建立 Azure 虛擬機器 az vm create --resource-group myResourceGroup --name myVM --image UbuntuLTS --admin-username azureuser --admin-password '<Password>' New-AzVM -ResourceGroupName <ResourceGroupName> -Name myVM -Image UbuntuLTS -Credential (Get-Credential)
建立 Azure 儲存體帳戶 az storage account create --name <儲存體 AccountName --resource-group <ResourceGroupName>> --location eastus --sku Standard_LRS --kind 儲存體 V2 New-Az 儲存體 Account -Name <儲存體 AccountName -ResourceGroupName ResourceGroupName <> -Location eastus -SkuName> Standard_LRS -Kind 儲存體 V2

管理 Azure 虛擬機器:

Command Azure CLI Azure PowerShell
列出 VM az vm list Get-AzVM
重新啟動 VM az vm restart --name myVM --resource-group <ResourceGroupName> Restart-AzVM -Name myVM -ResourceGroupName <ResourceGroupName>
停止 VM az vm stop --name myVM --resource-group <ResourceGroupName> Stop-AzVM -Name myVM -ResourceGroupName <ResourceGroupName>
停止和解除分配 VM az vm deallocate --name myVM --resource-group <ResourceGroupName> Stop-AzVM -Name myVM -ResourceGroupName <ResourceGroupName>
啟動 VM az vm start --name myVM --resource-group <ResourceGroupName> Start-AzVM -Name myVM -ResourceGroupName <ResourceGroupName>
刪除 VM az vm delete --name myVM --resource-group <ResourceGroupName> Remove-AzVM -Name myVM -ResourceGroupName <ResourceGroupName>

選取 [屬性] 和 [變更輸出格式]:

Command Azure CLI Azure PowerShell
顯示所有訂用帳戶資訊 az account list --all Get-AzSubscription |Select-Object -Property *
輸出為數據表 az account list -o table Get-AzSubscription |Format-Table
輸出為 JSON az account show \(部分機器翻譯\) Get-AzSubscription |ConvertTo-Json

重點︰

  • Azure CLI 預設為輸出 JSON 字串。 您可以在 Azure CLI 命令輸出格式中找到其他格式選項。
  • Azure PowerShell 預設為輸出物件。 若要深入瞭解 PowerShell 中的格式設定,請閱讀 使用格式命令變更輸出檢視

下一步

Azure CLI:

Azure PowerShell: