I am using the below code to remove a custom script extension using powershell for a particular VMSS instance. However, my code fails when retrieving the instance view with Get-AzVmssVM, as the instance ID is alphanumeric rather than numeric.Could you please assist me in obtaining the instance ID in numeric format
# 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 "--------------------------------------------"
}
I would like to get the instance view using instance Id but Get-AzVmssVM expecting numeric value but I am getting alphanumeric instance Id
Error Msg:Instance view with instance Id
The command Remove-AzVmssVMExtension you are trying for removing the extension is invalid. The correct cmdlet is Remove-AzVmssExtension.You can check MS Doc
: Remove-AzVmssExtension
I have a CustomScriptExtension
in VMSS before running the script.
Here is the updated PowerShell script to remove the CustomScriptExtension
in VMSS
.
Note: In flexible mode, each instance has a unique string identifier(venkat-vmss_3d929c9a). This is different from the uniform orchestration mode, where the InstanceId is an integer (starting from 0 and increasing for each instance)
$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 "--------------------------------------------"
}
Output
After running the script, the extension has been uninstalled in VMSS.
Reference: Remove an instance from within a VMSS