Task to start SSH using PowerCLI?

I’ve come up with a nice use-case for a task to run before making a terminal connection, but I can’t figure out how to configure this–if it’s even possible.

For any number of reasons, leaving SSH turned off on an ESXi host is a good idea, but there are times when you need to get on the host console to perform maintenance, like installing/uninstalling a VIB.

With a little PowerCLI, it’s pretty trivial to connect and turn on the SSH daemon, and I can do that before attempting to connect with Royal Terminal. But because Terminal connections allow for a “before connect” and “after connect” tasks, I thought it would be clever to do these things as part of the connection.

I can have the PowerCLI script in a .ps1 file, but it would be so much cleaner (not to mention portable!) if I could save it in a Command Task item. Being able to pass in the host FQDN, username, and password from the terminal config would be nice as well.

It’s unclear from documentation how one might go about doing this. For reference, here’s the code for enabling the daemon that I want to execute:

$pwd = ConvertTo-SecureString '$PASSWORD$' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential('$USERNAME$', $pwd)
Connect-VIServer -server $HOST$ -Credential $cred | Out-Null
get-VMHostService -VMHost $HOST$ | ?{$_.Label -eq 'SSH'} | Start-VMHostService | Out-Null

I don’t like my solution, but I figured out an okay way to get what I’m after.

First, I turned the script into a one-liner. Just a bunch of semicolons where there otherwise would be newline.

Next, I added credentials into the Command Task; it wouldn’t use the connection’s credentials, and I couldn’t figure out how to make the task refer to credentials in a “calling party”.

But it works!

Hi Jim,

Yes, this is currently the only way to “embed” the script in a Command Task. Royal Connect will provide better options for this kind of use case.

You can use replacement tokens to pass the resolved credentials from the connection:

  • $EffectiveUsername$
  • $EffectivePassword$

Configure the task to use the credentials from the context connection so these tokens resolve accordingly. See the replacement-token documentation.

Regards,
Stefan