PowerShell and putty – better together

Cover

The Connect-VMHostPutty PowerShell function from my PowerCLi Vi-Module will help you to open multiple PuTTY sessions to ESXi hosts with no password prompt!

  • The function really does not need the VMware PowerCLi to be installed. All it need is PowerShell v3 or above.
$PSVersionTable.PSVersion.Major -ge 3
True

Parameters

  • This is very small and simple function, it has two parameters only and only one of them is mandatory.
Parameter Description
-VMHost ESXi hostname or IP address
-PuttyExec Full path to the putty executable (optional)

Before you start

  • This is multi-step solution, but you do it once and it will work forever!

  • This solution will work if your ESXi hosts have the same root password Emoj only!

Step 1: Take the putty

  • Grab the putty.exe and save it in your PowerShell profile location if you still have not it Emoj.

  • You can open the profile path by executing below command in your PowerShell/PowerCLi console.

explorer (Split-Path $PROFILE)
  • You could prefer a different location. In this case, you will have to use the –PuttyExec parameter every time Emoj.
Connect-VMHostPutty –VMHost esx1.contoso.com –PuttyExec C:\temp\putty.exe

Step 2: Why you needn’t a password

  • The Connect-VMHostPutty function utilizes the small helper function named Read-VMHostCredential, which included in the Vi-Module.
Get-Command –Module Vi-Module
Get-Help Read-VMHostCredential -Full
  • This function reads and decrypts an encrypted file that contains your ESXi hosts root password.

  • We will create this file in the next step.

Step 3:  Secure file

  • To create the encrypted secure file we will utilize my helper script New-SecureCred.ps1.

  • Download it to your favorite scripts’ location, let’s say to C:\scripts and run it in this manner.

cd C:\scripts
.\New-SecureCred.ps1 ‘YourESXiRootPassword’ “$(Split-Path $PROFILE)\esx.cred”
  • Note! Do not change the path or the file name $(Split-Path $PROFILE)\esx.cred. The Read-VMHostCredential will look for it there.

  • To learn more about the New-SecureCred.ps1 script, run the below commands.

cd C:\scripts
Get-Help .\New-SecureCred.ps1 –Full
Get-Help .\New-SecureCred.ps1 –Examples

Step 4: root account

  • If you use in your environment administrative account other than root, edit the $Login variable within the Read-VMHostCredential function and re-import the module with -Force parameter.
$Login = 'devil'
Import-Module Vi-Module -Force

Step 5: Prepare your hosts

  • For all this business to work, you have to enable SSH and disable Lockdown mode for your ESXi hosts.

  • You can use the Enable-VMHostSSH function, to enable SSH on per cluster mode.

Step 6: You are ready!

  • After you have finished the preparations, just run the function.

putty esxlab1
  • Do not be surprised, the putty is the Alias of Connect-VMHostPutty.
Get-Alias –Definition Connect-VMHostPutty
  • If you want to connect to multiple hosts at once (let’s say you have six ESXi hosts named from esxlab1 to esxlab6), run this command.
1..6 | foreach { putty "esxlab$_" }
  • If all done right, the six putty consoles (windows) will be opened and connected to your hosts.

Summary

  • For more details about the function, please take a look at the content based help and examples.
Get-Help Connect-VMHostPutty -Full
Get-Help Connect-ViMVMHostPutty -Examples
Get-Help putty -Parameter VMHost

You may also like:

Set-PowerCLiTitle – Connect-VIServer deep dive
Get-Version – Get any VMware object version
Search-Datastore – Browse/Search VMware Datastores
Get-VMHostFirmwareVersion – Get ESXi servers BIOS/Firmware version

3 thoughts on “PowerShell and putty – better together

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s