Answered by:
How to get Virtual Network name for Virtual Machine in Azure PowerShell?

Question
-
I want to know what Virtual Network name is for specific Virtual Machine.
How can I get this through PowerShell in Azure?
Wednesday, January 22, 2014 6:54 AM
Answers
-
Hi,
Based on my research, you can view the Virtual Network name in the CONFIGURE page in the VM.
In addition, sorry to say that I haven’t found any Cmdlets in powershell to obtain the virtual network name of a special VM directly. You can use Get-AzureVNetConfig to retrieve the virtual network configuration and you can use Get-AzureVM on Windows Azure PowerShell console to obtain the VM’s IP address if you don’t know the IP address of the VM. You can compare the VM’s IP address with the Virtual networks’ range, then you will know which virtual network the VM belongs.
Best regards,
Susie
- Proposed as answer by Susie Long Monday, January 27, 2014 2:44 AM
- Marked as answer by Paweł Smejda Tuesday, January 28, 2014 2:21 PM
Thursday, January 23, 2014 7:00 AM
All replies
-
Hi,
Based on my research, you can view the Virtual Network name in the CONFIGURE page in the VM.
In addition, sorry to say that I haven’t found any Cmdlets in powershell to obtain the virtual network name of a special VM directly. You can use Get-AzureVNetConfig to retrieve the virtual network configuration and you can use Get-AzureVM on Windows Azure PowerShell console to obtain the VM’s IP address if you don’t know the IP address of the VM. You can compare the VM’s IP address with the Virtual networks’ range, then you will know which virtual network the VM belongs.
Best regards,
Susie
- Proposed as answer by Susie Long Monday, January 27, 2014 2:44 AM
- Marked as answer by Paweł Smejda Tuesday, January 28, 2014 2:21 PM
Thursday, January 23, 2014 7:00 AM -
This was four years ago so may be you have resolved the issue or the issue has gone. The way I could do this (in Azure Resource Manager but probably a parallel in classic Azure) from just the VM name and Resource Group name (within a subscription, natch) was something like the following (and I would be delighted with something tidier).
- you may have to look if a VM can have more than one network interface ... not sure this is possible but I've never seen it ... the code just works with the first network interface index [0] for a VM ... a foreach on network interfaces may generalize this
- Not sure but I am guessing if the vnet(s) are bound to another vm's network interface then the remove virtual network fails; haven't tested this but try / catch may be helpful
$vm = Get-AzureRmVM -Name $vmName -ResourceGroupName $RSGName;
$NetInt = $vm.NetworkProfile.NetworkInterfaces[0];
$NetIntName = $NetInt.ID.Substring($NetInt.Id.LastIndexOf("/")+1);
$VNets = Get-AzureRmVirtualNetwork | Where-Object {$_.Subnets.IpConfigurations.id.contains($NetIntName)};
foreach ($VNet in $VNets) {
$VNet | Remove-AzureRmVirtualNetwork -Force;
}
- Edited by T.A.Schultz Wednesday, February 7, 2018 11:28 PM
- Proposed as answer by Nirushi J Sunday, February 11, 2018 10:46 AM
Wednesday, February 7, 2018 11:24 PM