question

PedroRibeiro-8092 avatar image
0 Votes"
PedroRibeiro-8092 asked ·

YAML Powershell Install-Module every time?

Hi,
I have an pipeline that executes a powershell script. In that script I need to install a module.

  ### INSTALL MODULES AND LOGIN TO POWER ###
  Write-Host "### Installing PBI module..." -ForegroundColor Green;
  Install-module -Name "MicrosoftPowerBIMgmt" -AllowClobber -Scope CurrentUser -Verbose

My questions is: it's possible to install the module just once, without having to install every time the script runs?

Thanks.

windows-server-powershell
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
0 Votes"
cooldadtx answered ·

Yes, if you have permissions. PS has a store of all installed modules. When you install a module you specify whether to install per user or per machine. If you install at the machine level, again assuming permissions, then the module need only be installed once. Thereafter if you try to use a command in the module then PS will auto-load the module. Technically the same thing will work with a per-user module.

What I personally recommend is that you first check to see if the module is installed. If it isn't then install the module. After that you can run normally and PS will load the module. Something like this for PS 7.1 (not tested).

function EnsureModuleInstalled ( $name ) {
   $module = Get-InstalledModule -Name $name
   if ($null -eq $module) {
      Install-Module -Name $name -Scope CurrentUser -AllowClobber
   }
}

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PedroRibeiro-8092 avatar image
0 Votes"
PedroRibeiro-8092 answered ·

Sorry, but I forget to mention that this script runs in Azure DevOps.

· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Doesn't matter. You can do this in PS in DevOps as well.

0 Votes 0 ·
IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered ·

Hi,

Unless you add the "-Force" switch to the Install-Module cmdlet, the module will be skipped by default if it's already installed.
Install-Module

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.