如何将 powershell 脚本分发给团队成员?

How to distribute powershell scripts to team members?

我与一群软件开发人员一起工作,我有一堆方便的 powershell 脚本来自动化 building/deploying 等等...我希望我所有的同事都能够安装和使用这些脚本。如果他们在我添加更多 features/fix 个错误时获得自动更新,那就太好了。

这些是私有脚本,不想像https://www.powershellgallery.com/

那样就地发布

今天,我们团队中的每个开发人员都从 git 存储库下载这些脚本,并将此文件夹添加到 $path。此文件夹有一个打开 powershell 控制台的 .bat 文件。在此控制台中,他们可以获得帮助并调用各种可用命令。 今天他们需要调用一个从 repo 中提取最新的命令。

我觉得应该有比这更好的东西,我正在寻找类似 dotnet global tools 的 powershell 脚本。

至于这个...

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

.. 然后构建您自己的预置存储库。

如何执行此操作已由 Microsoft 和其他公司完整记录,如下所示:

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.

或者建立自己的内部 git 服务器。

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

OP 更新

关于你的跟进:

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

请记住,Import-Module 是关于要加载的模块,这些模块已经在您的本地计算机上,而不是来自任何本地或远程仓库的模块。您仍然必须从您定位的任何 repo 安装模块。

如果您的模块已正确定义并安装在本地计算机上,如果您使用的是 PowerShell v3 及更高版本,则不需要 Import-Module,因为在正确设计和实施后,它们应该会自动加载。

具体来说,您的后续问题与此 Q&A 重复 如何 install/update 来自本地文件夹的 PowerShell 模块 - 设置内部模块存储库

您只需执行正常步骤即可从您的存储库中获取和使用该模块。

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

Install-Module -Name 'MyModule'

Import-Module -Name 'MyModule'

另请参阅:

Update-Module