Select Page

Get NIC Details of VM’s in vCenter Using VMware PowerCLI

You will need to initially download and install Vmware PowerCLI via the VMware website, It can be downloaded here – https://my.vmware.com/group/vmware/get-download?downloadGroup=PCLI600R1 (You may need to login)

Once installed you will need to open the software, you will be presented with the following command if you have not previously changed your Powershell execution policy.

Powercli Execution Policy Error - Get NIC Details of VM's in vCenter Using VMware PowerCLI

To enable PowerCLI to work, I have used the below command to change my execution policy to unrestricted. This is not recommended in production environments.

Set ExecutionPolicy Unrestricted - Get NIC Details of VM's in vCenter Using VMware PowerCLI

You will now need to connect to your vCenter via PowerCLI. This can be completed by running the below command –

Connect-VIServer -Server HOSTNAME

This will use the credentials you are currently logged in so make sure you are logged in as a user with Administrator privileges to vCenter.

You will need to create a VM.txt file and change the $infile location to where you saved it. You will need to populate this list with a list of host names that you want to retrieve the NIC Details from vCenter.

Please note – You will need VMware Tools installed on the machine for this to obtain the information.

You can obtain a list of VM’s by using this command

Get-VM

You can then populate the VM.txt file using the results.

cls

$infile = ‘C:\vm.txt’

#The infile should be labled InputText,txt and contain the list of IPs

$IPs = get-content -path $infile

foreach ($vm in $IPs)

{

$vm = Get-VM -Name $vm

$subnetmask = @()

$row = “” | Select Name,Host,OS,NicType,VLAN,IP,Gateway,Subnetmask,DNS

$row.Name = $vm.Name

$row.Host = $vm.VMHost.Name

$row.OS = $vm.Guest.OSFullName

$row.NicType = [string]::Join(‘,’,(Get-NetworkAdapter -Vm $vm | Select -ExpandProperty Type))

$row.IP = [string]::Join(‘,’,$vm.Guest.IPAddress)

$row.Gateway = $vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute.Gateway.IpAddress | where {$_ -ne $null}

foreach ($iproute in $vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute) {

if (($vm.Guest.IPAddress -replace “[0-9]$|[1-9][0-9]$|1[0-9][0-9]$|2[0-4][0-9]$|25[0-5]$”, “0” | select -uniq) -contains $iproute.Network) {

$subnetmask += $iproute.Network + “/” + $iproute.PrefixLength

}

}

$row.Subnetmask = [string]::Join(‘,’,($subnetmask))

$row.DNS = [string]::Join(‘,’,($vm.ExtensionData.Guest.IpStack.DnsConfig.IpAddress))

$row

}

Save this in a location that you can run the script from, in my example I have used C:\GetNICDetails.ps1

GetNICDetails - Get NIC Details of VM's in vCenter Using VMware PowerCLI

Here is an example of how the data will be presented.

GetNICDetails Results - Get NIC Details of VM's in vCenter Using VMware PowerCLI

Pin It on Pinterest

Share This