skip to Main Content

I’m trying to gather information about VMs through the CLI in order to get those faster than through the Azure-Portal.
I’m trying to extract the vnet, the subnet and the routing table for the given VM.
But this is my first time using the CLI and my code is not working. I have disabled the login/logout for now and the input of the VM name.

The error occurs at the line $vnetId = (Get-AzNetworkInterface -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName).IpConfigurations[0].Subnet.Id.Split("/")[-3]

I’m getting the following error message: "

Get-AzNetworkInterface : The Resource ‘Microsoft.Network/networkInterfaces/vmva2015’ under resource group ‘RG-REDISPATCHPROD-PROD-001’ was not found. For more details please go to
https://aka.ms/ARMResourceNotFoundFix
StatusCode: 404
ReasonPhrase: Not Found
ErrorCode: ResourceNotFound
"

I’m fairly out of my depth and looking for help.

# Importieren der benötigten Az-Module
Import-Module Az.Accounts
Import-Module Az.Network
Import-Module Az.Compute
Import-Module Az.Resources
Import-Module Az.DesktopVirtualization

# Anmelden bei Azure
    #Connect-AzAccount

# Eingabeaufforderung für den VM-Namen anzeigen
    #$vmName = Read-Host "Geben Sie den Namen der VM ein: "
$vmName = "vmva2015" # Der Name der VM ist hier fest kodiert, für Testzwecke

# Durchsuchen aller Abonnements nach der VM
$subscriptions = Get-AzSubscription
foreach ($subscription in $subscriptions) {
    Set-AzContext $subscription

    # Suchen der VM mit dem angegebenen Namen
    $vm = Get-AzVM -Name $vmName -ErrorAction SilentlyContinue
    if ($vm) {
        $resourceGroupName = $vm.ResourceGroupName
        # Die VM wurde gefunden, die benötigten Parameter abrufen
        $vnetId = $vm.NetworkProfile.NetworkInterfaces[0].Id.Split("/")[8]
        $vnet = Get-AzVirtualNetwork -ResourceGroupName $resourceGroupName -Name $vnetId
        $subnetId = $vm.NetworkProfile.NetworkInterfaces[0].Id.Split("/")[10]
        $subnet = Get-AzVirtualNetworkSubnet -Name $subnetId -VirtualNetwork $vnet
        $rtName = $subnet.RouteTable.Id.Split("/")[-1]
        $rt = Get-AzRouteTable -Name $rtName -ResourceGroupName $resourceGroupName

        # Ergebnisse ausgeben
        Write-Host "Name der VM: $($vm.Name)"
        Write-Host "Name des VNet: $($vnet.Name)"
        Write-Host "Name des Subnet: $($subnet.Name)"
        Write-Host "Name der Routing-Tabelle: $($rt.Name)"

        # Das Skript endet, sobald eine VM gefunden wurde
        break
    }
}

Best,
Edward

I have tried several internet searches which lead me to different site, but my knowledge is too limited to find an answer (and I have tried for roughly 6–7 hours by now).

2

Answers


  1. Extracting several parameters from a VM from Azure through CLI (Code not working)

    I have used below PowerShell code to retrieve the VM properties (VM Name, VNet, Subnet, NIC & Route Table)

    VM Property details in portal

    enter image description here

    I have followed this MS Document1 & MS Document2 & MS Document3 as a reference to extracting several parameters from a VM.

    PowerShell Code

        $vmName = "VM8"
        $subscriptions = Get-AzSubscription
        foreach ($subscription in $subscriptions) {
            Set-AzContext $subscription
            $vm = Get-AzVM -Name $vmName -ErrorAction SilentlyContinue
            $resourceGroupName = $vm.ResourceGroupName
            if ($vm) {
                $networkInterface = Get-AzNetworkInterface -ResourceId $vm.NetworkProfile.NetworkInterfaces[0].Id
                $ipConfig = $networkInterface.IpConfigurations[0]
                $subnet= (Get-AzNetworkInterface -Name $ipConfig.Name -ResourceGroupName $resourceGroupName).IpConfigurations.Subnet.Id.Split("/")[-1]
                $virtualNetwork= (Get-AzNetworkInterface -Name $ipConfig.Name -ResourceGroupName
    $resourceGroupName).IpConfigurations.Subnet.Id.Split("/")[-3]
                $routeTable= (Get-AzVirtualNetwork -ResourceGroupName $resourceGroupName -Name $virtualNetwork).Subnets
                $routeTableId = $routeTable.RouteTable.Id
                $routeTableName = $routeTableId.Split("/")[-1]
                Write-Output "Virtual Machine Name: $($vm.Name)"
                Write-Output "Virtual Network Name: $virtualNetwork"
                Write-Output "Network Interface Name: $($networkInterface.Name)"
                Write-Output "Subnet Name: $subnet"
                Write-Output "Route Table Name: $routeTableName"
            }
        }
    

    Output

    enter image description here

    Login or Signup to reply.
  2. Have you tried this command line?

    az network vnet show -g MyResourceGroup -n MyVNet && az network route-table show -g MyResourceGroup -n MyRouteTable
    

    It will show you all the details you need.

    If you need more information, just follow this link:
    https://learn.microsoft.com/en-us/cli/azure/network/route-table?view=azure-cli-latest#az-network-route-table-show

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search