Automation

 View Only
Expand all | Collapse all

Getting virtual machine network informations by using Get-View

  • 1.  Getting virtual machine network informations by using Get-View

    Posted Jun 04, 2014 02:36 PM

    Hi,

    I have made simple script that is "dumping" virtual machines network information as below.

    "

    VM Name

    1;Network Adapter 1;Network name;MAC address;IP address.

    2;Network Adapter 2;Network name;MAC address;IP address.

    "

    I have used below variables.

         $_.Config.Hardware.Device (Network Adapter 1),

         $_.Guest.Net.Network (Network name),

         $_.Guest.Net.MacAddress (MAC address),

         $_.Guest.Net.IpAddress (IP address).

    Everything works fine but not for VMs that are using distributed vSwitch. For such VMs, Network name ($_.Guest.Net.Network) is empty. I'm doing something wrong or I should get network name from other var?



  • 2.  RE: Getting virtual machine network informations by using Get-View

    Posted Jun 04, 2014 03:41 PM

    I'd try a different variable.  Since you already get the Get-View listing from the virtual machine, dvSwitch information can at least be started from $_.Network.  Odds are, you'll get a managed object reference of a dvPortgroup and from there, what I've done in the past is do something similar to the following:

    $networkName = (Get-View -Id $_.Network).Name

    Do a quick Get-View command on the MoRef of the dvPortgroup and then pull it's Name property from it and store it in $networkName.



  • 3.  RE: Getting virtual machine network informations by using Get-View

    Posted Jun 04, 2014 05:23 PM

    Well, that would work perfectly in case where we would have only one network. In this case I have many different virtual machines, with many network adapters. But maybe I am wrong. I finally tried something like this.

    foreach($vm in Get-View -ViewType VirtualMachine){

        if($vm.Network.Type -eq "Network"){

              if($_.Network){$tmpNetwork = $_.Network}else{$tmpNetwork = "!NN!"};

         }elseif($vm.Network.Type -eq "DistributedVirtualPortgroup"){

              $tmpDeviceConfigId = $_.DeviceConfigId;

              $tmpdvSpgKey = ($vm.Config.Hardware.Device | ?{$_.Key -eq $tmpDeviceConfigId}).Backing.Port.PortgroupKey;

              $tmpNetwork = (Get-View -Id ($vm.Network | ?{$_.Value -eq $tmpdvSpgKey})).Name;

         }

    }

    When VM uses standard vSwitch it has $_.Guest.Net.Network filled but it has empty Backing.Port.PortgroupKey;

    When VM uses distributed vSwitch it has $_.Guest.net.Network empty, but it has Backing.Port.PortgroupKey and I am able to use it with Get-View -Id $vm.Network;

    Any ideas if I can make it another way?



  • 4.  RE: Getting virtual machine network informations by using Get-View

    Posted Jun 04, 2014 05:39 PM

    I know I struggled with something similar when I had a mix of VSS and VDS devices in the environment, even if I only had one NIC per VM.  I vaguely remember using something similar to the Backing.Port.PortgroupKey for easy determination of VSS or VDS and had my if..else statements setup like what you had.  It was a while ago, but even then I had to use an object with assumptions attached to it (that Backing.Port.PortgroupKey != $null means VDS, if = $null, it's VSS).

    --j



  • 5.  RE: Getting virtual machine network informations by using Get-View

    Posted Jun 09, 2014 02:02 PM

    Another problem...

    I'am able to get netmask from $_.Guest.Net.IpConfig.IpAddress.PrefixLength but this works only for virtual machines placed on vSwitch. I have no idea how to get netmask for virtual machines placed on VDS.



  • 6.  RE: Getting virtual machine network informations by using Get-View

    Posted Jun 09, 2014 02:30 PM

    I would think at that point, maybe it's time to start asking the guest OS a few questions through some of the guest cmdlets.

    Although, strange, I was able to get the PrefixLength on my devices in a VDS.



  • 7.  RE: Getting virtual machine network informations by using Get-View

    Posted Jun 10, 2014 06:25 AM

    Jon, are you able to check if you can still get PrefixLength in your env? Regarding "start asking guest OS" you mean to use for example invoke-VMscript?



  • 8.  RE: Getting virtual machine network informations by using Get-View

    Posted Jun 10, 2014 08:16 PM

    Yeah, might need to use Invoke-VMScript for some of those specifics.

    I'll double check the PrefixLength value in my environment tomorrow (at an all day event today and unable to get into the environment right now).

    --j



  • 9.  RE: Getting virtual machine network informations by using Get-View
    Best Answer

    Posted Jun 11, 2014 02:33 PM

    I just double checked a test machine in my lab.

    There's two different DVS portgroup connections, here's the output from the one that's on the network called "VM Network" which is our "prod" network in the lab:

    PowerCLI C:\> $vm.Guest.Net[1].IpConfig.IpAddress

    IpAddress       : 2607:f190:1:0:449c:d037:99cf:e180

    PrefixLength    : 64

    Origin          : linklayer

    State           : unknown

    Lifetime        :

    DynamicType     :

    DynamicProperty :

    IpAddress       : fe80::449c:d037:99cf:e180

    PrefixLength    : 64

    Origin          : linklayer

    State           : unknown

    Lifetime        :

    DynamicType     :

    DynamicProperty :

    IpAddress       : 10.10.100.16    <main IPv4 address>

    PrefixLength    : 24   <PrefixLength is populated>

    Origin          : manual

    State           : preferred

    Lifetime        :

    DynamicType     :

    DynamicProperty :



  • 10.  RE: Getting virtual machine network informations by using Get-View

    Posted Jun 13, 2014 01:04 PM

    Many thanks. It seems there is a problem with those vars on vCenter 4.0 (build 385281).



  • 11.  RE: Getting virtual machine network informations by using Get-View

    Posted Jun 13, 2014 02:01 PM

    Ah.  I never did ask what version you were testing against.  I have the latest 5.5 appliance that I'm testing against for vCenter, although the ESX host in question was 5.1.  Good to know.

    --j