PSModulePath
在PowerShell的命令行查看环境变量
PS C:\Users\autodump> $env:PSModulePath -split ';'
C:\Users\autodump\Documents\WindowsPowerShell\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
C:\Program Files\VisualSVN Server\PowerShellModules
C:\Program Files (x86)\AutoIt3\AutoItX
PS C:\Users\autodump> $PSVersionTable
Name Value
---- -----
PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.34014
BuildVersion 6.3.9600.17400
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2
Download Script Module and DLL file: SSH-SessionsPSv3.zip
http://www.powershelladmin.com/wiki/SSH_from_PowerShell_using_the_SSH.NET_library
解压缩上述下载的Zip文件,并放到PSModulePath环境变量指示的任意文件夹下,比如我放置的的地方:
C:\Program Files\WindowsPowerShell\Modules\SSH-Sessions
按上述描述放置好后,使用Import-Module加载模块
PS C:\Users\autodump> Import-Module SSH-Sessions
Import-Module : Could not load file or assembly 'file:///C:\Program Files\WindowsPowerShell\Modules\SSH-Sessions\Renci.SshNet.dll' or one of its dependencies. Operation
is not supported. (Exception from HRESULT: 0x80131515)
At line:1 char:1
+ Import-Module SSH-Sessions
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Import-Module], FileLoadException
+ FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand
遇到上述错误,右键点击所有文件,Renci.SshNet.dll,SSH-Sessions.psd1和SSH-Sessions.psm1,查看是否因为安全原因被操作系统block,有的话,全部unblock
然后关闭Windows PowerShell窗口重新打开,再次执行
PS C:\Users\autodump> Import-Module SSH-Sessions
PS C:\Users\autodump>
PS C:\Users\autodump> plink.exe -ssh -l USERNAME -pw PASSWORD HOSTNAME
这里与PowerShell无关,可以使用PowerShell或BAT脚本
使用plink.exe登录Linux并在Linux下执行指令或跑既定的shell脚本
脚本命名为autoobj.bat
rem login linux host
rem see: http://the.earth.li/~sgtatham/putty/0.58/htmldoc/Chapter7.html#plink-usage-batch
rem usage: plink.exe -ssh -l UNSERNAME -pw PASSWORD HOSTNAME(or IP) -m COMMANDS file
plink.exe -ssh -l JIUHZHAN -pw Chifang617 st-lnx-855 -m obj_856_cmd.txt
创建远程命令文件obj_856_cmd.txt,用于登录到Linux主机后代替手动的指令输入
cd /home/jiuhzhan/peopleauto
pwd
ls
./testftp.sh
使用plink.exe执行Linux上指令可能会遇到错误,这种错误是由环境变量不一致引起的,比如PATH的值不全,导致有些指令使用plink.exe将无法执行
可以将PATH的值写在脚本的头部初始化环境变量
和
http://stackoverflow.com/questions/3407386/plink-does-not-source-bashrc-or-bash-profile-on-connect