VMware Cloud Community
admin
Immortal
Immortal

esxupdate

Hi Folks

I am looking to run the following on multiple hosts in the same vCenter

"esxupdate clearpending"

I would guess that the code would be fairly simple if it can be done. Any thoughts welcome

Thanks

Rob

0 Kudos
13 Replies
LucD
Leadership
Leadership

And do you want to do this from a PowerCLI script ?


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

0 Kudos
admin
Immortal
Immortal

Yes I do. The environment has 100s of hosts so I want to automate the task.

0 Kudos
LucD
Leadership
Leadership

Ok, the steps as I see them

  1. Loop through all the ESXi servers
  2. Enable SSH (if it is not yet enabled)
  3. Excute the command with plink.exe on the ESXi server
  4. Place the SSH service back to the state is was before

The script could look something like this

$User = <ESX-account> 
$Pswd
= <ESX-password>
$plink
= "<PuTTY-directory>\plink.exe"
$plinkoptions
= " -v -batch -pw $Pswd"
$cmd1
= 'esxupdate clearpending'
foreach
($esx in Get-VMHost){   $ssh = Get-VMHostService -VMHost $esx | where {$_.Key -eq "TSM-SSH"}   if(!$ssh.Running){     Start-VMHostService -HostService $ssh -Confirm:$false | Out-Null
  }   $remoteCommand = '"' + $cmd1 + '"'
  $command = $plink + " " + $plinkoptions + " " + $User + "@" + $esx.Name + " " + $remoteCommand
 
$msg = Invoke-Expression -command $command
 
if(!$ssh.Running){     Stop-VMHostService -HostService $ssh -Confirm:$false | Out-Null
  } }

This assumes that the account and password are the same for all ESXi servers.

Also have a look at Alan's SSH PowerShell tricks with plink.exe, it shows how to answer to the prompt to accept the host key (when it's the first time you use a SSH connection to that ESXi server).


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

0 Kudos
admin
Immortal
Immortal

Excellent work! I will test it in the lab and see how it goes. Thanks for your help

0 Kudos
admin
Immortal
Immortal

Hi

I created the script, logged into the vcenter and ran it but it throws an error

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> .\update
.ps1
Missing expression after unary operator '!'.
At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\update.ps1:9 c
har:7
+   if(! <<<< ssh.Running){
    + CategoryInfo          : ParserError: (!:String) [], ParseException
    + FullyQualifiedErrorId : MissingExpressionAfterOperator
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

Here's the script

$User = root

$Pswd = vmware

$plink = "c:\putty\plink.exe"

$plinkoptions = " -v -batch -pw $Pswd"

$cmd1 = 'esxupdate clearpending'

foreach($esx in Get-VMHost){

  $ssh = Get-VMHostService -VMHost $esx | where {$_.Key -eq "TSM-SSH"}

  if(!ssh.Running){

    Start-VMHostService -HostService $ssh -Confirm:$false | Out-Null

  }

  $remoteCommand = '"' + $cmd1 + '"'

  $command = $plink + " " + $plinkoptions + " " + $User + "@" + $esx.Name + " " +

$remoteCommand

  $msg = Invoke-Expression -command $command

  if(!ssh.Running){

    Stop-VMHostService -HostService $ssh -Confirm:$false | Out-Null

  }

}

0 Kudos
RvdNieuwendijk
Leadership
Leadership

There is a $ sign missing. The lines (2 times):

if(!ssh.Running){

should be

if(!$ssh.Running){

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
admin
Immortal
Immortal

Ah yes, I should have seen that :). Thanks for pointing it out

0 Kudos
LucD
Leadership
Leadership

The script above has been corrected


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

0 Kudos
admin
Immortal
Immortal

The connection to the host is made but it errors out. It has an issue with the hostservice parameter. Is the syntax correct?

Disconnected: All channels closed
Stop-VMHostService : Cannot validate argument on parameter 'HostService'. The a
rgument is null or empty. Supply an argument that is not null or empty and then
try the command again.
At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\update.ps1:18
char:36
+     Stop-VMHostService -HostService <<<<   $ssh -Confirm:$false | Out-Null
    + CategoryInfo          : InvalidData: (:) [Stop-VMHostService], Parameter
   BindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
   ation.ViCore.Cmdlets.Commands.StopVMHostService
0 Kudos
LucD
Leadership
Leadership

It looks as if $ssh is empty.

Does

Get-VMHostService -VMHost $esx | where {$_.Key -eq "TSM-SSH"}

return anything ?

Could it be that you are running this against an older ESXi version ?

What names does

Get-VMHostService -VMHost $esx | Select Key 

return ?


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

0 Kudos
admin
Immortal
Immortal

Yes that could be the issue. It's ESX 4.1. I'll add a different host to VC and check

0 Kudos
LucD
Leadership
Leadership

You have to replace TSM-SSH with the key for the ESX 4 SSH service.

Or if you know that the SSH service is running, you can remove those start/stop parts in the script


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

0 Kudos
admin
Immortal
Immortal

I just tried with a 5.0 host and it worked :smileylaugh:. Thanks again

0 Kudos