AzurePowerShell@5 in an Azure Pipeline yaml.

awrb194 0 Reputation points
2024-01-15T21:21:29.73+00:00

Hi, I have 2 AzurePowerShell@5 tasks that both need to run some queries against a CosmosDB Database. Issue 1: If I load in the module CosmosDB and run it, all is fine in the first AzurePowerShell@5 task. When I try and run a query in the second AzurePowerShell@5 it falls over, because it's already loaded. If I remove the install (which has the -force option added) it fails, because it's not already loaded. Issue 2: If I load in the first AzurePowerShell@5, and then uninstall at the end of it, it goes through the motions. If I check for CosmosDB in the second AzurePowerShell@5 task, it still finds it so I try and uninstall it. It then fails: "No modules were removed. Verify that the specification of modules to remove is correct and those modules exist in the runspace" This is how it's set up:

- task: AzurePowerShell@5
      displayName: Shell1
      inputs:
        azureSubscription: '$(subscription)'
        pwsh: true
        ScriptType: 'InlineScript'
        azurePowerShellVersion: 'LatestVersion'
        Inline: |
          Install-Module -Name "CosmosDB" -Force
          Import-Module -Name "CosmosDB"

        <more lines>

		if( Get-Module -Name "CosmosDB" -ListAvailable )
        {
          Write-Output "Finish task, remove CosmosDB module"
          Remove-Module -Name "CosmosDB" -Force -Verbose
        }

- task: AzurePowerShell@5
      displayName: Shell2
      inputs:
        FailOnStandardError: true
        azureSubscription: '$(subscription)'
        azurePowerShellVersion: 'LatestVersion'
        pwsh: true
        ScriptType: 'InlineScript'
        Inline: |
          if( Get-Module -Name "CosmosDB" -ListAvailable )
          {
			Remove-Module -Name "CosmosDB" -Force -Verbose
          }
          Install-Module -Name "CosmosDB" -Force
          Import-Module -Name "CosmosDB"

		  <some lines>


The remove in the first task is called and with 'verbose' on I can see it removing the libs. I've tried the Install in multiples, in a single pre-call inline script and with removes. I've tried the Install and Remove commands with and without quotes for the name. No difference, it must be obvious, but I can't see what I'm doing wrong. Any info appreciated.

Windows for business | Windows Server | User experience | PowerShell
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2024-01-15T22:22:44.1133333+00:00

    What happens if you change the logic in the 2nd instance to this?

    if (-NOT (Get-Module -Name "CosmosDB" -ListAvailable) ) {   # module not present
        Install-Module -Name "CosmosDB" -Force
    }
    if (-NOT (Get-Module -Name "CosmosDb")){                    # module not imported
        Import-Module -Name "CosmosDB"
    }
    

  2. Preeti Mahajan 1 Reputation point
    2024-01-15T22:40:36.4866667+00:00

    Remove-Module does not uninstall the module or delete it from the machine. It will affect only the current PowerShell session. So, if you really need to delete the module and install again, you need to use Uninstall-Module. Otherwise, you can simply use Import-module in the second PowerShell task as the module is already installed on the machine using first PowerShell task.


  3. awrb194 0 Reputation points
    2024-01-16T14:32:00.8633333+00:00

    Couldn't get two AzurePowerShell@5 tasks to share the CosmosDB module so merged both AzurePowerShell@5 tasks into one. Not what I wanted but time constraints made the decision for me!

    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.