将Azure Powershell命令转换为Azure CLI

问题描述

我有一个Powershell脚本,该脚本使用Azure Powershell更新虚拟机规模集(在Azure Service Fabric下)以添加/删除关联的Service Fabric虚拟机使用的证书。该脚本按预期工作,并且我具有以下命令(我已删除了其他一些逻辑以关注此问题):

# This gets the Virtual Machine Scale Set object
$virtualMachinescaleSet = Get-Azvmss -ResourceGroupName $myResourceGroupName -VMScaleSetName $myVMScaleSetName

# Example of removing items from certificate items from the VMSS object.
$virtualMachinescaleSet.VirtualMachineProfile.osProfile.Secrets[$mySecretIndex].VaultCertificates.RemoveAt($myCertificateIndexThatIWantToRemove)

# Example of creating new certificate config
$newCertificateUrl = (Get-AzkeyvaultCertificate -VaultName $mykeyvaultName -Name $myCertificateName).SecretId
$newCertificateConfig = New-AzvmssVaultCertificateConfig -CertificateUrl $newCertificateUrl -CertificateStore "My"

# Example of adding new certificate to the VMSS object
$virtualMachinescaleSet.VirtualMachineProfile.OsProfile.Secrets[$mySecretIndex].VaultCertificates.Add($newCertificateConfig)

# Committing the update to VMSS
Update-Azvmss -ResourceGroupName $myResourceGroupName -VirtualMachinescaleSet $virtualMachinescaleSet -VMScaleSetName $myVMScaleSetName

上面的脚本工作正常。但是,我现在尝试将上述每个命令转换为Azure CLI。脚本调用的方式意味着我无法在同一脚本中混合和匹配Azure Powershell和Azure CLI命令。到目前为止,我拥有的命令会引起问题:

# This gets me the Virtual Machine Scale Set object
$virtualMachinescaleSet = az vmss show --name $myVMScaleSetName --resource-group $myResourceGroupName | ConvertFrom-Json

# Trying to RemoveAt gives the error: MethodInvocationException: Exception calling "RemoveAt" with "1" argument(s): "Collection was of a fixed size."
$virtualMachinescaleSet.VirtualMachineProfile.osProfile.Secrets[$mySecretIndex].VaultCertificates.RemoveAt($myCertificateIndexThatIWantToRemove)

# Not sure the CLI equivalent commands of this
$newCertificateUrl = (Get-AzkeyvaultCertificate -VaultName $mykeyvaultName -Name $myCertificateName).SecretId
$newCertificateConfig = New-AzvmssVaultCertificateConfig -CertificateUrl $newCertificateUrl -CertificateStore "My"

# Trying to Add gives the error: MethodInvocationException: Exception calling "RemoveAt" with "1" argument(s): "Collection was of a fixed size."
$virtualMachinescaleSet.VirtualMachineProfile.OsProfile.Secrets[$mySecretIndex].VaultCertificates.Add($newCertificateConfig)

所以我的问题是

  1. Azure Powershell脚本的CLI等效命令是什么?
  2. 为什么Azure CLI脚本中的VMSS对象似乎不一样? (至少因为我无法更改VaultCertificates数组)

提前谢谢

解决方法

您使用的所有PowerShell都可以更改为两个等效的CLI命令。

一个要删除的

az vmss update --resource-group $myResourceGroupName --name $myVMScaleSetName --remove virtualMachineProfile.osProfile.secrets index

添加一个:

az vmss update --resource-group $myResourceGroupName --name $myVMScaleSetName --add virtualMachineProfile.osProfile.secrets '{"sourceVault": {"id": "resourceId"},"vaultCertificates": [{"certificateStore": null,"certificateUrl": "certificateUrl"}]}'

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...