PowerShell 依赖管理

发布于 2024-10-20 22:43:24 字数 646 浏览 5 评论 0原文

场景

我的 PowerShell 文件夹包含实用程序脚本库。我通过 GitHub 在我的工作计算机和家庭计算机之间共享它并进行版本控制。在工作中,我现在有许多项目想要利用我的脚本库。

问题

当我更新实用程序脚本时,我不想将其手动复制到使用它的所有工作项目中。

可能的解决方案

  1. (简单)

    编写一个 PowerShell 函数,将整个脚本库复制到每个脚本项目工作目录下的“Dependencies\Scripts”目录中。随着我的脚本库的增长,其他人可能会很难找到与脚本项目相关的库脚本。

  2. (过于复杂?)

    在需要库脚本之一的每个工作项目脚本文件中使用某种“需要”函数。当库脚本更新时,工具可以决定哪些工作项目需要该库脚本并将最新版本复制到工作项目。如果脚本在没有适当依赖项的情况下运行,它将抛出一个错误,提醒用户如何从库中获取最新版本。

问题

  • 以前有人解决过这个问题吗?
  • 是否有现有的 PowerShell 依赖管理工具可以执行 2 操作?

Scenario

My PowerShell folder contains a library of utility scripts. I have it shared and version controlled with GitHub between my work and home computers. At work I now have a number of projects where I want to take advantage of my script library.

Problem

When I update the a utility script, I don't want to copy it manually to all the work projects where it is used.

Possible solutions

  1. (Simple)

    Write a PowerShell function to copy my whole script library to a 'Dependencies\Scripts' directory under the working directory for each script project. As my script library grows, it may become difficult for others to find the library scripts that are relevant to the script project.

  2. (Overcomplicated?)

    Use some kind of 'requires' function in each work project script file that requires one of library scripts. When a library script is updated a tool can then decide which work projects require that library script and copy the latest version to the work project. If a script is run without the appropriate dependency it will throw an error that reminding the user how to get the latest version from the library.

Questions

  • Has anyone solved this problem before?
  • Are there existing dependency management tools for PowerShell that will do 2?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只有一腔孤勇 2024-10-27 22:43:24

您是否考虑过 NuGet?它支持包依赖项、更新私有存储库

另请参阅:使用 Nuget 在企业中共享 PowerShell 模块

Have you considered NuGet? It supports package dependencies, updates, and private repositories.

See also: Use Nuget to Share PowerShell Modules in your Enterprise

黄昏下泛黄的笔记 2024-10-27 22:43:24

我为您创建了一个我认为适合您情况的解决方案。我根据歌曲播放列表方法创建了它。我创建了一个 xml 文档,您可以在其中单独列出每个脚本,并在同一文档的另一个节点中列出要为每个项目复制的脚本。我在下面创建了一个工作示例。虽然在管理数百个脚本文件或大量项目时它并不优雅,但它可以完成工作。

PS1 脚本

[xml]$XML = gc "C:\XMLFile1.xml"
$Scripts = $XML.Root.Scripts.Script
$Projects = $XML.Root.Projects.Project
foreach($Project in $Projects){
    $ProjectLocation = $Project.CopyPath
    $ProjectScripts = $Project.Script
    foreach($Script in $ProjectScripts){
        $ScriptPath = ($Scripts|?{$_.ID -eq $Script.ID}|Select Path).Path
        Copy-Item -Path $ScriptPath -Destination $ProjectLocation
    }
}

XML 文件

<Root>
  <Scripts>
    <Script ID="1" Path="C:\1.PS1"></Script>
    <Script ID="2" Path="C:\2.PS1"></Script>
    <Script ID="3" Path="C:\3.PSM1"></Script>
  </Scripts>
  <Projects>
    <Project Name="Project1" CopyPath="\\Server\Share\Project1">
      <Scripts ID="1"/>
    </Project>
    <Project Name="Project2" CopyPath="C:\Projects\Project2">
      <Scripts ID="1"/>
      <Scripts ID="3"/>
    </Project>
  </Projects>
</Root>

I created a solution for you that I think will fit your situation. I created it based off of the song playlist methodology. I created an xml document where you would list each of your scripts individually and in another node in the same document you list the scripts you want to copy for each project. I have created a working example of this below. Though it is not elegant when it comes to managing a few hundred script files or alot of projects but it gets the job done.

PS1 Script

[xml]$XML = gc "C:\XMLFile1.xml"
$Scripts = $XML.Root.Scripts.Script
$Projects = $XML.Root.Projects.Project
foreach($Project in $Projects){
    $ProjectLocation = $Project.CopyPath
    $ProjectScripts = $Project.Script
    foreach($Script in $ProjectScripts){
        $ScriptPath = ($Scripts|?{$_.ID -eq $Script.ID}|Select Path).Path
        Copy-Item -Path $ScriptPath -Destination $ProjectLocation
    }
}

XMLFile

<Root>
  <Scripts>
    <Script ID="1" Path="C:\1.PS1"></Script>
    <Script ID="2" Path="C:\2.PS1"></Script>
    <Script ID="3" Path="C:\3.PSM1"></Script>
  </Scripts>
  <Projects>
    <Project Name="Project1" CopyPath="\\Server\Share\Project1">
      <Scripts ID="1"/>
    </Project>
    <Project Name="Project2" CopyPath="C:\Projects\Project2">
      <Scripts ID="1"/>
      <Scripts ID="3"/>
    </Project>
  </Projects>
</Root>
烦人精 2024-10-27 22:43:24

一个简单的解决方案是使用 DropBox 之类的东西。您可以在此处查看我如何将其用于 PowerShell 脚本: http://www.ravichaganti.com /blog/?p=1963

您可以获得一个拥有 2GB 可用空间的 DropBox 帐户 http://db.tt /1DID1mR。在我看来,2GB 对于简单的脚本来说已经足够了。市场上还有其他选择。不过,我推荐 DropBox。免费帐户支持恢复 30 天前的文件版本。

A simple solution would be to use something like DropBox. You can see how I use it for my PowerShell Scripts here: http://www.ravichaganti.com/blog/?p=1963

You can get a DropBox account with 2GB of free space http://db.tt/1DID1mR. 2GB, in my opinion, is more than enough for simple scripts. There are also other choices in the market. However, I recommend DropBox. The free account supports restoring 30 days old file versions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文