6 Replies Latest reply on Dec 14, 2017 5:25 AM by jvm2016

    deleting portgroups other than management on VSS_powercli

    jvm2016 Enthusiast

      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

       

      }

        • 1. Re: deleting portgroups other than management on VSS_powercli
          LucD Guru
          Community WarriorsvExpertUser Moderators

          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}}

          }

          • 2. Re: deleting portgroups other than management on VSS_powercli
            jvm2016 Enthusiast

            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.

            • 3. Re: deleting portgroups other than management on VSS_powercli
              LucD Guru
              User ModeratorsCommunity WarriorsvExpert

              Try like this

               

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

               

              • 4. Re: deleting portgroups other than management on VSS_powercli
                jvm2016 Enthusiast

                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

                 

                 

                 

                }

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                }

                • 5. Re: deleting portgroups other than management on VSS_powercli
                  LucD Guru
                  vExpertCommunity WarriorsUser Moderators

                  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

                          }

                      }

                  }