VMware Cloud Community
IKirill
Enthusiast
Enthusiast
Jump to solution

Change password in linux guest vm

Hi!

I have a script to change pass for user

$VMs_name = Read-Host -Prompt 'enter vm'

$passwordold = Read-Host -Prompt 'password old'

$passwdnew = Read-Host -Prompt 'password new'

$VMs = Get-VM -Server $vserver -Name $VMs_name

foreach ($vm in $VMs)

{

$script = "sudo su -

passwd adm_gz

""$passwdnew""

""$passwdnew""

"

Invoke-VMScript -VM $vm -ScriptType bash -ScriptText $script -GuestUser adm_gz -GuestPassword $passwordold

}

and have output

----------|  bash: -c: line 0: syntax error near unexpected token `;'

|  bash: -c: line 0: `sudo su - ; passwd adm_gz ; "Aa123456" ; "Aa123456" ;  ; '

Aa123456 - is my new password

Whats wrong ? thanks!

0 Kudos
1 Solution

Accepted Solutions
IKirill
Enthusiast
Enthusiast
Jump to solution

I answer my own questions))

there are differences in writing the script field for bash

I you have variable

$script = "echo adm_gz:$passwdnew | chpasswd"

In this case i use variable $passwdnew and working fine

please note, the body of the script in double ". I's very impotant

and dont use new line in body, you keep error

another case

$script = @'

useradd adm_gz -g 0 -ou 0

'@

i use @' '@ in this case and it's work fine too

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

You have twice $passwdnew in the script.

The Invoke-VMScript cmdlet has some issues with multi-line scripts for bash.

Can you try with my Invoke-VMScriptPlus from Invoke-VMScriptPlus V2


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

0 Kudos
IKirill
Enthusiast
Enthusiast
Jump to solution

Thx LucD!

twice $passwdnew because i think in normal cli i must enter password 2 second. Now scipt

$script = @'

sudo su -

echo adm_gz:Aa123456 | sudo chpasswd

'@

Invoke-VMScript -VM $vm -ScriptType bash -ScriptText $script -GuestUser adm_gz -GuestPassword $passwordold

Works fine.  But how can I use a variable instead of Aa123456 ?

some like

echo adm_gz:"$newpass" | sudo chpasswd

echo adm_gz:'$newpass' | sudo chpasswd

not working

0 Kudos
IKirill
Enthusiast
Enthusiast
Jump to solution

I answer my own questions))

there are differences in writing the script field for bash

I you have variable

$script = "echo adm_gz:$passwdnew | chpasswd"

In this case i use variable $passwdnew and working fine

please note, the body of the script in double ". I's very impotant

and dont use new line in body, you keep error

another case

$script = @'

useradd adm_gz -g 0 -ou 0

'@

i use @' '@ in this case and it's work fine too

0 Kudos