PowerCLI

 View Only
  • 1.  Change password in linux guest vm

    Posted Sep 19, 2018 11:34 AM

    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!



  • 2.  RE: Change password in linux guest vm

    Posted Sep 19, 2018 11:45 AM

    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



  • 3.  RE: Change password in linux guest vm

    Posted Sep 19, 2018 01:50 PM

    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



  • 4.  RE: Change password in linux guest vm
    Best Answer

    Posted Sep 20, 2018 08:41 AM

    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