How to run the Invoke-AzVMRunCommand from the Azure DevOps Pipeline, when the -ScriptPath is in the Azure Devops Repository.

Aditya Mohapatra 40 Reputation points
2024-07-18T09:30:56.54+00:00

I am trying to run this commad -

 - task: AzurePowerShell@5
   displayName: "Execute Export File in VM to Download Image Version in VHD"
   inputs:
     azureSubscription: 'SBX-kpmgnp'
     ScriptType: 'InlineScript'
     Inline: |      
      $scriptPath = 'C:\Users\adityamohapatra\Downloads\Export-AzureComputeGalleryImageVersion.ps1'    
      $params = @{
        ImageDefinitionName = "VMImageDef"
        ComputeGalleryName = "VMImageGallery"
        ComputeGalleryResourceGroupName = "AzureImagePOC"
        ImageVersionName = "1.0.9"
      }
      Invoke-AzVMRunCommand -ResourceGroupName 'IT' -VMName 'TestVM01' -CommandId 'RunPowerShellScript' -ScriptPath $scriptPath -Parameter $params
     azurePowerShellVersion: 'LatestVersion'

from Azure Devops Pipeline Task but not able to figure out how to configure it. In $scriptPath variable when i am giving the path of the Export file from the Azure Devops Repository, i am getting this error -

##[error]Cannot find drive. A drive with the name ' https' does not exist.

Please help me to resolve this.

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
39,756 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Luis Arias 7,131 Reputation points
    2024-07-18T15:06:49.8066667+00:00

    Hi Aditya Mohapatra,

    I understood that you want to run an script that inside of your repository from the powershell task on Azure Devops pipeline , in that case replace**$(Build.SourcesDirectory)** with the appropriate variable that points to the root of your repository.

    For example lets assume that your script Export-azureComputeGalleryImageVersion.ps1 is on the directory scripts on your respository so you can update like this:

    - task: AzurePowerShell@5
      displayName: "Execute Export File in VM to Download Image Version in VHD"
      inputs:
        azureSubscription: 'SBX-kpmgnp'
        ScriptType: 'InlineScript'
        Inline: |
          $scriptPath = '$(Build.SourcesDirectory)/scripts/Export-AzureComputeGalleryImageVersion.ps1'
          $params = @{
            ImageDefinitionName = "VMImageDef"
            ComputeGalleryName = "VMImageGallery"
            ComputeGalleryResourceGroupName = "AzureImagePOC"
            ImageVersionName = "1.0.9"
          }
          Invoke-AzVMRunCommand -ResourceGroupName 'IT' -VMName 'TestVM01' -CommandId 'RunPowerShellScript' -ScriptPath $scriptPath -Parameter $params
        azurePowerShellVersion: 'LatestVersion'
    

    In Azure DevOps pipelines, you can use predefined variables to reference different directories and other useful information (https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml)

    If the information helped address your question, please Accept the answer.

    Luis

    0 comments No comments

  2. VarunTha 9,345 Reputation points Microsoft Vendor
    2024-07-18T15:57:38.44+00:00

    Hi Aditya Mohapatra,
    Thanks for the question and using MS Q&A platform.

    Azure DevOps is currently not supported in the Microsoft Q&A platform; the supported products are listed over here https://docs.microsoft.com/en-us/answers/products (more to be added later on).

    In order to assist best on your query, I would request you to post your query in SO => Azure Devops dedicated support. Additionally, adding the [Azure] tag on SO will increase visibility as it is a Microsoft Sponsored tag.

    https://stackoverflow.com/questions/tagged/azure-devops

    OR

    Report any Azure Devops problems on Developer Community.

    This will assist you with a faster reply to your query.T

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.