PowerCLI

 View Only
  • 1.  How to use variable for invoke-ssh

    Posted Dec 19, 2022 04:09 PM

    Hi,

    I am trying to update user names on multiple VMs. the below script is not working. how can I use the below script with variable so that I can mention or import through a csv input.

    Please help.

    $serv = "mytestvm"
    $myuser = "johnb"
    $Username = 'root'
    $session = New-SSHSession $serv
    $connected = $session.Connected
    Invoke-SSHCommand -SSHSession $session -Command 'sed -i "s/.*pam.allow.users:.*/&,$($myuser)/" /etc/centrifydc.conf'
    Remove-SSHSession $session -Verbose | Out-Null



  • 2.  RE: How to use variable for invoke-ssh
    Best Answer

    Posted Dec 19, 2022 05:05 PM

    You can use the ExpandString method, see Here strings and the ExpandString method - LucD notes



  • 3.  RE: How to use variable for invoke-ssh

    Posted Dec 19, 2022 06:46 PM

    That worked. Thank you very much LucD

    $serv = "mytestvm"
    $myuser = "johnb"
    $mycommand = @'
    sed -i "s/.*pam.allow.users:.*/&,$myuser/" /etc/centrifydc.conf
    '@
    $Username = 'root'
    $session = New-SSHSession $serv
    $connected = $session.Connected
    $cmdSub = $ExecutionContext.InvokeCommand.ExpandString($mycommand)
    Invoke-SSHCommand -SSHSession $session -Command $cmdSub
    Remove-SSHSession $session -Verbose | Out-Null