- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the Azure VM virtual Network and the 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])"
- Related Articles
- How to get the Azure VM DNS name using PowerShell?
- How to get the Azure VM Size using Azure CLI in PowerShell?
- How to get the Azure VM username using Azure CLI in PowerShell?
- How to retrieve the Azure VM Subscription Name using PowerShell?
- How to retrieve the Azure VM NIC name using PowerShell?
- How to retrieve the Azure VM vNet name using PowerShell?
- How to get the Azure VM available sizes using PowerShell?
- How to get the installed Azure VM extension using PowerShell?
- How to retrieve the Azure VM network security group (NSG) using PowerShell?
- How to retrieve the Azure VM nic name using Azure CLI in PowerShell?
- How to get the available azure VM size using Azure CLI in PowerShell?
- How to get the location of the Azure VM using Azure CLI in PowerShell?
- How to get Azure VM activity logs using PowerShell?
- How to get the Azure VM disk encryption settings using PowerShell?
- How to get the Azure VM disk caching settings using PowerShell?

Advertisements