Sureshadmin
Contributor
Contributor

Script to kill a hung vCenter task

hi,

I'm looking for a script to kill a hung vCenter task. There was esx box(v3.5) which had problems and i has to disconnect and reconnect back in vCenter. When i tried to reconnect the esx box back, the task is in "In progress" state for several hours without timing out. I tried restarting mgmt-vmware and virtual center services in the ESX box but no luck. Can this task be killed via a powershell script or do i have to restart vCenter services?

LucD
Leadership
Leadership

That depends, some tasks are not cancelable.

You would first have to "get" the task ID and then you can use the Stop-Task cmdlet.

Get-Task -Status "running"

will return all running task and you will have to select the one you're trying to kill.

Then you can use the Stop-Task cmdlet.

$task = Get-Task -Status "Running" | where {$_.Name -eq $tgtName}
Stop-Task -Task $task -Confirm:$false

Here the task is selected based on the name, but there are other properties you could use.

____________

Blog: LucD notes

Twitter: lucd22


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

Sureshadmin
Contributor
Contributor

Luc,

You're right. I got a error msg "This task is not cancellable"

So is there a way from powershell to force disconnect a ESX server from VC and de-register all the virtual machines in that esxbox and reconnect it back to the virtual center?

Reply
0 Kudos
LucD
Leadership
Leadership

To remove all VMs on a specific host from the inventory, you can do

Get-VMHost -Name <esxname> | Get-VM | Remove-VM -DeleteFromDisk:$false

To disconnect/connect an ESX(i) host you can do the following.

I assume there are no guests running on the host.

$esxName = <esxname>
Get-VMHost -Name $esxName | Remove-VMHost
Add-VMHost -Name $esxName -Credential (Get-Credential) 

To add the guests back to the inventory, you could eventually use the script from my Raiders of the Lost VMX post.

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

Sureshadmin
Contributor
Contributor

Thanks much Luc. Finally i disconnected that host from VC.

Is it possible from powershell to retrieve a list of all services running/stopped on a ESX(i) host just like the output of "service --status-all" command?

Reply
0 Kudos
LucD
Leadership
Leadership

I'm afraid not witha PowerCLI cmdlet.

This

Get-VMHost $esxName | Get-VMHostService

only returns the status of the ntpd, sshd,vmware-vpxa and vmware-webaccess services.

You can of course use the plink.exe trick to launch the "service --status-all" command and capture the output.

Something like this (provided sudo is set up correctly)

$ESXserver = <esxname>

$User = <esx-account>
$Pswd = <esx-password>
$plink = <PuTTY directory>\Putty\plink.exe"
$plinkoptions = "-v -batch -pw $Pswd"
$cmd1 = "/sbin/service --status-all"

$remoteCommand = "'" + $cmd1 + "'"
$command = $plink + " " + $plinkoptions + " " + $User + "@" + $ESXserver + " " + $remoteCommand

$msg = Invoke-Expression -command $command

____________

Blog: LucD notes

Twitter: lucd22


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

Sureshadmin
Contributor
Contributor

Thank you so much.

Reply
0 Kudos
baber
Expert
Expert

I don't want use it with variable for example when I run :

Get-Task -Status "Running"

it shows follow  :

Name ;                    state:                 complete:

Deploy myvm             running               0%

Now I want use follow command and just type the task name . How can do it ?

Stop-Task -Task

 

Please mark helpful or correct if my answer resolved your issue.
Reply
0 Kudos
LucD
Leadership
Leadership

How is this related in any way to the original question in this thread from 2010?


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

Reply
0 Kudos
baber
Expert
Expert

No problem . I will open new topic

Please mark helpful or correct if my answer resolved your issue.
Reply
0 Kudos