ronaldoalv
Contributor
Contributor

JOB Object

Jump to solution

hi all,

is there any object that i can check how the jobs followed after I create them in powerCLI? (especially when I create them with "-RunAsync")

for example :

New-vm -vmhost xxx.xxx.xx.xx -Name $name -RunAsync

and I now I want to print the status or how much percent,

so, how i can get that jobs object?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Did you try the Get-Task cmdlet ?


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

View solution in original post

0 Kudos
7 Replies
LucD
Leadership
Leadership

Did you try the Get-Task cmdlet ?


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

View solution in original post

0 Kudos
ronaldoalv
Contributor
Contributor

exactly,

thanks!!! Smiley Happy

0 Kudos
ronaldoalv
Contributor
Contributor

I try it and if I use it like that:

     $task=New-vm -vmhost xxx.xxx.xx.xx -Name $name -RunAsync

$task isn't change,it means even I use wait-task with this task, and it's finish, if I check the variable $task it's still in the same status:

Name                                       State      % Complete   Start Time   Finish Time

----                                            -----      ----------          ----------   -----------

CloneVM_Task                        Running       18             08:50:49 AM

nothing change....

how i can refreshing it?

thank

0 Kudos
LucD
Leadership
Leadership

When you use the Get-Task cmdlet, you should get a list of all tasks.

Monitor the status of your task(s) in a Where-loop.

Something like this

$myTask = Get-Task | where {$_.Name -match "CloneVM"}

while ($myTask.State -eq "running") {

    sleep 5

    $myTask = Get-Task | where {$_.Name -match "CloneVM"}

}

When there are multiple clone jobs running, the logic becomes a bit more complex.


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

0 Kudos
ronaldoalv
Contributor
Contributor

I want to use something like that:

     $allTask = Get-Task | where {$_.Id -match $_.Id}

but in this command both $_.Id are reference to the Get-Task task's

and I what that the second one will refer to the  $allTask task's

for example :

        $allTask=($task2,$task3)

        Get-Task = ($task1,$task2,$task3,$task4)

     now I what to get only $task2,$task3 from Get-Task and insert them to $allTask

How I can do this command?

thanks

0 Kudos
LucD
Leadership
Leadership

Since $allTask is an array, you can use the index notation to address the 2nd one.

Like this

Get-Task | where {$_.Id -match $allTask[1].Id}


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

0 Kudos
ronaldoalv
Contributor
Contributor

finally I use this script:

     $allTask=$args[0]

     $sleeptime=$args[1]

     while (($allTask | where {$_.State -eq "running"}).count -gt 0) {

            $i=0

            $temp=@()    

            while ($i -le $allTask.Count){

                 $temp = $temp + (Get-Task | where {$_.Id -match $allTask[$i].Id})    

                 $i++

            }

            $allTask=$temp

            Sleep -s $sleeptime

     }

0 Kudos