Today we are going to see how to fetch the IPv4 Address details of a VM from a HyperVisor console. We will be using Powershell to achieve the same with a One-Liner code.
You can modify the Command as per your need. Our requirement is to check Only the Running VM's and get the VM Name and its corresponding IPv4 Address. So lets get started.
CODE (Execute in HyperV ):
Get-VM | ? State -eq "Running" | select Name, @{l="IPv4Address";e={($_.NetworkAdapters.IPAddresses -match "(?<Address >((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))")} }
CODE DETAILS :
1. Get-VM : Fetches the VM List present on the HyperVisor
2. ? State -EQ "Running" : It is similar to Where-Object {$_.State -eq "Running"} , here we are using where-object to fetch only the Running VM list from the Pipeline.
3. NetworkAdapters properties of the VM Contains the IPAddresses information, we are using IPv4 address Regex to get the valid IPv4 address and ignore IPv6 address ( you can modify this as per your need)
Hope you all liked this post. Do share and provide your comments below in case of any query.
Comments
Post a Comment