AlanRaczek
Contributor
Contributor

Progress of a task

Found this code here:

$t = New-VM -Name NewVM -Template stress -Datastore MyDS -VMHost MyEsx -RunAsync

while('Running','Queued' -contains $t.State){

 Write-Progress -Activity 'Creating VM' -PercentComplete $t.PercentComplete

    $t = Get-Task -Id $t.Id

}

Write-Output "Task '$($t.Description)/$($t.Id)' for $(Get-View -Id $t.ObjectId -Property Name | select -ExpandProperty Name) ended with status $($t.State)"

I am a little thrown off by the line: while('Running','Queued' -contains $t.State)  . I am by no means a powershell expert and I am asking. I would think that is backwards. I would think it would be ($t.state -contains "Running', 'Queued') . To me that says "if the string Running or Queued contains the "state" . I have never seen it done like that. I most likely am missing something! 

 

TIA

 

 

 

 

 

 

 

 
0 Kudos
3 Replies
a_p_
Leadership
Leadership

From how I understand this, 'Running','Queued' will be interpreted as an array, and the $t.State is evaluated against it.
A nice alternative to multiple if statements.

André

0 Kudos
a_p_
Leadership
Leadership

Moderator note: Moved to VMware PowerCLI Discussions

 

0 Kudos
LucD
Leadership
Leadership

The explanation is that 'Running','Queued' is not a string but an array of strings (the comma between the 2 strings does that).
So in fact you should read: if the collection (array) of strings contains the state ($t.State) the condition evaluates to $true.


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

0 Kudos