VMware Cloud Community
Eparchment
Contributor
Contributor
Jump to solution

Simply trying to add VLANIDs to a column

Hi folks! I used code found here on another thread and wanted to somewhat alter it to display more information I'm looking for. I'm not familiar with Powershell but plan on learning more. Though I feel the study of powershell is going to take a long time just to figure this simple list of data.

Get-DataCenter testdata | Get-Cluster test |

Get-VM |

ForEach-Object {

  $ErrorActionPreference= 'silentlycontinue'

  $VM = $_

 

  $VM | Get-VMGuest | Select-Object -ExpandProperty Nics |

  &ForEach-Object {

    $Nic = $_

    foreach ($IP in $Nic.IPAddress)

   

    {

      if ($IP.Contains('.'))

      {     

        "" | Select-Object -Property @{Name='VM';Expression={$VM.Name}},

          @{Name='IPAddress';Expression={$IP}},

          @{Name='NetworkName';Expression={$Nic.NetworkName}},

          @{Name='VLANID';Expression={$vlanid.VLanId}}

     }

    }

  }

}

You can see what I have the @{Name VLANID where I'd like to print VLANIDs. I cannot figure out how to not print every VLANID horizontally instead of vertically assigned to each IP address in the list.

Thanks for any assistance!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The VLANid is not available in that object, you will have to fetch the VirtualPortgroup.

Try like this

Get-DataCenter testdata | Get-Cluster test |

Get-VM |

ForEach-Object {

  $ErrorActionPreference= 'silentlycontinue'

  $VM = $_

  $VM | Get-VMGuest | Select-Object -ExpandProperty Nics |

  ForEach-Object {

    $Nic = $_

    $vlan = Get-VirtualPortgroup -Name $nic.NetworkName -VM $VM

    foreach ($IP in $Nic.IPAddress)

    {

      if ($IP.Contains('.'))

      {    

        "" | Select-Object -Property @{Name='VM';Expression={$VM.Name}},

          @{Name='IPAddress';Expression={$IP}},

          @{Name='NetworkName';Expression={$Nic.NetworkName}},

          @{Name='VLANID';Expression={$vlan.VLanId}}

     }

    }

  }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

The VLANid is not available in that object, you will have to fetch the VirtualPortgroup.

Try like this

Get-DataCenter testdata | Get-Cluster test |

Get-VM |

ForEach-Object {

  $ErrorActionPreference= 'silentlycontinue'

  $VM = $_

  $VM | Get-VMGuest | Select-Object -ExpandProperty Nics |

  ForEach-Object {

    $Nic = $_

    $vlan = Get-VirtualPortgroup -Name $nic.NetworkName -VM $VM

    foreach ($IP in $Nic.IPAddress)

    {

      if ($IP.Contains('.'))

      {    

        "" | Select-Object -Property @{Name='VM';Expression={$VM.Name}},

          @{Name='IPAddress';Expression={$IP}},

          @{Name='NetworkName';Expression={$Nic.NetworkName}},

          @{Name='VLANID';Expression={$vlan.VLanId}}

     }

    }

  }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Eparchment
Contributor
Contributor
Jump to solution

Thanks LucD! I had references to the VLADid before posting it, but couldn't figure out what to add after the Get. I have to read up about why "-Name $nic.NetworkName -VM $VM" is significant. Lots to learn. Thanks again!

0 Kudos