powershelldotnet-tool

How to distribute powershell scripts to team members?


I work with bunch of software developers, I have bunch of handy powershell scripts to automate building/deploying etc... I would like all my fellow co-workers to be able to install and use these scripts. It would be nice, if they get automatic updates as I add more features/fix bugs.

These are private scripts and don't want to publish in place like https://www.powershellgallery.com/

Today, every developer in our team downloads these scripts from a git repo and add this folder to $path. This folder has a .bat file that opens a powershell console. In this console they can get help and invoke various available commands. Today they need to invoke a command that pulls latest from repo.

I feel like there ought to be something better than this, I am looking for something like dotnet global tools for powershell scripts.


Solution

  • As for this...

    These are private scripts and don't want to publish in place like

    .. then build your own on prem repo.

    How to do this is fully documented by Microsoft and other as seen here:

    Setting up an Internal PowerShellGet Repository

    Powershell: Your first internal PSScript repository

    # Network share
    # The other thing we should have is an empty folder on a network share. This will be the location of our repository. Your users will need to have access to this location if they are going to be loading content from it.
    
    
    $Path = '\\Server\Share\MyRepository'
    
    
    # If you just want to experiment with these commands, you can use a local folder for your repository. PowerShellGet does not care where the folder lives.
    
    
    # Creating the repository
    # The first thing we should do is tell PowerShellGet that our $Path is a script repository.
    
    Import-Module PowerShellGet
    
    $repo = @{
        Name = 'MyRepository'
        SourceLocation = $Path
        PublishLocation = $Path
        InstallationPolicy = 'Trusted'
    }
    
    Register-PSRepository @repo
    
    
    # And we are done.
    
    Get-PSRepository
    
    Name         InstallationPolicy SourceLocation
    ----         ------------------ --------------
    MyRepository Trusted            \\Server\Share\MyRepository
    PSGallery    Untrusted          https://www.powershellgallery.com/api/v2/
    
    
    # Other than creating a folder, there is no complicated setup to creating this repository. Just by telling PowerShellGet that the folder is a repository, it will consider it to be one. The one catch is that you need to run this command on each machine to register the repository.
    

    Or stand up your own internal git server.

    Bonobo Git Server for Windows is a web application you can install on your IIS. It provides an easy management tool and access to your git repositories that are self hosted on your server.

    https://bonobogitserver.com/features

    https://bonobogitserver.com/install

    Update for OP

    As for your follow-up:

    Once I have the files in their local computer, how would the bring it into their current powershell session?

    Remember that Import-Module, is about modules to be loaded that are already on your local machine, not modules from any local or remote repo. You still have to install the module form whatever repo you target.

    If you module is properly defined, and installed on the local machine, if you are on PowerShell v3 and higher, Import-Module should not be needed, as when properly designed and implemented, they should autoload.

    Specifically, your follow-up question is a duplicate of this Q&A How to install/update a PowerShell module from a local folder - set up an internal module repository

    You should just have to do the normal steps to get and use the module from your repo.

    Find-Module -Name 'MyModule' -Repository MyRepository | 
    Save-Module -Path "$env:USERPROFILE\Documents\WindowsPowerShell\Modules"
    
    Install-Module -Name 'MyModule'
    
    Import-Module -Name 'MyModule'
    

    See also:

    Update-Module