Home > database >  Excute Shell Script remotely to Azure Linux VM
Excute Shell Script remotely to Azure Linux VM

Time:08-09

i'am trying to excute a shell script to an azure linux virtual machine using powershell

Why I'm i using powershell? :

The virtual machine has a Copied VHD from storage account which means it doesn't have Azure VM agent

so i can't use :

  • azure vm extentions
  • azure vm Runs

i tried also using an automation runbook with the ssh module and got those errors :

Exception calling "Connect" with "0" argument(s): "Server HMAC algorithm not found" At C:\Modules\User\SSH\SSH.psm1:68 char:5   $SSHConnection.Connect()   ~~~~~~~~~~~~~~~~~~~~~~~~   CategoryInfo : NotSpecified: (:) [], MethodInvocationException   FullyQualifiedErrorId : SshConnectionException

and

Exception calling "RunCommand" with "1" argument(s): "Client not connected." At C:\Modules\User\SSH\SSH.psm1:69 char:5   $ResultObject = $SSHConnection.RunCommand($ScriptBlock.ToString() ...   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   CategoryInfo : NotSpecified: (:) [], MethodInvocationException   FullyQualifiedErrorId : SshConnectionException

which is based on my understanding caused by the vm that is missing KexAlgorithms to describe which methods are supported by the SSH daemon

What i'm trying to do now is to ssh into the vm and excute the command using powershell

here is what i got now (a Powershell scripot to ssh into the vm and excute a command):

$Password = "pwd"
$User = "pwd"
$ComputerName = "ip adress"
$Command = "touch tst.txt"

$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential($User, $secpasswd)

echo 'logging...'
$SessionID = New-SSHSession -ComputerName $ComputerName -Credential $Credentials #Connect Over SSH
echo 'Executing...'
$output = (Invoke-SSHCommand -Index $SessionID -Command $Command).Output
Remove-SSHSession -Name $SessionID | Out-Null

im getting this error :

Invoke-SshCommand : A parameter cannot be found that matches parameter name 'Index'.
At C:\Users\octoadmin\Desktop\sign in.ps1:11 char:30
  $output = (Invoke-SSHCommand -Index $SessionID -Command $Command).Out ...
                               ~~~~~~
      CategoryInfo          : InvalidArgument: (:) [Invoke-SshCommand], ParameterBindingException
      FullyQualifiedErrorId : NamedParameterNotFound,Invoke-SshCommand

i looked in the internet but couldn't find anything. apparently the ssh invoke command can't find a session with the index $SessionID but i don't know where exactly is the problem

hope someone can guide me to the right direction.

CodePudding user response:

Updating Posh-SSH worked for me with this code :

to install Posh-SSH :

Install-Module -Name Posh-SSH -RequiredVersion 2.1

The Script:

$Command = "fetch $scripturl; sh script.sh"
$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential($User, $secpasswd)
$ComputerName = Get-AzPublicIpAddress -ResourceGroupName $RG -Name $IPName | Select-Object -ExpandProperty ipAddress
echo 'ip is : '
echo $ComputerName
echo 'logging...'
$SessionID = New-SSHSession -ComputerName $ComputerName -AcceptKey -Credential $Credentials 
echo 'Exucuting...'
$Query = (Invoke-SshCommand -SSHSession $SessionID  -Command $Command).Output
echo $Query
Remove-SSHSession -Name $SessionID | Out-Null
  • Related