How Can We Help?
Use Posh-SSH instead of PuTTY
During the pre-Posh-SSH module times we often used the PuTTY Suite to run commands on our ESXi nodes.
With the arrival of the Posh-SSH module, there is no more need to use any of the PuTTY EXE.
Running a command on an ESXi node, provided SSH is running, is now as simple as:
1 2 3 4 5 |
$session = New-SSHSession -ComputerName $esx.Name -Credential $cred –AcceptKey $result = Invoke-SSHCommand -SSHSession $session -Command $cmdSub Remove-SSHSession -SSHSession $session | Out-Null |
Jon Adams
Hello. I am trying to SSH into, and then edit two files and restart a service in hundreds our ESXI hosts. Do you have any examples of doing that (with POSH-SSH, of course)?
LucD
Once you have established a session, you can send commands (just as you would type them at the console prompt).
For example, to restart the vCenter Agent, you could do
SSHCommand -Command "/etc/init.d/vpxa restart" -Session $ssh
To change files, I normally use the sed (streaming editor) command.
It depends a bit on what exactly you want to do
# Add a line (to the /etc/pam.d/password file)
SSHCommand -Command "sed '$ a Your new line' /etc/pam.d/password" -Session $ssh
# Replace a line
SSHCommand -Command "sed -i 's/wordtofind/replacewithword/g' /path/to/file.txt" -Session $ssh
# Replace a string in a line
SSHCommand -Command "sed -e 's/oldtext/newtext/g' /etc/vmware/esx.conf.old > /etc/vmware/esx.conf" -Session $ssh
A good document to learn more about the sed command is sed, a stream editor, which contains many examples
vinoop
I found this very late.. I was using plink and pscp for lot of scripts..
Abraham Yunes
This is pretty sweet, I didnt know this was a thing. I can finally get rid of this hacky plink calls.