Ress
Ress

Reputation: 77

Azure - Connect to a remote Linux VM via Azure PowerShell runbooks and input commands

I'm trying to find a way to ssh into a Linux VM programmatically via our Azure Automation Account (PowerShell runbook) and input commands. Is this possible? I'm only finding info on how to run other PowerShell commands on remote VMs, but in this case it has to be Linux commands.
Thanks!

Upvotes: 0

Views: 381

Answers (1)

Jim Xu
Jim Xu

Reputation: 23111

If you want to run the shell script on Azure linux VM, you can use the feature Custom Script extension. But it has a limit. If you want to use the way , you need to store your command in a public location (such as Azure blob) then provide the location. For more details, please refer to here.

For example

$context=New-AzStorageContext -StorageAccountName "andyprivate" -StorageAccountKey ""


$container=Get-AzStorageContainer -Name "input" -Context $context

$stream=$container.CloudBlobContainer.GetBlockBlobReference("my.sh").OpenWrite()
$content = [system.Text.Encoding]::UTF8.GetBytes('apt-get -y update && apt-get install -y apache2')
$stream.write($content,0,$content.Length)
$stream.Flush()
$stream.close()


$sas=New-AzStorageBlobSASToken -Container "input" -Blob "my.sh"  -Permission r -Context $context
$url=$container.CloudBlobContainer.GetBlockBlobReference("my.sh").StorageUri.PrimaryUri.ToString() + $sas


$protectedSettings = @{"fileUris" = @($url); "commandToExecute" = "sh my.sh"};
Set-AzVMExtension  -ResourceGroupName 'testdata' -Name 'test' -VMName "testdocker"  -Location "eastasia" `
   -Publisher "Microsoft.Azure.Extensions" -ExtensionType "CustomScript" -ProtectedSettings $protectedSettings -TypeHandlerVersion 2.1

enter image description here

Upvotes: 1

Related Questions