用于安装 Chocolatey 和软件包列表的 PowerShell 脚本
PowerShell script to install Chocolatey and a list of packages
我的目标是创建一个我可以下载并 运行 在新的 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?
- 如何根据该结果有条件地 运行 安装命令?
- 如何遍历软件包列表并运行每个软件包的
choco install
命令?
如果您对如何使用其他方式实现我的主要目标有任何建议,请告诉我。
您的所有问题都可以通过查看 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
循环
(I think the -y makes them run without a prompt.)
正确,应该始终在脚本中使用。
The script should check if Chocolatey is installed and if not, run the install script.
Then it should loop through a list of package names and silently install them.
• How do I detect if Chocolatey is already installed?
Use PowerShell to Quickly Find Installed Software
使用上面的 link - 或者在安装时设置了一个环境变量,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
• How do I conditionally run the install command based on that result?
使用 if
语句:
If(Test-Path -Path "$env:ProgramData\Chocolatey") {
DoYourPackageInstallStuff}
Else {
InstallChoco
DoYourPackageInstallStuff
}
• How do I loop through a list of packages and run the choco install command on each?
使用 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。
我实际上制作了一个使用巧克力本身(配置文件)功能的批处理脚本。你可以查一下here。只需更改配置文件中的包。
实际上 Microsoft 一直在开发 windows-dev-box-setup-scripts to accomplish that, using boxstarter and chocolatey
作为一个开源项目,您可以分叉或下载它并根据您的需要进行调整
希望对您有所帮助:)
这是我的答案,其中包含一个包含软件包列表的文件:
#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
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
`
让我结合 and "How to run a PowerShell script
的精彩见解
home_env_powershell_scr.ps1
的内容:
$Packages = 'googlechrome',
'git'
If(Test-Path -Path "$env:ProgramData\Chocolatey") {
# DoYourPackageInstallStuff
ForEach ($PackageName in $Packages)
{
choco install $PackageName -y
}
}
Else {
# InstallChoco
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; 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
我的目标是创建一个我可以下载并 运行 在新的 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?
- 如何根据该结果有条件地 运行 安装命令?
- 如何遍历软件包列表并运行每个软件包的
choco install
命令?
如果您对如何使用其他方式实现我的主要目标有任何建议,请告诉我。
您的所有问题都可以通过查看 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
循环
(I think the -y makes them run without a prompt.)
正确,应该始终在脚本中使用。
The script should check if Chocolatey is installed and if not, run the install script. Then it should loop through a list of package names and silently install them.
• How do I detect if Chocolatey is already installed?
Use PowerShell to Quickly Find Installed Software
使用上面的 link - 或者在安装时设置了一个环境变量,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
• How do I conditionally run the install command based on that result?
使用 if
语句:
If(Test-Path -Path "$env:ProgramData\Chocolatey") {
DoYourPackageInstallStuff}
Else {
InstallChoco
DoYourPackageInstallStuff
}
• How do I loop through a list of packages and run the choco install command on each?
使用 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。
我实际上制作了一个使用巧克力本身(配置文件)功能的批处理脚本。你可以查一下here。只需更改配置文件中的包。
实际上 Microsoft 一直在开发 windows-dev-box-setup-scripts to accomplish that, using boxstarter and chocolatey
作为一个开源项目,您可以分叉或下载它并根据您的需要进行调整
希望对您有所帮助:)
这是我的答案,其中包含一个包含软件包列表的文件:
#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
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
`
让我结合
home_env_powershell_scr.ps1
的内容:
$Packages = 'googlechrome',
'git'
If(Test-Path -Path "$env:ProgramData\Chocolatey") {
# DoYourPackageInstallStuff
ForEach ($PackageName in $Packages)
{
choco install $PackageName -y
}
}
Else {
# InstallChoco
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; 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