任務是修改 Azure 網路安全組規則。
我在那里一切正常(使用 az network nsg rule update ),提供 NSG 的名稱和實際規則。
但是我想要做的是使用 VM 名稱(訂閱 ID、RG 名稱和規則名稱已知并且將是明確的)作為輸入引數來編輯該規則。
我正在使用 az(不是 Az)命令。
我看到的是 az vm show,但它沒有顯示 VM 連接到哪個 NSG。
我想問一下如何使用 CLI 獲取與 VM 關聯的 NSG 名稱。直接的答案、指南或資源鏈接都將受到贊賞。
謝謝你,提前謝謝!
uj5u.com熱心網友回復:
按照下面的 Microsoft 檔案進行操作
https://docs.microsoft.com/en-us/powershell/module/az.network/get-aznetworksecuritygroup?view=azps-6.5.0
Get-AzNetworkSecurityGroup [-Name <String>][-ResourceGroupName<String> [-DefaultProfile <IAzureContextContainer>][<CommonParameters>]
uj5u.com熱心網友回復:
您還需要提供 VM 所在的資源組 - 這就是它能夠區分不同資源組中具有相同名稱的兩個 VM 的方式。在這里,{id:id}創建一個可以與后續命令一起使用的映射。還有其他方法可以做到這一點,但我更喜歡這種方法,因為我更熟悉這種格式。
你可以這樣做:
- 使用 VM 名稱和 RG,獲取 VM 的 NIC ID 并將其保存到
nic_id:
vm_name="myvm"
vm_rg="myrg"
nic_id=`az vm nic list -g $vm_rg --vm-name $vm_name --query "[].{id:id}"`
# echo to make sure we're getting the right value.
echo $nic_id
- 使用 NIC ID,獲取子網 ID
snet_id=`az network nic show --ids $nic_id --query "ipConfigurations[0].subnet.{id:id}"`
# echo to make sure we're getting the right value.
echo $snet_id
- 最后,使用子網 ID,獲取 NSG 的名稱
nsg_name=`az network vnet subnet show --ids $snet_id --query networkSecurityGroup.{id:id} -o tsv | sed 's/^.*networkSecurityGroups\///'`
# echo to make sure we're getting the right value.
echo $nsg_name
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/329472.html
上一篇:AzureADB2C性能
