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
And do you want to do this from a PowerCLI script ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes I do. The environment has 100s of hosts so I want to automate the task.
Ok, the steps as I see them
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
Excellent work! I will test it in the lab and see how it goes. Thanks for your help
Hi
I created the script, logged into the vcenter and ran it but it throws an error
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
}
}
There is a $ sign missing. The lines (2 times):
if(!ssh.Running){
should be
if(!$ssh.Running){
Ah yes, I should have seen that :). Thanks for pointing it out
The script above has been corrected
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The connection to the host is made but it errors out. It has an issue with the hostservice parameter. Is the syntax correct?
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
Yes that could be the issue. It's ESX 4.1. I'll add a different host to VC and check
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
I just tried with a 5.0 host and it worked :smileylaugh:. Thanks again