Showing posts with label VirtualMachineScaleSet. Show all posts
Showing posts with label VirtualMachineScaleSet. Show all posts

Saturday, February 12, 2022

Azure VMSS VM Instance Status using powershell - Get-AzureRmVmssVM

 The below powershell script helps you to check the status of VM Instances inside a Virtual Machine Scale Sets. This will check the Status whether the Instance is Running or Deallocated/Stopped as some actions will not be allowed on a deallocated instance while performing on VMSS.


For Instance, I'm checking if the status is Running and then do any operation on the instance.

Get-AzureRmVmssVM -ResourceGroupName $nameprefix  -VMScaleSetName $key | foreach { $Id = $_.InstanceId; $vmssvm = Get-AzureRmVmssVM -ResourceGroupName $nameprefix  -VMScaleSetName $key -InstanceView -InstanceId $_.InstanceId; if($vmssvm.Statuses[$vmssvm.Statuses.Count-1].Code.Contains("running")){ Write-Host "vmid: " $vmssvm.Statuses[$vmssvm.Statuses.Count-1].Code}}






Wednesday, November 10, 2021

Add VMSS extensions with settings and protected settings- Powershell

 The below script guides you to add/remove a vmss extension in Azure. The example is for adding Microsoft Monitoring Agent to Azure vmss which will have workspace Id to collect custom logs from VM's


Get VMSS to a variable

$vmssname= Get-AzVmss -ResourceGroupName npqatx -VMScaleSetName <vmssname>

$workspaceId="GUID of log analytics workspace"

 $workspaceKey="workspace key"

$settings=@{"workspaceId" = $workspaceId}

$protSettings=@{"workspaceKey" = $workspaceKey}

#Adding new extension

Add-AzVmssExtension -VirtualMachineScaleSet $vmssname -Name "MMAExtension" -Publisher "Microsoft.EnterpriseCloud.Monitoring" -Type "MicrosoftMonitoringAgent" -TypeHandlerVersion "1.0" -AutoUpgradeMinorVersion $true -Setting $settings -ProtectedSetting $protSettings

Update-AzVmss -ResourceGroupName "RGName" -VMScaleSetName $vmssname.Name -VirtualMachineScaleSet $vmssname


#Remove existing vmss extension

Remove-AzVmssExtension -VirtualMachineScaleSet $webvmss -Name "MMAExtension"

Update-AzVmss -ResourceGroupName "RGName" -VMScaleSetName $vmssname.Name -VirtualMachineScaleSet $vmssname


#make sure to upgrade instances in vmss to the latest model after every change to vmss.