Forum Discussion

sag007's avatar
sag007
Copper Contributor
May 06, 2022

Execute script on Azure Windows VM via Azure pipeline

I am using third party software for master data management. This software does not have API's to perform deployment across environment. It has command line utility which needs to be installed on machine and call the commands to perform the installation. I am looking to automate this solution using Azure Devops pipeline. What I am trying to do is as below

  1. Create windows VM in Azure and install command line utility on it.
  2. Store script in some folder in the VM.
  3. Use Azure pipeline to call this script which is stored in my VM.

I don't even know if it possible to do such things. I tried looking on internet on how to call script stored in VM via Azure pipeline but didn't find any useful link. If any one has done such activity or have an idea how can it be achieved please help.

Regards S

  • pazdedav's avatar
    pazdedav
    Steel Contributor

    Hi sag007,

     

    There are several ways you could accomplish what you want:

     

    1. Custom script VM extension - you could deploy a custom script VM extension as part of your VM provisioning (assuming you are using CLI, PowerShell, ARM/Bicep template or similar IaC tool for infra provisioning). You would add such an extension as a 'child resource' to your VM declaration. When the VM is fully up and running, this extension could trigger a PSH script to download and install your tool. Azure Custom Script Extension for Windows - Azure Virtual Machines | Microsoft Docs

    2. State configuration - a feature of Azure Automation that is using PowerShell DSC to declare the 'desired state' of your VM. This indeed could be used as well, but it requires a bit of extra knowledge and steps to set it up, if you are not familiar with DSC. Azure Automation State Configuration overview | Microsoft Docs

    3. Azure VM Run command - there is also a way to trigger an imperative command from "anywhere" (through the Azure Resource Manager API / control plane) without a need to either expose the VM to the Internet using a public IP or requiring a private connectivity for e.g., PowerShell Remoting. You could run this command from your pipeline agent. Run scripts in a Windows VM in Azure using action Run Commands - Azure Virtual Machines | Microsoft Docs

     

    Depending where you could store the binary file (the tool) and the script, you might need to solve the topic of getting those files to the VM, e.g. by using a custom VM image and "bake the files" into the image, or storing the files in Azure Blob.

     

    Hope it helps.

  • nguyenphupn's avatar
    nguyenphupn
    Copper Contributor

    Hi sag007 , 

    You can use AzureCLI in the pipeline to run the command:

    taskAzureCLI@2
      inputs:
        azureSubscription'YourSubscription'
        scriptType'pscore'
        scriptLocation'inlineScript'
        inlineScript: |
          az vm run-command invoke --command-id RunPowerShellScript --name your-vm-name -g resource-group-name --scripts C:\Script\yourscript.ps1
     
    "C:\Script\yourscript.ps1" is the path of your script in the VM, if you want to run others command, you only need to change it. Example: "--scripts Get-Process".
     
    To verify executed script. Go to VM and check in this path: C:\Packages\Plugins\Microsoft.CPlat.Core.RunCommandWindows\1.1.15\Downloads

     

Resources