Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell dependency management

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?
like image 305
Alex Avatar asked Mar 05 '11 08:03

Alex


2 Answers

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>
like image 73
jamason1983 Avatar answered Nov 15 '22 09:11

jamason1983


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

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

like image 25
Emperor XLII Avatar answered Nov 15 '22 09:11

Emperor XLII