VMware Cloud Community
vespavbb
Enthusiast
Enthusiast

get custom attribute if like and

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

            }

        }

    }

}

VCP4,VCP5,VCP6,VCP7,VCP8
0 Kudos
2 Replies
LucD
Leadership
Leadership

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vespavbb
Enthusiast
Enthusiast

this could work yes, thank.

i will try

VCP4,VCP5,VCP6,VCP7,VCP8
0 Kudos