
Recently I needed to perform some actions in PowerCLI from the ESXi Shell, as you may know there are currently no cmdlets from VMware to allow you to run shell commands but one option which is popular within the communities is using a 3rd party tool called plink.exe to run the commands via SSH.
Examples of this can be seen here and here.
Download plink.exe via PowerShell
The script I was writing was for someone who I knew didn’t already have plink installed and I wanted them to have little to no effort when using this script so the first trick I wanted to share was the ability to check for plink.exe and download it if it wasn’t in the same directory as the script, this can be seen below:
$myDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$PlinkLocation = $myDir + "\Plink.exe"
If (-not (Test-Path $PlinkLocation)){
Write-Host "Plink.exe not found, trying to download..."
$WC = new-object net.webclient
$WC.DownloadFile("http://the.earth.li/~sgtatham/putty/latest/x86/plink.exe",$PlinkLocation)
If (-not (Test-Path $PlinkLocation)){
Write-Host "Unable to download plink.exe, please download from the following URL and add it to the same folder as this script: http://the.earth.li/~sgtatham/putty/latest/x86/plink.exe"
Exit
} Else {
$PlinkEXE = Get-ChildItem $PlinkLocation
If ($PlinkEXE.Length -gt 0) {
Write-Host "Plink.exe downloaded, continuing script"
} Else {
Write-Host "Unable to download plink.exe, please download from the following URL and add it to the same folder as this script: http://the.earth.li/~sgtatham/putty/latest/x86/plink.exe"
Exit
}
}
}
Accept plink.exe host key automatically
Secondly when connecting to a host for the first time you will need to accept the host key and allow plink.exe to connect to the host, the message is similar to the one below:

At the moment there is no option in plink.exe to skip the host key checking, however after some searching and messing around I came upon a solution.
It turns out you can actually send a Y to the script you are running by simply piping “echo Y” to plink.exe, this sends a Y accepting the host key when the question is asked from the command line, this can be scripted like the below example:
Echo Y | Plink.exe…..
Hopefully someone will find these useful in the future as I did with this script.