It's about open source softwares

Wednesday, May 6, 2015

Execute Bash commands with Powershell on Azure VM



Recently, I had to provision and configure Linux machine through powershell on Azure cloud. We can create the linux machine very easily using powershell script. But i am more interested in configuring linux machine through powershell. For this i have to somehow SSH into linux machine to execute the bash scripts and commands.

There is module based on SSH.NET library for powershell which do SSH in to linux machine and then executes commands through powershell commands.

In this post, i am writing about installation and usage of SSH-Session module of powershell.

Installation



2. Firstly unblock the zip file (go to file properties), then unzip and place unzipped folder in one of PowerShell modules folders. You can find the module folders in powershell using following command,

PS C:\> $env:PSModulePath -split ';'
C:\Users\rahul\Documents\WindowsPowerShell\Modules
C:\Program Files (x86)\WindowsPowerShell\Modules
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement

3. After unzipping the folders in powershell module folder import SSH-Sessions module.
 
PS C:\> Import-Module SSH-Sessions
VERBOSE: Loading module from path
'C:\Users\rahul\Documents\WindowsPowerShell\Modules\SS
VERBOSE: Loading 'Assembly' from path
'C:\Users\rahul\Documents\WindowsPowerShell\Modules\SS
VERBOSE: Loading module from path
'C:\Users\rahul\Documents\WindowsPowerShell\Modules\SS
VERBOSE: Exporting function 'ConvertFrom-SecureToPlain'.
VERBOSE: Exporting function 'New-SshSession'.
VERBOSE: Exporting function 'Invoke-SshCommand'.
VERBOSE: Exporting function 'Enter-SshSession'.
VERBOSE: Exporting function 'Remove-SshSession'.
VERBOSE: Exporting function 'Get-SshSession'.
VERBOSE: Importing function 'ConvertFrom-SecureToPlain'.
VERBOSE: Importing function 'Enter-SshSession'.
VERBOSE: Importing function 'Get-SshSession'.
VERBOSE: Importing function 'Invoke-SshCommand'.
VERBOSE: Importing function 'New-SshSession'.
VERBOSE: Importing function 'Remove-SshSession'.

3. Confirm the installation

PS C:\> Get-Command -Module SSH-Sessions
CommandType     Name                                               ModuleName
-----------               ----                                                    ----------
Function               ConvertFrom-SecureToPlain             SSH-Sessions
Function               Enter-SshSession                               SSH-Sessions
Function               Get-SshSession                                  SSH-Sessions
Function               Invoke-SshCommand                        SSH-Sessions
Function               New-SshSession                                SSH-Sessions
Function               Remove-SshSession                          SSH-Sessions

Usage

  • First we have to setup a ssh session with linux server using New-SshSession command
PS C:\> New-SshSession -ComputerName rk-ubuntu.cloudapp.net -Username rahul -Password "test123"
Successfully connected to rk-ubuntu.cloudapp.net

  • Get the SSH sessions using Get-SshSession command
PS C:\> Get-SshSession
ComputerName                                                                                        Connected
------------                                                                                                  ---------
rk-ubuntu.cloudapp.net                                                                             True
  • Execute the commands using Invoke-SshCommand command. Here i am executing commands to see kernel information and get hostname of linux machine.
PS C:\> Invoke-SshCommand -ComputerName rk-ubuntu.cloudapp.net -Command 'uname -a' -q
Linux rk-ubuntu 2 3.2.0-69-virtual #103-Ubuntu SMP Tue Sep 2 05:21:29 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux 
PS C:\> Invoke-SshCommand -ComputerName rk-ubuntu.cloudapp.net -Command 'hostname' -q
rk-ubuntu

  • Interactive SSH session using Enter-SshSession command
  PS C:> Enter-SshSession -ComputerName rk-ubuntu.cloudapp.net
[rk-ubuntu] rahul # : cat /etc/*-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.2 LTS"
NAME="Ubuntu"
VERSION="14.04.2 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.2 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
[rk-ubuntu] rahul # : exit

  • Remove SSH session using Remove-SshSession command 
PS C:\> Remove-SshSession -ComputerName rk-ubuntu.cloudapp.net 
PS C:\> Get-SshSession
No connections found

Hope this article is helpful for you guys. 
Comments and suggestions are welcome.


Share:

4 comments: