最近我需要执行一些PowerCLI的行动,从ESXi Shell中,你可知道,目前还没有从的cmdlet允许您运行shell命令,但这是流行在社区内的一个选项是使用第三方工具,称为砰砰。exe来运行通过SSH命令。
下载plink.exe通过PowerShell。
我在写脚本的人谁,我不知道已经有安装砰砰,我想他们很少有使用该脚本时没有力气,所以我想分享的第一招是能够检查plink.exe下载它,如果它不是在同一个目录中的脚本,这可以看出如下:
$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("~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: ~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: ~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.