VMware Cloud Community
chakoe
Enthusiast
Enthusiast

Run a command on multiple ESX-Hosts

Hi,

i want to run a command on multiple ESX-Hosts. Therefor i want to build a list of hostnames, the command should run on.

1)     Trying Get-Datacenter "RZ" | Get-VMHost * | Where {$_.CpuUsageMhz -eq "0"} | Sort-Object $_.Name | Select Name

        results in a simple list of hostnames...this is what i want

2)     $hosts = Get-Datacenter "RZ D MS-GR" | Get-VMHost * | Where {$_.CpuUsageMhz -eq "0"} | Sort-Object $_.Name | Select Name

        results in a list of Hostnames, but it is in the following format: {@{Name=hostname.datacenter}

          I do not want to have the {@Name=...} in the output of the variable, because the script cannot use it...

How to i change this?

Thx

Chakoe

Reply
0 Kudos
3 Replies
RvdNieuwendijk
Leadership
Leadership

You can use:

$hosts = Get-Datacenter "RZ D MS-GR" |
Get-VMHost * |
Where {$_.CpuUsageMhz -eq "0"} |
Sort-Object $_.Name |
ForEach-Object {$_.Name}

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

Hi,

i´ve corrected it, and it seems to work now, but i have the problem, that the script stops after running the commands on the first host. Here´s the actual

script content:

$hosts = Get-Datacenter "RZ " | Get-VMHost * | Where {$_.CpuUsageMhz -eq "0"} | Sort-Object $_.Name | Select $_.Name
foreach ( $singlehost in $hosts ) {
Write-Host $singlehost.Name
$User = "root"
$Pswd = <ourPassword>
$hostName = $singlehost.Name
$plink = "C:\plink.exe"
$plinkoptions = " -v -batch -pw $Pswd"
$cmd1 = '/sbin/service mgmt-vmware restart'
$remoteCommand = '"' + $cmd1 + '"'
$command = $plink + " " + $plinkoptions + " " + $User + "@" + $hostName + " " + $remoteCommand
Invoke-Expression -command $command
$cmd2 = '/sbin/service vmware-vpxa restart'
$remoteCommand = '"' + $cmd2 + '"'
$command = $plink + " " + $plinkoptions + " " + $User + "@" + $hostName + " " + $remoteCommand
Invoke-Expression -command $command
$cmd3 = 'logout'
$remoteCommand = '"' + $cmd3 + '"'
$command = $plink + " " + $plinkoptions + " " + $User + "@" + $hostName + " " + $remoteCommand
Invoke-Expression -command $command
}

And here´s the output:

<hostname.datacenter>

plink.exe : Server version: SSH-2.0-OpenSSH_4.3

At line:1 char:13

+ C:\plink.exe <<<<   -v -batch -pw <OurPAssword> root@hostname.datacenter "/sbin/service mgmt-vmware restart"

    + CategoryInfo          : NotSpecified: (Server version: SSH-2.0-OpenSSH_4.3:String) [], RemoteException

    + FullyQualifiedErrorId : NativeCommandError

We claim version: SSH-2.0-PuTTY_Release_0.58

Using SSH protocol version 2

Doing Diffie-Hellman group exchange

Doing Diffie-Hellman key exchange

Host key fingerprint is:

ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx

Initialised AES-256 client->server encryption

Initialised HMAC-SHA1 client->server MAC algorithm

Initialised AES-256 server->client encryption

Initialised HMAC-SHA1 server->client MAC algorithm

Using username "root".

********************************************************************************

   Message of the day

********************************************************************************

Sent password

Access granted

Opened channel for session

Started a shell/command

Stopping VMware ESX Management services:

   VMware ESX Host Agent Watchdog[  OK  ]

   VMware ESX Host Agent[  OK  ]

Starting VMware ESX Management services:

   VMware ESX Host Agent (background)[  OK  ]

   Availability report startup (background)[  OK  ]

Server sent command exit status 0

Here the script stops....

the script is based on this article: http://communities.vmware.com/message/1388491

Any idea?

Reply
0 Kudos
LucD
Leadership
Leadership

The problem is that the SSH connection (behind the plink.exe) prompts you to save the key for the ESXi host.

This happens the first time you connect, but with plink.exe there is no option to save that key.

Try connecting first with PuTTY.exe to that host, you'll be prompted to save the key.

Save the key and now the plink.exe should work.

Btw you can avoid the @{Name="..."} construct with the ExpandProperty parameter.

Something like this

$hosts = Get-Datacenter "RZ D MS-GR" |
Get-VMHost * | Where {$_.CpuUsageMhz -eq "0"} | Sort-Object $_.Name |
Select -ExpandProperty Name


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

Reply
0 Kudos