It's a bit more complicated than the simple example I gave.
With a 'regular' SSH session there is no TTY connected to your session, hence the problem of entering the 'shell' command.
You can force a TTY by opening a shell stream.
$vcsa = 'vcsa.domain'$user = 'root'
$pswd = 'VMware1!'
$secPswd = ConvertTo-SecureString $pswd -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($user, $secPswd)
$newUser = 'lucd'
$newPswd = 'VMware1!'
$newFirst = 'Luc'
$newLast = 'D'
$createUser = @'
/usr/lib/vmware-vmafd/bin/dir-cli user create --account $newUser --first-name $newFirst --last-name $newLast --user-password '$newPswd' --password '$pswd'
'@
$createUser = $ExecutionContext.InvokeCommand.ExpandString($createUser)
$session = New-SSHSession -ComputerName $vcsa -Credential $cred –AcceptKey
$stream = New-SSHShellStream -SSHSession $session -TerminalName tty
$stream.WriteLine('shell')
while ($stream.Length -ne 0)
{
$stream.Read()
}
$stream.WriteLine($createUser)
while ($stream.Read() -notmatch 'created successfully')
{
sleep 2
}
while ($stream.Length -ne 0)
{
$stream.Read()
}
$stream.Close()
Remove-SSHSession -SSHSession $session | Out-Null
---------------------------------------------------------------------------------------------------------
Was it helpful? Let us know by completing this short survey here.