VMware Cloud Community
kjoyce_SA
Contributor
Contributor
Jump to solution

PowerCLI script to compare items in array to condition

So I am working on a script that will kick off SCCM updates for newly built VMs. The issue I am running into is that each required update is put into an array with a property called evaluationstate. I only want the script to continue after all updates in the array have an evalstate of either 8 or 13 (success or fail). Not sure how I would accomplish that. Any help would be appreciated

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could do something like this.
Try changing one the Evaluationstate values in the array of objects.

$tab = @(

    @{

        EvaluationState = 8

    },

    @{

        EvaluationState = 8

    },

    @{

        EvaluationState = 13

    },

    @{

        EvaluationState = 8

    },

    @{

        EvaluationState = 13

    }

)

if(-not (($tab.evaluationstate | %{8,13 -contains $_}) -contains $false)){

    Write-Host "All are 8 or 13"

}

else{

    Write-Host "Not all are 8 or 13"

}

With the same test you could wait in a While-loop.

You didn't specify how that array is populated, and how the evaluationstate property can be updated, so I assume it is done auto-magically.

If not you would need to refresh the content of the array some way or the other.

while(($tab.evaluationstate | %{8,13 -contains $_}) -contains $false){

    sleep 5

}


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

You could do something like this.
Try changing one the Evaluationstate values in the array of objects.

$tab = @(

    @{

        EvaluationState = 8

    },

    @{

        EvaluationState = 8

    },

    @{

        EvaluationState = 13

    },

    @{

        EvaluationState = 8

    },

    @{

        EvaluationState = 13

    }

)

if(-not (($tab.evaluationstate | %{8,13 -contains $_}) -contains $false)){

    Write-Host "All are 8 or 13"

}

else{

    Write-Host "Not all are 8 or 13"

}

With the same test you could wait in a While-loop.

You didn't specify how that array is populated, and how the evaluationstate property can be updated, so I assume it is done auto-magically.

If not you would need to refresh the content of the array some way or the other.

while(($tab.evaluationstate | %{8,13 -contains $_}) -contains $false){

    sleep 5

}


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

0 Kudos
kjoyce_SA
Contributor
Contributor
Jump to solution

Thanks. Just to clarify on the While loop, as you have it written if EvaluationState doesn't equal 8 or 13 for all items it will wait 5 secs and check again?

0 Kudos
kjoyce_SA
Contributor
Contributor
Jump to solution

Thanks, I ended up using our syntax with a Do Until loop. Just curious but I noticed the statement used a double negative. Any reason for that?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The expression is build up from the following:

  • the expression $tab.evaluationstate will return an array with the values of evaluationstate for all objects in the array
  • then %{8,13 -contains $_} will test each returned value if it is 8 or 13, the result is an array of $true and $false
  • with -contains $false, the expression checks if there is a $false value in that array
  • and finally, the -not negates the result, so the Then clause can be the success part. You could leave out the -not, and just switch the Then and Else blocks


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

0 Kudos