Highlighted
Enthusiast
Enthusiast

Stop Task via powercli

i have a task that is hung in vcenter 6.5 and i cannot cancel it via the web.

powercli Get-Task state is "running" at 0% for the last hour. do i need to retrieve the task id in order to run a "stop-task"?

the task is "cancel remediating entity"

Tags (1)
0 Kudos
5 Replies
Highlighted
User Moderator
User Moderator

Do you see the Task listed when you do

$taskMgr = Get-View TaskManager

if($taskMgr.RecentTask){

    Get-View -Id $taskMgr.RecentTask

}


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

0 Kudos
Highlighted
Enthusiast
Enthusiast

unfortunately the task error'ed right as i was reading your comment. regardless, i need to know how to do this for the future. lets pretend the task does show up.

0 Kudos
Highlighted
User Moderator
User Moderator

You could try something like this.

But note that not all tasks are cancelable.

$taskName = '<task-name>'

$taskMgr = Get-View TaskManager

if($taskMgr.RecentTask){

    $task = Get-View -Id $taskMgr.RecentTask | where {$_.Info.Name -eq $taskName}

    if('queued','running' -contains $task.Info.State -and $task.Info.Cancelable){

        $task.CancelTask()

    }

    else{

        if(-not $task.Info.Cancelable){

            Write-Host "Task not cancelable"

        }

        if('queued','running' -notcontains $task.Info.State){

            Write-Host "Task not canceled due to state $($task.Info.State)"

        }

    }

}


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

0 Kudos
Highlighted
Enthusiast
Enthusiast

was able to re-create the issue. doesnt appear to cancel the task. could it be that this task cant be canceled?

Capture.PNG

0 Kudos
Highlighted
User Moderator
User Moderator

You can check with $task.Info.Cancelable.

I added that condition in the script above.


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

0 Kudos