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

Kinga - Jul 17 - - Dev Community

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?

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player