VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

deleting portgroups other than management on VSS_powercli

hello Luc,

can you check the following code   i want to  compare $pgs (which store all port gruopson vss) to $mgmt(which stores mangement port group on vss)

however there are unwanted strings  in $mgmt  i.e" key-vim.host.PortGroup-" can you suggest how to use remove or any easy method to get rid of this .

key-vim.host.PortGroup-VMNET-VLAN-410

key-vim.host.PortGroup-VM Network

key-vim.host.PortGroup-Management Network

foreach ($esxi in (get-vmhost -location $cluster))

{

#get-virtualswitch -vmhost $esxi -standard|select name,vmhost,@{N='portgroups';E={$_.extensiondata.Portgroup -join '|'}}

$vss=get-virtualswitch -vmhost $esxi -Standard

$pgs=$vss.ExtensionData.Portgroup

$pgs

$refpg =Get-VirtualPortGroup -Name "Management Network" -vmhost $esxi -standard

$mgmt=$refpg.name

$mgmt

if($pgs -cne "$mgmt")

{

write-host "there are portgrups other than managemnet network" -ForegroundColor DarkRed

}

else

{

write-host "only management network is configure on" $vss.name "on" $esxi.name

}

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

Get-VirtualPortGroup -Name 'PG1' | Get-VM


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

View solution in original post

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

I would do this in this way.

foreach ($esxi in (Get-VMHost -location $cluster))

{

  $vmkPg = Get-VMHostNetworkAdapter -VMHost $esxi -VMKernel | select -ExpandProperty PortGroupName

  Get-VirtualSwitch -Standard -VMHost $esxi | Get-VirtualPortGroup |

  where{$vmkpg -notcontains $_.Name} |

  select @{N='VMHost';E={$esxi.Name}},@{N='Portgroup';E={$_.Name}}

}


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

ThnaksLuc iam checking this but for the time bieng i compared with key not with  names .

is there any property where in if i can check what vms are configured to standard port group.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

Get-VirtualPortGroup -Name 'PG1' | Get-VM


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

hello Luc ,

can yu please suggest what conditional operator shoud i use to compare two  $pgs.name  (containing name of all  spg including virtual machine and vmkernel) and $mgmt.name.

following code does not give right output . can i use -eq for comparing two arrays .

foreach ($esxi in (get-vmhost -location $cluster))

{

$vss=get-virtualswitch -vmhost $esxi -Standard

$pgs=$vss|get-virtualportgroup

$mgmt =Get-VirtualPortGroup -Name "Management Network" -vmhost $esxi -standard

if($pgs.name -eq "$mgmt.name")

{

write-host "only management network is configured on" $vss.name "on" $esxi.name -ForegroundColor DarkGreen

}

else

{

write-host "there are portgrups other than managemnet network on" $vss.name "on" $esxi.name -ForegroundColor DarkRed

}

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try something like this

foreach ($esxi in (Get-VMHost -Location $cluster))

{

    foreach($vss in (Get-VirtualSwitch -VMHost $esxi -Standard)){

        $pgs = Get-VirtualPortGroup -VirtualSwitch $vss

        

        $mgmt = Get-VirtualPortGroup -VirtualSwitch $vss -Name "Management Network" -VMHost $esxi -Standard

        

        if((Compare-Object -ReferenceObject $pgs.name -DifferenceObject $mgmt.name -PassThru) -eq $null)

        {

            Write-Host "only management network is configured on" $vss.name "on" $esxi.name -ForegroundColor DarkGreen

        }

        else

        {

            Write-Host "there are portgrups other than managemnet network on" $vss.name "on" $esxi.name -ForegroundColor DarkRed

        }

    }

}


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

Thanks Luc .

Reply
0 Kudos