我使用以下代码通过 powershell 删除特定 VMSS 实例的自定义脚本扩展。但是,当使用 Get-AzVmssVM 检索实例视图时,我的代码失败,因为实例 ID 是字母数字而不是数字。您能否帮助我获取数字格式的实例 ID
# Input Parameters
$ResourceGroupName = "MyResourceGroup" # Replace with your resource group name
$VMScaleSetName = "MyVMSS" # Replace with your VMSS name
$ExtensionName = "CustomScriptExtension" # Replace with the name of the extension to remove
# Get all VMSS instances
$instances = Get-AzVmssVM -ResourceGroupName $ResourceGroupName -VMScaleSetName $VMScaleSetName
# Loop through each instance
foreach ($instance in $instances) {
$InstanceId = $instance.InstanceId # InstanceId is often alphanumeric
$InstanceName = $instance.Name
Write-Output "Processing Instance ID: $InstanceId, Instance Name: $InstanceName"
try {
# Get the instance view to check extensions
$instanceView = Get-AzVmssVM -ResourceGroupName $ResourceGroupName -VMScaleSetName $VMScaleSetName -InstanceId $InstanceId -InstanceView
$extensionExists = $instanceView.Extensions | Where-Object { $_.Name -eq $ExtensionName }
if ($extensionExists) {
Write-Output "Removing extension '$ExtensionName' from Instance ID: $InstanceId"
# Remove the extension
Remove-AzVmssVMExtension -ResourceGroupName $ResourceGroupName -VMScaleSetName $VMScaleSetName -InstanceId $InstanceId -Name $ExtensionName -Force
Write-Output "Extension '$ExtensionName' removed successfully from Instance ID: $InstanceId"
} else {
Write-Output "Extension '$ExtensionName' not found on Instance ID: $InstanceId"
}
} catch {
Write-Output "Error processing Instance ID: $InstanceId. Details: $_"
}
Write-Output "--------------------------------------------"
}
我想使用实例 Id 获取实例视图,但 Get-AzVmssVM 期望数值,但我得到字母数字实例 Id
您尝试删除扩展的命令 Remove-AzVmssVMExtension 无效。正确的 cmdlet 是 Remove-AzVmssExtension。您可以检查
MS Doc
:Remove-AzVmssExtension
在运行脚本之前,我在 VMSS 中有一个
CustomScriptExtension
。
这里是更新后的 PowerShell 脚本,用于删除
CustomScriptExtension
中的 VMSS
。
$RGName = "venkat-rg"
$vmssName= "venkatvmss"
$vmssExtensionName= "CustomScriptExtension"
# Get all VMSS instances
$instances = Get-AzVmssVM -ResourceGroupName $ResourceGroupName -VMScaleSetName $VMScaleSetName
# Loop through each instance
foreach ($instance in $instances) {
$InstanceId = $instance.InstanceId # InstanceId is often alphanumeric
$InstanceName = $instance.Name
Write-Output "Processing Instance ID: $InstanceId, Instance Name: $InstanceName"
try {
# Get the instance view to check extensions
$instanceView = Get-AzVmssVM -ResourceGroupName $ResourceGroupName -VMScaleSetName $VMScaleSetName -InstanceId $InstanceId -InstanceView
$extensionExists = $instanceView.Extensions | Where-Object { $_.Name -eq $ExtensionName }
if ($extensionExists) {
Write-Output "Removing extension '$ExtensionName' from Instance ID: $InstanceId"
# Remove the extension
Remove-AzVmssExtension -VirtualMachineScaleSet $vmss -Name $vmssExtensionName
Write-Output "Extension '$ExtensionName' removed successfully from Instance ID: $InstanceId"
Write-Host "Updating the VMSS to reflect the removal of the extension..."
Update-AzVmss -ResourceGroupName $RGName -Name $vmssName -VirtualMachineScaleSet $vmss
} else {
Write-Output "Extension '$ExtensionName' not found on Instance ID: $InstanceId"
}
} catch {
Write-Output "Error processing Instance ID: $InstanceId. Details: $_"
}
Write-Output "--------------------------------------------"
}
输出
运行脚本后,VMSS 中的扩展已被卸载。
参考: 从 VMSS 中删除实例