2 Replies Latest reply on Oct 30, 2019 7:43 AM by vespavbb

    get custom attribute if like and

    vespavbb Novice

      Hi,

       

      I have a Script which I want to modify. This works well, but I want to ad something like below

      -----

      ->   if ($_.Value.StartsWith("DE-") -eq $true -or $_.Value.StartsWith("UK-) -eq $true) -and $customFieldName2 -eq "Prod" , the bold command I dont know how I can do the -and in a if loop

       

       

       

      Script org.

      ##############

      # create a list

      $customFieldName1 = "Location"

      $customFieldName2 = "ProductionState"

      $attrib = Get-CustomAttribute -Name $customFieldName1

      Write-Host $attrib

      $filteredVMs = Get-View -ViewType virtualmachine -Property 'CustomValue' |?{$_.customvalue}

      $customGroups = @()

      $filteredVMs | ForEach {

          $_.CustomValue | ForEach {

              if ($_.Key -eq $attrib.Key) {

                  if ($_.Value.StartsWith("DE-") -eq $true -or $_.Value.StartsWith("UK-) -eq $true)

                  {

                      $customGroups += $_.Value

                  }

              }

          }

      }

        • 1. Re: get custom attribute if like and
          LucD Guru
          Community WarriorsUser ModeratorsvExpert

          Would this work for you?

           

          $customFieldName1 = "Location"

          $customFieldName2 = "ProductionState"


          $attrib1 = Get-CustomAttribute -Name $customFieldName1

          $attrib2 = Get-CustomAttribute -Name $customFieldName2


          $filteredVMs = Get-View -ViewType virtualmachine -Property 'CustomValue'  | Where-Object{$_.customvalue}

          $customGroups = @()

          $filteredVMs | ForEach {

              $vmCust1 = $_.CustomValue | where{$_.Key -eq $attrib1}

              $vmCust2 = $_.CustomValue | where{$_.Key -eq $attrib2}

              if(($vmCust1.Value.StartsWith("DE-") -or $vmCust1.Value.StartsWith("UK-")) -and $vmCust2.Value -eq 'Prod'){

                  $customGroups += $_.Value

              }

          }

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: get custom attribute if like and
            vespavbb Novice

            this could work yes, thank.

            i will try