Recommended practices for use of Install-Module in scripts.

Welcome Forums General PowerShell Q&A Recommended practices for use of Install-Module in scripts.

This topic contains 2 replies, has 2 voices, and was last updated by

2 weeks, 4 days ago.

  • Author
  • #132629

    Points: 20
    Rank: Member

    We have recently completed our internal PowerShell repository using ProGet. We are publishing modules here with a Jenkins CI job whenever changes are committed. That whole part of the set up was pretty smooth. I ran a script that needs one of the modules in the new repository so I added Install-Module at the beginning of the script to make sure the dependency is met before the rest of the script starts.

    However, Install-Module itself depends on the presence of a PSRepository that points to my internal ProGet feed so a call to Register-PSRepository might be needed before Install-Module can be used. There is also a need for Install-PackageProvider before Install-Module can be used if that is missing.

    I suppose I could put steps into each script to run Install-PackageProvider and Register-PSRepository if those items are missing but that feels kind of heavy to be putting that into a bunch of scripts. I could use some help on methods to manage this effectively.

    I'm assuming that I must find all the servers where my scripts will be running and get the Install-PackageProvider and Register-PSRepository steps done by some method outside the individual scripts that will call Install-Module.

    Am I on the right track here?


  • #132647

    Points: 282
    Helping Hand
    Rank: Contributor

    Hi Matthew РUsually, during the server build, teams ensure that all the necessary packages are installed and configured as it is required for the environment. But for now, as you mentioned you can install the package provider, then register the repository and install the module on all the servers, or you can install them if they are missing before the script execution. And the other you can use PowerShell DSC to ensure that the required package provider is installed, the repository is registered and the module is also installed. Thank you.

  • #132785

    Points: 20
    Rank: Member

    Kirin, That makes sense. I'll get with the infrastructure team and work out how to roll out this necessary infrastructure first before we start trying to use the Install-Module option in our scripts.

You must be logged in to reply to this topic.