在編寫Powershell腳本時,有時會須要和Linux機器進行交互。因此這時就須要在Powershell中使用SSH。shell
本文將介紹如何在Powershell中安裝SSH模塊,以及如何使用ssh命令。windows
一 Posh-SSH模塊session
1 安裝ssh
在Windows中點擊搜索,powershell_iseide
在PowerShell_ise的環境中運行測試
PS C:\windows\system32> find-module *ssh* Version Name Repository Description ------- ---- ---------- ----------- 2.0.2 Posh-SSH PSGallery Provide SSH and SCP functionality for executing commands against remote hosts. 1.0.0 SSH PSGallery Provides a PowerShell-based SSH client based on SSH.net http://sshnet.codeplex.com/ 1.9 SSHSessions PSGallery Svendsen Tech's SSH-Sessions module provides SSH session creation, management and int... 1.1.3 PowerSSH PSGallery This module detects the first use of an SSH command, automatically runs the SSH agent... 1.0 cEPRSSharepoint PSGallery DSCModule helps in installing & configuring the sharepoint site, Farm etc., 1.0.4 PSShortcut PSGallery This module eases working with Windows shortcuts (LNK and URL) files.
若是沒有find-module的命令,須要安裝PackageManagement:spa
https://www.microsoft.com/en-us/download/details.aspx?id=51451.net
本文采用Posh-SSH的模塊:code
Install-Module Posh-SSH
能夠經過下面的命令,查看安裝的模塊包含什麼命令:blog
PS C:\Users\hengwei> get-command -Module posh-ssh CommandType Name ModuleName ----------- ---- ---------- Function Get-PoshSSHModVersion posh-ssh Function Get-SFTPChildItem posh-ssh ......
2 在PowerShell中使用SSH
能夠用下面的腳本進行測試:
$username = "hengwei" $password = "xxxx" $secure = $password | ConvertTo-SecureString -AsPlainText -Force $cred = New-Object System.Management.Automation.PSCredential($username,$secure) New-SSHSession -ComputerName 40.125.173.212 -Credential $cred -AcceptKey Invoke-SSHCommand -SessionId 0 -Command "cat /etc/redhat-release" Invoke-SSHCommand -SessionId 0 -Command "df -k"
Host : 40.125.173.212 Output : {CentOS Linux release 7.3.1611 (Core) } ExitStatus : 0 Host : 40.125.173.212 Output : {Filesystem 1K-blocks Used Available Use% Mounted on, /dev/sda2 30929148 1200340 29728808 4% /, devtmpfs 847688 0 847688 0% /dev, tmp fs 858264 0 858264 0% /dev/shm...} ExitStatus : 0
二 SSHSessions模塊
1 安裝
和Posh-SSH相似,安裝採用install-module的方式:
Install-module SSHSessions
其命令比Posh-SSH要少,但都是SSH相關的:
PS C:\Users\hengwei> get-command -Module sshsessions CommandType Name ModuleName ----------- ---- ---------- Function ConvertFrom-SecureToPlain SSHSessions Function Enter-SshSession SSHSessions Function Get-SshSession SSHSessions Function Invoke-SshCommand SSHSessions Function New-SshSession SSHSessions Function Remove-SshSession SSHSessions
2 在PowerShell中使用SSH
New-SshSession -ComputerName 40.125.173.212 -Username hengwei -Password xxxx PS C:\Users\hengwei> Get-SshSession ComputerName Connected ------------ --------- 40.125.173.212
SSHSessions中有一個enter-sshsession的命令,能夠直接進入交互式模式,進行一些交互的操做:
PS C:\Users\hengwei> Enter-SshSession -ComputerName 40.125.173.212 [40.125.173.212]: /home/hengwei # : pwd /home/hengwei [40.125.173.212]: /home/hengwei # : mkdir test [40.125.173.212]: /home/hengwei # : ls test [40.125.173.212]: /home/hengwei # : ls -l total 0 drwxrwxr-x. 2 hengwei hengwei 6 Nov 20 07:29 test [40.125.173.212]: /home/hengwei # :
也能夠使用invoke-sshcommand的模式實現命令
PS C:\Users\hengwei> Invoke-SshCommand -ComputerName 40.125.173.212 -Command "sudo cat /etc/fstab" [40.125.173.212] # UUID=b7559ac5-b3a4-4b00-b98a-a2a2611806d0 / xfs defaults 0 0 UUID=b98659b2-5f8c-493e-9304-658905ef1391 /boot xfs defaults 0 0
你們還能夠安裝一下其餘的ssh模塊,實如今Powershell中的ssh功能。
三 刪除模塊
以Posh-SSH模塊爲例,刪除命令:
PS C:\windows\system32> remove-module -name posh-ssh -Force -Verbose -Debug VERBOSE: Removing the imported "Get-PoshSSHModVersion" function. VERBOSE: Removing the imported "Get-SFTPChildItem" function. VERBOSE: Removing the imported "Get-SFTPContent" function. ......
同時,刪除Posh-SSH的目錄:
C:\Program Files\WindowsPowerShell\Modules\Posh-SSH
這樣就完全刪除了Posh-SSH模塊。