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
I have used below PowerShell code to retrieve the VM properties (VM Name, VNet, Subnet, NIC & Route Table)
VM Property details in portal
I have followed this MS Document1 & MS Document2 & MS Document3 as a reference to extracting several parameters from a VM.
PowerShell Code
Output
Have you tried this command line?
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