How to get a list of packages from one machine and install in another with Chocolatey?

前端 未结 6 2028
迷失自我
迷失自我 2021-01-31 08:23

Calling clist -l gives me a list of packages with versions:

7zip.install 16.04
ccleaner 5.28.6005
ConEmu 17.3.16.0
...

How do I ge

相关标签:
6条回答
  • 2021-01-31 08:57

    Since you want to omit the version I used the choco upgrade:

    # Filter for selecting packages, if empty will match all.
    # I do this at times to see whats installed for my company packages
    $PkgPrefix = ""
    
    $cmd = "cup -y "
    Test-WSMan $server | Out-Null
    $session = New-PSSession -ComputerName $server -Credential ( Import-Clixml -Path $CredenitalFile ) -Verbose -Authentication Negotiate
    
    $(clist -lo -r --id-starts-with "$PkgPrefix" )| % { $cmd += "$($_.Split( "|" )[0]),"}
    
    Invoke-Command -Session $session -ScriptBlock $cmd
    

    As Gary suggested, a configuration file might be an easier solution to maintain. I server up my configurations on a web-server, so I can just shell in and execute one command to install everything and have the ability to make a simple XSL style sheet for viewing.

    cinst -y $( ( [xml]( Invoke-WebRequest -Uri http://softwareList.config) ).packages.package | Select id ).id
    

    Or you could just save it locally and call it with all the information:

    (iwr -Uri http://softwareList.config).content | Out-File "$($env:LOCALAPPDATA)\list.config" -Encoding utf8;
    cinst "$($env:LOCALAPPDATA)\list.config -y
    
    0 讨论(0)
  • 2021-01-31 09:05

    Chocolatey doesn't provide that command. The question (plus additional requirements you mentioned) can only be answered with an ETL solution. There's no command to accomplish this proposal.

    A .ps1 script would work fine.

    That being said, by removing the version, you'll be jumping some packages to the latest published version on the target server. If you need a clone of the installs, you'll need to consume the versions as well.

    0 讨论(0)
  • 2021-01-31 09:10

    Here is how I generated my packages.config:

    $packageXml = ''
    choco list -lo -r | % { $_ -split '\|' | select -first 1 } | % { $packageXml += "`n`t<package id=""$_"" />" }
    Set-Content "<?xml version=`"1.0`" encoding=`"utf-8`"?>`n<packages>$packageXml`n</packages>" -Path .\packages.config
    

    Once you have that, you take that file to the other machine and do:

    choco install packages.config
    
    0 讨论(0)
  • 2021-01-31 09:10

    I would like to summarize the two general approaches given by other answers to this post.

    1. Export a plain list of package names, and use it to build a cinst command.

    This is aligned with the question as asked: how to clist -l without version information.

    I like the simple answer of clist -l --idonly, but to automate this, one must still account for the first and last lines. So, a simple script is probably necessary -- and on Windows, this is a bit of a pain (which is why this question is even being asked).

    If the primary goal is to backup and re-install Choclatey packages, then there are some good choices without writing your own scripts. Hence the next type of solution...

    2. Export a packages.config file, which can be installed by cinst natively.

    This is a nice way to do it, because installing a packages.config is natively supported by cinst.
    Unfortunately, Chocolatey lacks native support for exporting packages.config.

    Here are a few common ways to export packages.config:

    • Chocolatey GUI
    • bcurran3/choco-package-list-backup - an unofficial package for this purpose
    • Custom scripts (example 1, example 2)

    For background to this approach, and to track progress of a built-in solution, see https://github.com/chocolatey/choco/issues/357.

    0 讨论(0)
  • 2021-01-31 09:11

    This was my poor-man's solution to the same problem, i.e. take all the Chocolatey packages on one machine and install them on another, without worrying about specific versions (i.e. I want the latest versions).

    1. Use the Export button on Chocolately-GUI to save a packages.config file (to a shared network drive).
    2. Edit that .config file and remove the version="X.Y.Z" fields from each <package ... /> line.
    3. On the new machine run choco install \\mypc\shared\packages.config -y.

    For example, my edited packages.config file looks like this:

    <?xml version="1.0" encoding="utf-8"?>
    <packages>
      <package id="audacity" />
      <package id="autohotkey" />
      <package id="autohotkey.install" />
      <package id="ccleaner" />
      <package id="chocolatey" />
      <package id="chocolatey-core.extension" />
      <package id="chocolateygui" />
    </packages>
    

    PS.: Don't make the same mistake I did: I used a simple regular expression in Notepad++ to delete all the version="1.1.1" entries and inadvertently removed the same field from the first <?xml ... ?> line. This breaks the XML file. Be more careful/clever than me!

    0 讨论(0)
  • 2021-01-31 09:21

    If you have a look at the help information for the choco install command (you can do this using chcco install -h, you will find the following usage:

    cinst <pkg|packages.config> [<pkg2> <pkgN>] [<options/switches>]
    

    As you will see, it is possible to pass a packages.config file, which would contain all the packages that you want to install. The format of this packages.config file is very simple and looks like the following:

    <?xml version="1.0" encoding="utf-8"?>
    <packages>
      <package id="calibre" version="2.81.0" />
      <package id="chocolatey" version="0.10.3" />
      <package id="chocolatey.extension" version="1.9.6" />
      <package id="chocolatey-core.extension" version="1.1.0" />
    </packages>
    

    Once you have this file, installing all the packages again on another machine is a simple one line command.

    A simple way to generate this packages.config file would be to install ChocolateyGUI (choco install chocolateygui), which includes an option to export the currently installed list of applications.

    0 讨论(0)
提交回复
热议问题