-
1. Re: Need powerCLi script to execute command on multiple ESXi hosts
LucD Mar 8, 2017 5:06 AM (in response to swamynaveen)Is SSH enabled, or can it be enabled, on those ESXi nodes?
-
2. Re: Need powerCLi script to execute command on multiple ESXi hosts
swamynaveen Mar 10, 2017 6:25 AM (in response to LucD)Yeah, It can be enabled. Script should be validated before executing it.
-
3. Re: Need powerCLi script to execute command on multiple ESXi hosts
LucD Mar 10, 2017 7:10 AM (in response to swamynaveen)What do you mean by "validated"?
-
4. Re: Need powerCLi script to execute command on multiple ESXi hosts
swamynaveen Mar 13, 2017 11:25 PM (in response to LucD)Hi LucD,
I mean script should check the SSH status before executing it. While executing below script was throwing an error. Could you please help me out to complete this task.
Thank you in Advance!!
$root = "root"
$Passwd = "supersecret"
$esxlist = "xx.xx.xx.xx"
$cmd = @'
"cp /var/spool/cron/crontabs/root /var/spool/cron/crontabs/root.bak && sed -e '/device\ purge/s/^/#/g' -i /var/spool/cron/crontabs/root"
'@
$plink = "<path to >\plink.exe" #Provide the path of plink
$PlinkOptions = " -v -batch -pw $Passwd"
$remoteCommand = '"' + $cmd + '"'
foreach ($esx in $esxlist) {
Connect-VIServer $esx -User $root -Password $Passwd
Write-Host -Object "starting ssh services on $esx"
$sshstatus= Get-VMHostService -VMHost $esx| where {$psitem.key -eq "tsm-ssh"}
if ($sshstatus.Running -eq $False) {
Get-VMHostService | where {$psitem.key -eq "tsm-ssh"} | Start-VMHostService }
Write-Host -Object "Executing Command on $esx"
$output = $plink + " " + $plinkoptions + " " + $root + "@" + $esx + " " + $remoteCommand
}
$message = Invoke-Expression -command $output
$message
Error:
=======
Invoke-Expression : At line:1 char:133
+ ... spool/cron/crontabs/root /var/spool/cron/crontabs/root.bak && sed -e ...
+ ~~
The token '&&' is not a valid statement separator in this version.
At E:\Plink\configchange.ps1:19 char:12
+ $message = Invoke-Expression -command $output
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Invoke-Expression], ParseExcep
tion
+ FullyQualifiedErrorId : InvalidEndOfLine,Microsoft.PowerShell.Commands.I
nvokeExpressionCommand
-
5. Re: Need powerCLi script to execute command on multiple ESXi hosts
swamynaveen Mar 13, 2017 11:56 PM (in response to swamynaveen)Hi LucD,
To workaround this error I've used separate variables. Now, it has executed without any error.
$root = "root"
$Passwd = "XXXX"
$esxlist = "XXXX"
$cmd = @'
"cp /var/spool/cron/crontabs/root /var/spool/cron/crontabs/root.bak "
'@
$cmd1= @'
"sed -e '/device\ purge/s/^/#/g' -i /var/spool/cron/crontabs/root"
'@
$plink = "E:\Plink\plink.exe" #Provide the path of plink
$PlinkOptions = " -v -batch -pw $Passwd"
$remoteCommand = '"' + $cmd + '"'
$remoteCommand1 = '"' + $cmd1 + '"'
foreach ($esx in $esxlist) {
Connect-VIServer $esx -User $root -Password $Passwd
Write-Host -Object "starting ssh services on $esx"
$sshstatus= Get-VMHostService -VMHost $esx| where {$psitem.key -eq "tsm-ssh"}
if ($sshstatus.Running -eq $False) {
Get-VMHostService | where {$psitem.key -eq "tsm-ssh"} | Start-VMHostService }
Write-Host -Object "Executing Command on $esx"
$output = $plink + " " + $plinkoptions + " " + $root + "@" + $esx + " " + $remoteCommand
$output1 = $plink + " " + $plinkoptions + " " + $root + "@" + $esx + " " + $remoteCommand1
}
$message = Invoke-Expression -command $output
$message1 = Invoke-Expression -command $output1
$message
$message1
Output:
========
PowerCLI E:\Plink> .\configchange.ps1
Name Port User
---- ---- ----
10.50.56.55 443 root
starting ssh services on 10.50.56.55
Executing Command on 10.50.56.55
Connecting to 10.50.56.55 port 22
We claim version: SSH-2.0-PuTTY_Release_0.68
Server version: SSH-2.0-OpenSSH_7.1
Using SSH protocol version 2
Doing ECDH key exchange with curve Curve25519 and hash SHA-256
Host key fingerprint is:
ssh-rsa 2048 ab:8b:fe:7b:3f:67:fd:1d:4d:41:d8:72:86:12:77:dd
Initialised AES-256 SDCTR client->server encryption
Initialised HMAC-SHA-256 client->server MAC algorithm
Initialised AES-256 SDCTR server->client encryption
Initialised HMAC-SHA-256 server->client MAC algorithm
Using username "root".
Attempting keyboard-interactive authentication
Access granted
Opening session as main channel
Opened main channel
Started a shell/command
Server sent command exit status 0
Disconnected: All channels closed
Connecting to 10.50.56.55 port 22
We claim version: SSH-2.0-PuTTY_Release_0.68
Server version: SSH-2.0-OpenSSH_7.1
Using SSH protocol version 2
Doing ECDH key exchange with curve Curve25519 and hash SHA-256
Host key fingerprint is:
ssh-rsa 2048 ab:8b:fe:7b:3f:67:fd:1d:4d:41:d8:72:86:12:77:dd
Initialised AES-256 SDCTR client->server encryption
Initialised HMAC-SHA-256 client->server MAC algorithm
Initialised AES-256 SDCTR server->client encryption
Initialised HMAC-SHA-256 server->client MAC algorithm
Using username "root".
Attempting keyboard-interactive authentication
Access granted
Opening session as main channel
Opened main channel
Started a shell/command
Server sent command exit status 0
Disconnected: All channels closed
-
6. Re: Need powerCLi script to execute command on multiple ESXi hosts
FM19999999 Jan 25, 2018 6:48 AM (in response to swamynaveen)I was able to take this script and adapt it to upload and patch vsan hosts. I'll try to put it up to github repo soon.
-
7. Re: Need powerCLi script to execute command on multiple ESXi hosts
FM19999999 Jan 30, 2018 8:06 AM (in response to FM19999999)1 person found this helpfulThis is my version with some additional tweaks.
https://github.com/Coolguydoesnotlookatexplosions/vmware-utils/blob/master/Executeremotecommands.ps1
https://github.com/Coolguydoesnotlookatexplosions/vmware-utils/blob/master/Executeremotecommands.ps1