VMware Cloud Community
rxjoseph
Enthusiast
Enthusiast
Jump to solution

Powercli script to start SSHD on Centos

Hi LucD

I have a script to create VMs from OVA its a zscaler VM which is centos based

The script works ok and builds the VM 

Now I need to start sshd on each vm and i need to logon to each VM and start sshd, since there are more than 1000 its not practicle

How can i use a powercli command to execute the following command to start sshd please

sudo systemctl start sshd

 

#############################
### Create New Zscaler VM ###
#############################

$ovaConfnew.NetworkMapping.VM_Network.Value =$vmdetail.PortGroup_VLAN

Import-VApp -Source $ovapathnew -OvfConfiguration $ovaConfnew -Name $vmdetail.AppName ` -Location $vmdetail.TGCluster ` -VMHost $destination -Datastore $destinationDatastore -Confirm:$false

get-vm -Name $VMdetail.AppName | Set-Vm -numcpu 4 -Confirm:$false

 

 

#############################
### Power on Zscaler VM ###
#############################

Foreach ($VMdetail in $VMdetails ) {

Get-vm -name $VMdetail.AppName |Start-VM -Confirm:$false

}

Please advise 

 

Many thanks

 

RXJ

 

Reply
0 Kudos
2 Solutions

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That seems to indicate the 'sudo' wants to prompt you for a password, but there is no tty, since you are calling this via Invoke-VMScript.

You might want to give my Invoke-VMScriptPlus function with the Sudo switch a go.
That Sudo switch prefixes all sudo commands it finds in ScriptText with an inline password.

You could do that yourself with Invoke-VMScript itself, but that would mean adding the password in clear text to the ScriptText value.


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

View solution in original post

Reply
0 Kudos
rxjoseph
Enthusiast
Enthusiast
Jump to solution

Hi LucD

Thanks for your help

After some research i found the following and it worked 

I was able to set the sshd on all 1000 vms 

#############################
### Enable sshd on all Zscaler VMs ###
############################

Foreach ($VMdetail in $VMdetails) {


invoke-vmscript -VM $VMdetail.AppName -ScriptText "echo <password>| sudo -S -k systemctl start sshd" -GuestUser admin -GuestPassword ,password. -scripttype Bash

}

 

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

If the VMware Tools are installed, you could use Invoke-VMScript.


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

Reply
0 Kudos
rxjoseph
Enthusiast
Enthusiast
Jump to solution

Hi LucD

I get an error  and not sure if you could help me

 

$Credential=Get-Credential

$vmd = Get-Content -Path 'D:\Temp\RXJ\Zscaler_OVA\zscaler.txt'

$myvmds= get-vm -Name $vmd

foreach ($myvmd in $myvmds) {

invoke-vmscript -VM $myvmd -ScriptText "sudo systemctl start sshd" -GuestCredential $credential

}

 

ScriptOutput
-------------------------------------------------------------------------------
----------------------------------------| We trust you have received the usual
lecture from the local System
| Administrator. It usually boils down to these three things:
|
| #1) Respect the privacy of others.
| #2) Think before you type.
| #3) With great power comes great responsibility.
|
| sudo: no tty present and no askpass program specified
|
-------------------------------------------------------------------------------
----------------------------------------

Many thanks

 

Rxj

 

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That seems to indicate the 'sudo' wants to prompt you for a password, but there is no tty, since you are calling this via Invoke-VMScript.

You might want to give my Invoke-VMScriptPlus function with the Sudo switch a go.
That Sudo switch prefixes all sudo commands it finds in ScriptText with an inline password.

You could do that yourself with Invoke-VMScript itself, but that would mean adding the password in clear text to the ScriptText value.


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

Reply
0 Kudos
rxjoseph
Enthusiast
Enthusiast
Jump to solution

Hi LucD

Thanks for your help

After some research i found the following and it worked 

I was able to set the sshd on all 1000 vms 

#############################
### Enable sshd on all Zscaler VMs ###
############################

Foreach ($VMdetail in $VMdetails) {


invoke-vmscript -VM $VMdetail.AppName -ScriptText "echo <password>| sudo -S -k systemctl start sshd" -GuestUser admin -GuestPassword ,password. -scripttype Bash

}

 

Reply
0 Kudos