question

RoryFeng-2354 avatar image
0 Votes"
RoryFeng-2354 asked RoryFeng-2354 commented

how to start Tomcat process and postgre SQL process with runbook powershell workflow in Linux of azure VM

Hello

Someone knows how to start Tomcat process and postgre SQL process with azure automation runbook(powershell flow)in the system of Linux of VM.

I am making a powershell work flow runbook to start postgre DB process and Tomcat process in Linux system of VM of azure.

There are some templates just start VMs.
Could someone give me a template or tell me how to run Linux commands like「service Tomcat start」in runbooks

Need API or else ?

Best Regard

azure-automation
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

tbgangav-MSFT avatar image
0 Votes"
tbgangav-MSFT answered RoryFeng-2354 commented

Hi @RoryFeng-2354,

To remotely run linux command like "service tomcat start" in VM via Azure Automation runbook, you may follow one of the below 2 approaches.

  1. Create an Azure storage account, container and upload a blob i.e., script (say tomcat_service_start.sh file with the content "service tomcat start") and then have below code in Azure Automation PowerShell runbook.

    $connectionName = "AzureRunAsConnection"
    $servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
    $ConnectToAzAccount = Add-AzAccount -ServicePrincipal -TenantId $servicePrincipalConnection.TenantId -ApplicationId $servicePrincipalConnection.ApplicationId -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint

    $StorageAccountName = "xxxxxxxxxxxxx"
    $StorageAccountKey = "xxxxxxxxxxxxxx=="
    $ContainerName = "xxxxxxxxxxxxxxx"
    $BlobName_Linux = "tomcat_service_start.sh"
    $RG_VM = "xxxxxxxxxxxxxxxxxx"
    $VM_Name_Linux = "xxxxxxxxx"
    $InvokeCmd_Id_Linux = "RunShellScript"

    $AzStorage = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
    $AzStorageContext = $AzStorage.Context

    $GetBlobContent_Linux = Get-AzStorageBlobContent -Container $ContainerName -Blob $BlobName_Linux -Destination ($Env:temp+"/tomcat_service_start.sh") -Context $AzStorageContext -Force
    $InvokeRunCmdOutput_Linux = Invoke-AzVMRunCommand -ResourceGroupName $RG_VM -VMName $VM_Name_Linux -CommandId $InvokeCmd_Id_Linux -ScriptPath ($Env:temp+"/tomcat_service_start.sh")
    $TomcatServiceStart_Output_Linux = $InvokeRunCmdOutput_Linux.Value[0].Message
    Write-Output $TomcatServiceStart_Output_Linux

  2. Save a script (say \home\xxxx\tomcat_service_start.sh) in your VM and have the linux command "service tomcat start" as content in that file and then have below code in Azure Automation PowerShell runbook.

    $ServicePrincipalConnection = Get-AutomationConnection -Name 'AzureRunAsConnection'
    Add-AzAccount -ServicePrincipal -TenantId $ServicePrincipalConnection.TenantId -ApplicationId $ServicePrincipalConnection.ApplicationId -CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint
    $rgname ="rrrrrrrrrrrrrr"
    $vmname ="vvvvvvvvvvvvvv"
    $ScriptToRun = "\home\xxxx\tomcat_service_start.sh"
    Out-File -InputObject $ScriptToRun -FilePath ScriptToRun.sh
    Invoke-AzVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunShellScript' -ScriptPath ScriptToRun.sh
    Remove-Item -Path ScriptToRun.sh

Note:

  • Before trying any of the above approaches, make sure you import required cmdlets modules in your Azure Automation Account.

  • Above approaches are with PowerShell runbook so you may have to tweak them to re-use in PowerShell workflow runbook.

  • This / this questions are the source of the above information.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

thanks so much.I will try it.

Best wishes

0 Votes 0 ·

Hello tbgangav-MSFT

I put the powershell runbook into powershell workflow runbook the first one.

but when I run it error happended.

System.Management.Automation.ParameterBindingException: Cannot bind parameter 'Context'. Cannot convert the "Microsoft.WindowsAzure.Commands.Storage.AzureStorageContext" value of type "System.String" to type "Microsoft.Azure.Commands.Common.Authentication.Abstractions.IStorageContext".

Same command cannot run in powershell workflow runbook.

I would appreciate it if you could tell me.

Best wishes!

0 Votes 0 ·