
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Azure VM Virtual Network and Subnet Name Using PowerShell
To retrieve the Azure VM virtual network and subnet name, we first need to retrieve the Azure
VM NIC information. To get the Azure VM NIC information, we need to use the Get-AzVM command, and then we can use the NetworkProfile property to retrieve the NIC name as shown below.
PS C:\> $vm = Get-AzVM -Name TestVM $vmnic = ($vm.NetworkProfile.NetworkInterfaces.id).Split('/')[-1]
Once we have the NIC name stored from the above command in the $vmnic variable, we can retrieve the NIC information using the Get-AzNetworkInterface command as shown below.
$vmnicinfo = Get-AzNetworkInterface -Name $vmnic
To get the Virtual Network name attached to the VM use the below operation on the command.
(($vmnicinfo.IpConfigurations.subnet.id).Split('/'))[-3]
To get the Subnet Attached to the VM use the below operation on the command
(($vmnicinfo.IpConfigurations.subnet.id).Split('/'))[-1]
The overall script will look like below
$vm = Get-AzVM -Name 'VMName' $vmnic = ($vm.NetworkProfile.NetworkInterfaces.id).Split('/')[-1] $vmnicinfo = Get-AzNetworkInterface -Name $vmnic Write-Output "Virtual Network: : $((($vmnicinfo.IpConfigurations.subnet.id).Split('/'))[-3])" Write-Output "Subnet: $((($vmnicinfo.IpConfigurations.subnet.id).Split('/'))[-1])"
Advertisements