Hi Everyone,
We have bunch of vm's which are failing for vMotion as some the of vmware tools installation were stuck. I am able to resolve the issue by following the below KB Article.
https://kb.vmware.com/s/article/1038542
Now, I am trying to write a script to automate the above process mentioned in the article.
I am trying to ssh into the host using posh-ssh module to find the vmid so we can cancel the tools installation task.
$session=New-SSHSession -ComputerName "hostname" -Credential $esxcred
$streamssh = New-SSHShellStream -SSHSession $session
$streamsshoutput=Invoke-SSHStreamShellCommand -ShellStream $streamssh -Command "vim-cmd vmsvc/getallvms"
I get the output
$streamsshoutput
vim-cmd vmsvc/getallvms
Vmid Name File Guest OS Version Annotation
1 TESTVM [Local-testhost01] TESTVM/TESTVM.vmx windows8_64Guest vmx-10
2 TESTVM2 [Local-testhost02] TESTVM2/TESTVM2.vmx windows8Server64Guest vmx-14
Now I need to filter only the VMID of the TESTVM2 so I can go ahead and cancel the task. I am not sure how to approach further on this.
Can someone help me in this process?
Try with
$vmName = 'TESTVM2'
$line = $streamsshoutput.Split("`n") | Select-Object -Skip 2 | Where {$_ -match $vmName}
$vmId = $line.Split()[0]
Write-Host "VM $vmName has VMId $vmId"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try with
$vmName = 'TESTVM2'
$line = $streamsshoutput.Split("`n") | Select-Object -Skip 2 | Where {$_ -match $vmName}
$vmId = $line.Split()[0]
Write-Host "VM $vmName has VMId $vmId"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you so much @LucD for the Quick reply. It worked 😍 I am able to complete the remaining script which I am looking for. I will post that here in sometime.