Automation

 View Only
  • 1.  Get PortGroup List

    Posted Jul 16, 2008 10:23 AM

    I'm trying to build a script that extracts all PortGroups with a certain VLAN ID. The only thing that's missing is the output :smileywink: Obviously I'm doing something completely wrong, but I don't know what. Somebody can help me out?

    Get-VIServer xxx +-User +xxx +-Password +xxx

    $MyHost = Get-VMHost xxx

    $HostView = Get-View $MyHost.ID

    $NetworkSystem = get-view $HostView.ConfigManager.NetworkSystem

    $AllPortGroups = get-view $NetworkSystem.NetworkInfo.Portgroup | where {$_.HostPortGroupSpec.VlanId > 0 }

    Foreach ($HostPortGroup in $AllPortGroups){

    write-output $_.HostPortGroupSpec.Name

    }



  • 2.  RE: Get PortGroup List
    Best Answer

    Posted Jul 16, 2008 11:13 AM

    This should work

    
    Get-VIServer xxx +-User +xxx +-Password +xxx
    $MyHost = Get-VMHost xxx
    $HostView = Get-View $MyHost.ID
    $NetworkSystem = get-view $HostView.ConfigManager.NetworkSystem
    $AllPortGroups = $NetworkSystem.NetworkInfo.Portgroup | where {$_.Spec.VlanId -gt 0 }
    foreach ($HostPortGroup in $AllPortGroups){
    write-output $HostPortGroup.Spec.Name
    }
    
    

    Note1: Portgroup is an array not a MoRef

    Note2: the property name is Spec not HostPortGroupSPec

    Note3: for the comparison use -gt instead of the redirection operator



  • 3.  RE: Get PortGroup List

    Posted Jul 16, 2008 11:43 AM

    You're really amazing, it works, many thanks for the correction, I was going to give you maximum points for the correct answer but clicked the wrong icon sorry about that.



  • 4.  RE: Get PortGroup List

    Posted Jul 16, 2008 11:51 AM

    No problem.

    Glad to have helped.



  • 5.  RE: Get PortGroup List

    Posted Jul 16, 2008 12:54 PM

    LucD,

    Just one more question, when I try to pipe the output to a CSV file I only see one entry.

    {write-output $HostPortGroup.Spec | Select-Object Name,VlanId | Export-Csv c:\test.txt}



  • 6.  RE: Get PortGroup List

    Posted Jul 16, 2008 04:38 PM

    This should create the CSV file.

    Get-VIServer xxx +-User +xxx +-Password +xxx
    $MyHost = Get-VMHost xxx
    $HostView = Get-View $MyHost.ID
    $NetworkSystem = get-view $HostView.ConfigManager.NetworkSystem
    $AllPortGroups = $NetworkSystem.NetworkInfo.Portgroup | where {$_.Spec.VlanId -gt 0 }
    
    $vlanReport = @()
    
    foreach ($HostPortGroup in $AllPortGroups){
      $vlanInfo = "" | select-object Name, VLANId
      $vlanInfo.Name = $HostPortGroup.Spec.Name
      $vlanInfo.VLANId = $HostPortGroup.Spec.VlanId
      $vlanReport += $vlanInfo
    }
    $vlanReport | Export-Csv c:\test.csv
    

    I used an array ($vlanReport) to store custom objects ($vlanInfo) for each portgroup.

    Via the pipe the array is written to the CSV file



  • 7.  RE: Get PortGroup List

    Posted Jul 17, 2008 07:10 PM

    I received some odd error and created a workaround:

    {$HostPortGroup.Spec | Select-Object Name,VlanId | Out-File -filepath "C:\portgroups.txt" -append}

    Thanks for all the help.