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?
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
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
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