{"id":1415,"date":"2016-11-14T09:08:02","date_gmt":"2016-11-14T07:08:02","guid":{"rendered":"http:\/\/hen.co.za\/blog\/?p=1415"},"modified":"2016-11-14T18:56:11","modified_gmt":"2016-11-14T16:56:11","slug":"ssh-from-powershell","status":"publish","type":"post","link":"https:\/\/hen.co.za\/blog\/2016\/11\/ssh-from-powershell\/","title":{"rendered":"SSH from PowerShell"},"content":{"rendered":"<p>I&#8217;ve been playing with more non Microsoft technologies recently and even have a few Linux VMs running here and there. In the process I&#8217;ve learned a lot about interacting between the two worlds\/environments using technologies like SSH and even running the Powershell Alpha previews on my Linux VMs. Then I got &#8216;bored&#8217; and looked for a way to programmatically interact with my VMs (for monitoring) and discovered the open source project <a href=\"http:\/\/sshnet.codeplex.com\/\">SSH.Net<\/a> which I now have incorporated into even <a href=\"https:\/\/quickmon.codeplex.com\/\">QuickMon<\/a>. Then I got even more bored and started looking for a way to use SSH from Powershell and came across the <a href=\"http:\/\/www.powershelladmin.com\/wiki\/SSH_from_PowerShell_using_the_SSH.NET_library\">SSH-Sessions<\/a> module which incidentally also use SSH.Net to give Powershell a way to call\/connect using SSH.<\/p>\n<p><strong>No PassPhrase<\/strong><\/p>\n<p>This all works nicely except&#8230; SSH-Sessions had one shortcoming.. which I just had to fix. The creator(s) of this module implements a way to specify a key file when connecting to an SSH server but does not provide a way to specify the &#8216;PassPhrase&#8217; at all! WHY?<\/p>\n<p>Any way, I knew the SSH.Net library does support it since I actually make use of this functionality in QuickMon. Then I started digging inside the SSH-Session script files and made a few adjustments&#8230;<\/p>\n<p><strong>Fix<\/strong><\/p>\n<p>To enable PassPhrase functionality I simply had to add the following code in the &#8216;New-SshSession&#8217; function:<\/p>\n<p>Change the function header to:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nfunction New-SshSession {\r\n    param(&#x5B;Parameter(Mandatory=$true)]&#x5B;string&#x5B;]] $ComputerName,\r\n          &#x5B;Parameter(Mandatory=$true)]&#x5B;string]   $Username,\r\n          &#x5B;string] $KeyFile = '',\r\n          &#x5B;string] $PassPhrase = 'blankPassphrase',\r\n          &#x5B;string] $Password = 'SvendsenTechDefault', # I guess allowing for a blank password is &quot;wise&quot;...          \r\n          &#x5B;int] $Port = 22,\r\n          &#x5B;switch] $Quiet\r\n    )\r\n<\/pre>\n<p>and then inside the function:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nif ($KeyFile -ne '') {\r\n        if (-not $Quiet) {\r\n            &quot;Key file specified. Will override password. Trying to read key file...&quot;\r\n        }\r\n        if ($PassPhrase -eq 'blankPassphrase') {\r\n            $SecurePassPhrase = Read-Host -AsSecureString &quot;key provided. Please enter pass phrase for $KeyFile&quot;\r\n            $PassPhrase = ConvertFrom-SecureToPlain $SecurePassPhrase\r\n        }\r\n        if (Test-Path -PathType Leaf -Path $Keyfile) {\r\n            $Key = New-Object Renci.SshNet.PrivateKeyFile( $Keyfile, $PassPhrase ) -ErrorAction Stop\r\n        }\r\n        else {\r\n            &quot;Specified keyfile does not exist: '$KeyFile'.&quot;\r\n            return\r\n        }   \r\n    }\r\n<\/pre>\n<p><strong>Example<\/strong><\/p>\n<p>And with the you can now use the module to connect to SSH using a Key file that has a PassPhrase. If you don&#8217;t specify the PassPhrase the script will prompt you for one. If you actually don&#8217;t have a PassPhrase at all then pass an empty string as the value of PassPhrase (not tested but it should work).<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nImport-Module SSH-Sessions\r\nNew-SshSession -Computer mySSHServer -Username me  -KeyFile 'c:\\mykeys\\mykey.key' -PassPhrase 'somePhrase'\r\nInvoke-SshCommand  -ComputerName mySSHServer -Command &quot;cat \/proc\/cpuinfo | grep processor&quot; -Quiet\r\nRemove-SshSession -RemoveAll -Quiet\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been playing with more non Microsoft technologies recently and even have a few Linux VMs running here and there. In the process I&#8217;ve learned a lot about interacting between the two worlds\/environments using technologies like SSH and even running &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/hen.co.za\/blog\/2016\/11\/ssh-from-powershell\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[267,16],"tags":[240,311],"class_list":["post-1415","post","type-post","status-publish","format-standard","hentry","category-administration","category-reference","tag-powershell","tag-ssh"],"_links":{"self":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/1415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/comments?post=1415"}],"version-history":[{"count":8,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/1415\/revisions"}],"predecessor-version":[{"id":1423,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/1415\/revisions\/1423"}],"wp:attachment":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/media?parent=1415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/categories?post=1415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/tags?post=1415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}