List all ARM VMs in azure

Below script will list all ARM VMs in Azure

 

Add-AzureAccount

Class VirtualMachineInfo
{
    [string]$ResourceType
    [string]$Subscription
    [string]$ResourceId
    [string]$ResourceGroup
    [string]$Location
    [string]$Name
    [string]$ServiceName
    [string]$VNet
    [string]$Subnet
    [string]$Size
    [string]$Status
    [string]$VMtype
    [string]$GuestName
    [string]$PrivateIP
    [string]$publicIP
    [string]$diskname
    [string]$fqdn 
    [string]$restype
    [string]$ostype
    [string]$monitoringagentextension
}

$allResources = @()
$filename = "c:\temp\azurereports\" + [string](get-azureaccount).ID.Split("@")[1] + "-CLASSIC.csv" 

$vms =@()
$subscriptions = Get-AzureSubscription
foreach($subscription in $subscriptions){
$subscription

   Select-AzureSubscription   -Subscriptionid $subscription.SubscriptionId  
   Write-Output "Processing data for subscription $Subscription.subscriptionname"
   $vms = Get-AzureVM 

foreach ($vm in $vms){
         $VirtualMachineInfo = New-Object VirtualMachineInfo
         $VirtualMachineInfo.Subscription = $subscription.SubscriptionName
         $VirtualMachineInfo.Name = $vm.Deploymentname
    
         $VirtualMachineInfo.Status = $vm.InstanceStatus
         $VirtualMachineInfo.restype = [string] "Classic VM"
       
         #$VirtualMachineInfo.PrivateIP = $vm.IpAddress
         #$VirtualMachineInfo.publicip = $vm.PublicIPAddress
         $VirtualMachineInfo.ostype= $vm.vm.OSVirtualHardDisk.OS

         $VirtualMachineInfo.ServiceName = $vm.servicename 

$uri=$vm.VM.OSVirtualHardDisk.MediaLink.AbsoluteUri
$VirtualMachineInfo.Location = Get-AzureDisk | Where-Object {$_.MediaLink -eq $uri}| Select-Object Location

$allResources += $VirtualMachineInfo | Select-Object `
            Subscription, `
            Name,   `
            Status, `
            Location , `
            Ostype , `
            Servicename
}

}
$allResources = $allResources | Sort-Object Subscription, ResourceType
$allResources | Export-Csv -Path $filename -Force

 

How to remove @ from array when working with JSON on Azure

When you work with JSON on powershell to chnage/replace arrays/list . you will see Convertto-Json will change your Azure compliant ARM template and put @ in front of arrays. This may cause issues in deployment.

Filter : {@{resource=; resourcegroup=}}

$TargetFile | ConvertTo-Json -Depth 50 | % { [System.Text.RegularExpressions.Regex]::Unescape($_) }

Output : 

“resource”: {
“property”: “name”,
“condition”: “like”,
“resourcetype”: “*”
},
“resourcegroup”: {
“property”: “resourcegroupname”,
“condition”: “like”,
“subscription”: “sub-xyz”
}

Enable Accelerated Networking

When Azure planned reboots occurred we have seen performance degradation on several VMs . Here is the quick fix which doesn’t cost anything extra.

We need to enable Accelerated networking to overcome this problem . process is pretty simple

Note : This solution only works on VM that is not a member of availability set.

Steps to enable accelerated networking

  1. Deallocate VM
  2. Note NIC Name and resource group name where nic reside
  3. Assign new NIC to VM
  4. Deallocated old nic .
  5. Run below on cloudshell
az network nic update --name NICNAME --accelerated-networking true --resource-group RESOURCEGROUP

 

  1. Assign old nic back to the VM
  2. Deallocated new NIC and start VM

How to use Classic Azure Express Route Powershell

get-module

####### #Import the required Azure Powershell program modules 
Import-Module 'C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Azure.psd1' 
Import-Module 'C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\ExpressRoute\ExpressRoute.psd1'  

#Get the publish settings file for the master Express Route Subscription 
login-azurermaccount

Import-AzurePublishSettingsFile $PublishSettings 
#Change to the Master Subscription 

$Subscription = "subscfriptionname" 
Select-AzureSubscription -SubscriptionName $Subscription 
#Get the Service Key 
Get-AzureDedicatedCircuitlink -ServiceKey   #***  # This should show all the existing links provisioned on the ExpressRoute Circuit 


# Use the following command to list all the Authorisations currently associated with the ExpressRoute circuit 
# If required use the Remove Authorization commend to tidy up any authorisations with unused links 

Get-AzureDedicatedCircuitLinkAuthorization -ServiceKey *** # This should show all the existing authorisations