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
The PowerShell script WAS like that:
ExportLists.ps1
[CmdletBinding()]
param(
$tenantName,
$siteName,
$folderPath
)
Install-Module -Name PnP.PowerShell -Scope CurrentUser `
-SkipPublisherCheck -Force
#.....
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]"
And.. it works. Since whole three days already! Let's hope it will stay this way.🤞
Can someone explain it to me?
[Update 2024.11.07] Please see Robert Reems' suggestion below. It will save you some cash. :D
Top comments (7)
I don't fully understand the solution. But it got me thinking that the get-module cmdlet imports the PowerShellGet module.
So I thought, maybe it's enough to just state that the module is required by stateing the following on top of my script:
This seems to work as well.
I can image that importing the module using Import-Module works as well.
You are absolutely right and I think your approach is better.
I admit I was in a rush (aren't we all 😅 ), and once I found a solution I moved on.
I never felt really good about it because this module is not always missing,
and if it is available, I'm reinstalling it, wasting resources, time and $$$[2024.11.11] Just looked at my code again and I see I'm simply running
Get-Module
. Still... I think that #Requires is cleaner ⭐I'm not even sure if both of these modules should be reinstalled but never found time to come back to it and test. And.. was hoping it would get fixed soon.
So thank you for your suggestion! 💖
@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.
: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
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.
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.
awww, thank you! You made my day!
"will see how long this would continue to work in our environment"- ditto 😂