VMware Cloud Community
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

 

 

 

 

 

 

 

 
Reply
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é

Reply
0 Kudos
a_p_
Leadership
Leadership

Moderator note: Moved to VMware PowerCLI Discussions

 

Reply
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

Reply
0 Kudos