【问题标题】:How to distribute powershell scripts to team members?如何将 powershell 脚本分发给团队成员?
【发布时间】:2019-04-30 18:10:19
【问题描述】:

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

这些是私有脚本,不想像https://www.powershellgallery.com/那样在原地发布

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

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

【问题讨论】:

  • 如果您有有助于构建/部署的脚本,它们应该与应用程序位于同一存储库中。除此之外,为什么不将您的脚本转换为 NuGet 包并将它们托管在内部 NuGet 源上呢?人们可以注册您的 NuGet 提要并使用标准的Install-Module 命令。
  • git pull 从 repo 中提取最新数据?可以制作一个 powershell 构建脚本并将其附加到每个用户配置文件中运行,该配置文件从 repo 中提取并导入您希望默认可用的每个模块。请注意,您导入的模块越多,每次启动的速度就越慢。
  • 谢谢,一旦我在他们的本地计算机中获得了文件,如何将其带入他们当前的 powershell 会话?

标签: powershell dotnet-tool


【解决方案1】:

至于这个……

这些是私人脚本,不想像这样发布

.. 然后在 prem repo 上构建你自己的。

如何做到这一点由微软和其他公司完整记录,如下所示:

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 是一个 Web 应用程序,您可以 安装在您的 IIS 上。它提供了一个简单的管理工具和访问 在您的服务器上自托管的 git 存储库。

https://bonobogitserver.com/features

https://bonobogitserver.com/install

OP 更新

至于你的后续:

一旦我在他们的本地计算机中获得了文件,如何将它带来 进入他们当前的 powershell 会话?

请记住,Import-Module 是关于要加载的已在本地计算机上的模块,而不是来自任何本地或远程 repo 的模块。您仍然必须安装模块形式,无论您的目标是什么。

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

具体来说,您的后续问题与此Q&A 重复 如何从本地文件夹安装/更新 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

【讨论】:

  • 谢谢。一旦这样做,我如何将我的模块导入到我当前的 powershell 会话中?普通的Import-Module 会起作用吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-09
相关资源
最近更新 更多