VMware Cloud Community
tdubb123
Expert
Expert

restart-vmhostservice with start-job

I am trying to restart a service with a background job since restart-vmhostservice does not support runasync

restartvmhostservice.ps1

$vmhosts = get-datacenter -name xx | get-vmhost

$vmhosts | get-vmhostservice | ?{$_.key -eq "TSM-SSH"} | Restart-VMHostService -confirm:$false

start-job -filename restartvmhostservice.ps1

the job says it completed sucessfully but i do not see a task or the service being restarted

any idea?

0 Kudos
3 Replies
LucD
Leadership
Leadership

You have to connect to the vCenter in the background job.

See Re: Stop New-hardDisk task/command if task taking more time or hung for an example


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

0 Kudos
tdubb123
Expert
Expert

hi lucd

When i ran the below it ran but says got blocked. any idea?

$script = {

param(

[string]$vcServer

)

Import-Module VMware.VimAutomation.Core

connect-viserver -Server $vcServer

$vmhosts = get-content "c:\scripts\esxhosts.txt"

foreach ($vmhost in $vmhosts) {

Get-VMHostService -VMHost $vmhost | ?{$_.key -eq "TSM-SSH"} | Restart-VMHostService -Confirm:$false

}}

}

start-job -Verbose -ScriptBlock $script -ArgumentList $global:DefaultVIServer.name

Screen Shot 2019-04-26 at 7.29.47 AM.png

0 Kudos
LucD
Leadership
Leadership

Some remarks, first you have to make sure that DepreciationWarnings are disabled.

Second, you better reuse the open connection by passing the SessionId of that connection.
Have a look at my Running a background job dive.


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

0 Kudos