VMware Cloud Community
DyJohnnY
Enthusiast
Enthusiast

Get Port-Profile and VLAN-ID information from distributed switch

Hi,

I'm trying to create a script that will automatically reconfigure VMs by changing the port-profile from a dvswitch(1000v) to a vswitch.

For that i'm trying to create a matrix that will give me following:

For vswitch

PortgroupName,VLANID

for distributed Switch

PortProfile,VLAN ID

However I do not know how to get the last part (the vlan ID of a port profile in a nexus dvs). I did notice the port VLAN ID information is available on a "per port connected in Cisco 1000V dVS". I'm ok with this information, since all our port profiles are access-ports (no trunks, no private vlans).

I've attached a file with the view from :

networking > port-profileA > ports tab from the client.

I've tried using Onyx to get the code but I can't.

Thanks for helping,

Ionut

IonutN
Tags (3)
0 Kudos
5 Replies
LucD
Leadership
Leadership

The Nexus is installed as an extension to vCenter and the API are private and hence not visible in Onyx I'm afraid.


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

DyJohnnY
Enthusiast
Enthusiast

Hi,

I'm not really interested in doing things throught onyx. I just want to find the VLAN-ID of a port group, or a port connected to a port-profile, via powershell, like the vsphere client displays it on the screenshot I attached. Can this be done?

Thanks,

Ionut

IonutN
0 Kudos
DyJohnnY
Enthusiast
Enthusiast

I managed to answer this question myself now. It seems the information is somehow grabbed by the API from Cisco, don't really know how.

Here is the relevant piece of code

$VirtualPortGroups = Get-VirtualPortGroup
$VirtualPortGroups | where { $_.Key -like '*dvportgroup*' } | % {
    $row = "" | select PortGroupName,VLANID,SwitchName,Type,Hosts
    $row.PortGroupName = $_.Name
    $row.VLANID = $_.Notes.Split(' ')[1]
    $row.SwitchName = (get-view $_.ExtensionData.config.DistributedVirtualSwitch).Name
    $row.Type = "dvSwitch"
    $row.Hosts = (get-view $_.Id).host |  % { (get-view $_).Name}
    $Report += $row
    }

I noticed the Notes field sometimes contains "VLANID XXXX" or "vlanid XXXX" which made me use .Split instead ot Trim, or replace.

I checked with our network guys and they did not put a description on the port profiles (so it is not a description set on the profiles).

Anyhow, hope someone else can find this useful aswell.


Ionut

IonutN
0 Kudos
mry
Contributor
Contributor

Thanks for the script really helped me... didnt need the host + my description fields wasnt as nicely done as yours... so made a few changes to it, plus added an export feature...

Just wanna share it with you all Smiley Happy

$Report = @()
function Test-Array { if ( $Args[0] -is [system.array] ) { $true } }
$VirtualPortGroups = get-virtualportgroup -distributed | Sort Name
$VirtualPortGroups | where { $_.Key -like '*dvportgroup*' } | % {
    $row = "" | select PortGroupName,VLANID,SwitchName,Type
    $row.PortGroupName = $_.Name
   
    $array = ($_.Notes -split "\D" |? {$_ -match "\d"})
    if(Test-Array $array){
    $row.VLANID = $array[0]
    }else{
    $row.VLANID = $array}
   
    $row.SwitchName = (get-view $_.ExtensionData.config.DistributedVirtualSwitch).Name
    $row.Type = "dvSwitch"
    $Report += $row
    }
$Report | Export-Csv -Delimiter ";" c:\temp\Export-vDSVLan.csv -NoTypeInformation

0 Kudos
andreasbrunner
Contributor
Contributor

@mry

I just tried your script but the VLANID row remains blank in the report. I did not received any errors.

I use powercli 5.01 in a vsphere 4.1 u2 environment

regards

Andreas

0 Kudos