DEV Community

Kinga
Kinga

Posted on • Updated on

Azure DevOps and "The term 'Install-Module' is not recognized" issue

Just a quick note in case it will help someone. =)

The problem

Since somewhere beginning of July, I started noticing that my pipeline is failing randomly with The term 'Install-Module' is not recognized as a name of a cmdlet, function, script file, or executable program. error.

The pipeline is configured as follows:

mypipeline.yaml

- job: ExportLists
    displayName: Export Lists from ${{ parameters.exportFrom }}
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - task: AzurePowerShell@5
      name: DeploySPFx
      inputs:
        azureSubscription: $(serviceConnection)
        azurePowerShellVersion: LatestVersion
        ScriptType: FilePath
        ScriptPath: $(Build.SourcesDirectory)/Pipelines/scripts/SPO-ExportLists.ps1
        ScriptArguments: >
          -tenantName '$(Az_TenantName)'
          -siteName '$(SPO_SiteName)'
          -folderPath '$(Build.SourcesDirectory)/SPO/templates'
      displayName: Export lists
Enter fullscreen mode Exit fullscreen mode

The PowerShell script WAS like that:

ExportLists.ps1

[CmdletBinding()]
param(
    $tenantName,
    $siteName,
    $folderPath
)

Install-Module -Name PnP.PowerShell -Scope CurrentUser `
   -SkipPublisherCheck -Force

#.....
Enter fullscreen mode Exit fullscreen mode

And depending on the day the script sometimes worked, sometimes it didn't.
Occasionally, different jobs in the same stage would either succeed or fail on the Install-Module command.

Troubleshooting

According to the Ubuntu 22.04, this image comes with PowerShell 7.4.3, and

PowerShell 7.4 includes Microsoft.PowerShell.PSResourceGet v1.0.1. This module is installed side-by-side with PowerShellGet v2.2.5 and PackageManagement v1.4.8.1.

I see no reason for Install-Module failing, and ... why so randomly.

The solution

I changed my code to call Get-Module and now the script looks like that:

ExportLists.ps1

[CmdletBinding()]
param(
    [string]$tenantName,
    [string]$siteName,
    [string]$folderPath
)

Write-Host "##[group]Install PS modules"
Write-Host "##[command] Get Module PowerShellGet"
Get-Module -Name PowerShellGet -ListAvailable
Write-Host "##[command] Get Module PowerShellGet"
Get-Module -Name Microsoft.PowerShell.PSResourceGet -ListAvailable
Write-Host "##[command] Install PnP.PowerShell"
Install-Module -Name PnP.PowerShell -Scope CurrentUser -SkipPublisherCheck -Force

Write-Host "##[endgroup]"
Enter fullscreen mode Exit fullscreen mode

And.. it works. Since whole three days already! Let's hope it will stay this way.🤞

Can someone explain it to me?

Top comments (5)

Collapse
 
satish_nagasubramaniam profile image
Satish Nagasubramaniam

@kkazala I am making you popular, this link is very famous in Microsoft now, I have referred this page to most of the support Engineers. The above fix you have given here, even makes MS support guys wonder. Please give yourself a pat on your back.

Collapse
 
kkazala profile image
Kinga

:D Really??? Wow! Thank you! 💙
And I was wondering if I should write it at all. I thought the error was so generic that nobody would ever read it :D

Collapse
 
satish_nagasubramaniam profile image
Satish Nagasubramaniam

Oh no, never think like that, you don't know what impact you r post created, your post saved our production pipelines. You are our rockstar at the moment. I mean it.

Collapse
 
satish_nagasubramaniam profile image
Satish Nagasubramaniam

I was haunted by this install-module error for months now and I too use ubuntu-latest and never had time to wrap my head around this and yesterday shit hits the fan and we were scrambling to get MS to support and I stumbled upon your post (how lucky I am).

You saved my day. I have applied your fix and I will see how long this would continue to work in our environment.

Collapse
 
kkazala profile image
Kinga

awww, thank you! You made my day!
"will see how long this would continue to work in our environment"- ditto 😂