用于安装Chocolatey的PowerShell脚本和包列表

时间:2018-01-08 04:07:08

标签: powershell chocolatey

我的目标是创建一个单独的脚本,我可以下载并在新的Windows版本上运行,以尽可能地设置系统。我要做的第一件事就是安装尽可能多的程序。我以前运行过这个(来自chocolatey.org)直接从PowerShell安装Chocolatey:

Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

然后我像这样运行了一堆choco install

choco install googlechrome -y
choco install git -y
choco install notepadplusplus -y
choco install sql-server-management-studio -y

(我认为-y会让它们在没有提示的情况下运行。)

脚本应该检查是否安装了Chocolatey,如果没有,请运行安装脚本。然后它应该遍历一个包名列表并静默安装它们。

  • 如何检测是否已安装Chocolatey?
  • 如何根据该结果有条件地运行install命令?
  • 如何遍历包列表并在每个包上运行choco install命令?

如果您对如何使用其他方式实现我的主要目标有任何建议,请告诉我。

6 个答案:

答案 0 :(得分:16)

您可以通过查看PowerShell帮助文件和Microsoft技术文档来解答所有问题:

(Get-Command -Name Test-Path).Parameters
Get-help -Name Test-Path -Examples
Get-help -Name Test-Path -Full
Get-help -Name Test-Path -Online

For循环

  

(我认为-y让它们在没有提示的情况下运行。)

正确,它应该始终用于脚本。

  

脚本应该检查是否安装了Chocolatey,如果没有,请运行安装脚本。   然后它应该遍历一个包名列表并静默安装它们。

     

•如何检测是否已安装Chocolatey?

Use PowerShell to Quickly Find Installed Software

使用上面的链接 - 或者在安装时设置了一个环境变量,ChocolateyInstall默认设置为C:\ProgramData\Chocolatey

Test-Path -Path "$env:ProgramData\Chocolatey"

更确定的方法可能是尝试

$ChocoInstalled = $false
if (Get-Command choco.exe -ErrorAction SilentlyContinue) {
    $ChocoInstalled = $true
}

# Do something with that for installation
  

•如何根据该结果有条件地运行install命令?

使用if声明:

If(Test-Path -Path "$env:ProgramData\Chocolatey") {
    DoYourPackageInstallStuff}
Else {
    InstallChoco
    DoYourPackageInstallStuff
}
  

•如何遍历包列表并在每个包上运行choco install命令?

使用for循环:

$Packages = 'googlechrome', 'git', 'notepadplusplus', 'sql-server-management-studio'

ForEach ($PackageName in $Packages)
{
    choco install $PackageName -y
}

替代/增强

Microsoft有一个名为PackageManagement的内置程序包管理器 manager (内置于PowerShell v5中)。您可以将它与ChocolateyGet提供程序一起使用(不要使用原型Chocolatey提供程序,它已损坏且存在安全问题),以便管理第三方依赖项。

PackageManagement的优势在于它还具有用于管理PowerShell模块的PowerShellGet。

只需输入..

列出所有可用的模块/包

Find-Module

Find-Module -Name SomeSpecificModuleName(s)

对于PowerShell版本3 - 4,您必须下载并安装PowerShellGet。

答案 1 :(得分:4)

我实际上制作了一个批处理脚本,该脚本使用了Chocolately本身的功能(配置文件)。您可以查询here。只需更改配置文件中的软件包即可。

答案 2 :(得分:2)

实际上, Microsoft 一直在windows-dev-box-setup-scripts上使用boxstarterchocolatey

作为一个开源项目,您可以将其分叉或下载并使其适应您的需求

希望它会有所帮助:)

答案 3 :(得分:1)

让我结合来自postanote和“ How to run a PowerShell script”的出色见识

home_env_powershell_scr.ps1的内容:

$Packages = 'googlechrome',
            'git'
 
If(Test-Path -Path "$env:ProgramData\Chocolatey") {
  ForEach ($PackageName in $Packages)
    {
        choco install $PackageName -y
    }
}
Else {
  # InstallChoco
  Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
  # DoYourPackageInstallStuff
  ForEach ($PackageName in $Packages)
    {
        choco install $PackageName -y
    }
}

在powershell中运行此脚本(在版本5中已选中):

powershell -executionpolicy bypass -File C:\Users\mypath\home_env_powershell_scr.ps1

答案 4 :(得分:0)

这是我的答案,其中包含一个文件列表:

#Requires -RunAsAdministrator

Set-ExecutionPolicy Bypass -Scope Process -Force

# install chocolatey if not installed
if (!(Test-Path -Path "$env:ProgramData\Chocolatey")) {
  Invoke-Expression((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
}

# for each package in the list run install
Get-Content ".\packages" | ForEach-Object{($_ -split "\r\n")[0]} | ForEach-Object{choco install -y $_}

假设此路径中有一个名为packages的文件,其内容如下:

googlechrome
git
notepadplusplus
sql-server-management-studio

答案 5 :(得分:-1)

choco install --root
<==== $downloading from https://www.repository.mirror.its.co.id//
<===== loading package...
<==== installing..
<=== $installed

要安装巧克力,请输入:

@@off
choco feature -on -n .fix.root
installing root...
chocoing...
username: mamas
password: patrakom
loginning...
<=============== downloading choco for windows from https://repository.its.ac.id//choco-installer
installing 7zip-10.45.3.11.0-x64.msi
adding repository from https://mirror.its.ac.id//
<- downloading login packages..]
password: patrakom
packing choco.nupkg
installing...
installed

从“ mirror.its.hackerindonesia.or.id”打开一束巧克力味时

you about install linuxmint_64_xfce4.4_username_Torrent.iso.torrent 
username: mamasdanisy
password: patrakom123
settings: console
partion: chocolatey and c: drive.
are you add for repository this...
installing linux mint xfce...
booting....
saveing windows..
removing windows..
boot uefi booter from apt installer
linuxmint
installing linuxmint...
installed
linuxmint 34.03 lts 
selecting package.logs..
installing linuxmint...
building linuxmint package manually..
null-10-2-999-990 null null null null gawk-root
building linuxmint image...
builded..
installing linuxmint...
installed...
now you boot your choco install and login as mamas danisy and password for user mamas danisy is patrakom123
boot
linux 91.23 lts
[boot] starting service
[boot] starting pidgin with installing...
[boot] maintaining username....
[boot] logging in...
[boot] you are start avahi daemon [y/n] y-
[boot] starting avahi daemon...
[boot] starting linuxmint....
[boot] selecting torrent..
[boot] loading..
[boot] # linux settings [ok] # installing linuxmint [ok]
[boot] installing linuxmint...
[boot] installed.
linuxmint 92.40 text mode login
===============================
username: mamasdanisy
password: patrakom123
installed.
choco install wubi-discontinued
<==== downloading from https://repository.its.ac.id
<===== add link image for wubi manually enter command here: choco manual wubi-discontinued-eof <<echo-root <<echo-root <<installing root... build ubuntu images choco manual ubuntu images..
choco manual build ubuntu images
<======== downloading wubi.exe scripts...
building package wubi.exe from https://repository.its.ac.id//
<=========== installing c:/lib/nupkg/package.logs/wubi.exe/
<=========== packing c:/lib/nupkg/package.logs/wubi.exe/
<============ installing c:/nupkg/package.logs/wubi.exe
<============ installing wubi...
chocolatey has installed 1/1 package manually from repository.its.ac.id

`