Win10离线安装choco方案

article/2025/8/18 11:21:52

Win10离线安装choco方案

    • 问题描述
    • 前置条件,下载必须的`nupkg`
    • 解决办法: 离线安装choco
      • 修改`install.ps1`脚本
        • 修改第46行:
        • 修改第277行:
      • install.ps1脚本
      • `powersell`开启`信任脚本`策略
      • 以`管理员`powershell执行安装脚本
    • 结果验证

问题描述

安装choco的时候,总是超时失败.


前置条件,下载必须的nupkg

https://packages.chocolatey.org/chocolatey.0.10.15.nupkg
下载chocolatey.0.10.15.nupkg,和下面的install.ps1脚本放在同一目录。


解决办法: 离线安装choco

参考: https://blog.csdn.net/sw3114/article/details/104299003
choco官方离线安装方案: https://docs.chocolatey.org/en-us/choco/setup


修改install.ps1脚本


修改第46行:

# 修改第46行的nupkg包路径
$localChocolateyPackageFilePath = 'nupkg包路径'

在这里插入图片描述


修改第277行:

# 第277行 Download-Package 这个注释掉
Download-Package $searchUrl $localChocolateyPackageFilePath

在这里插入图片描述


install.ps1脚本

install.ps1内容如下,将该脚本保存到本地,和chocolatey.0.10.15.nupkg文件放在同一目录

# Download and install Chocolatey nupkg from an OData (HTTP/HTTPS) url such as Artifactory, Nexus, ProGet (all of these are recommended for organizational use), or Chocolatey.Server (great for smaller organizations and POCs)
# This is where you see the top level API - with xml to Packages - should look nearly the same as https://chocolatey.org/api/v2/
# If you are using Nexus, always add the trailing slash or it won't work
# === EDIT HERE ===
$packageRepo = '<INSERT ODATA REPO URL>'# If the above $packageRepo repository requires authentication, add the username and password here. Otherwise these leave these as empty strings.
$repoUsername = ''    # this must be empty is NOT using authentication
$repoPassword = ''    # this must be empty if NOT using authentication# Determine unzipping method
# 7zip is the most compatible, but you need an internally hosted 7za.exe.
# Make sure the version matches for the arguments as well.
# Built-in does not work with Server Core, but if you have PowerShell 5
# it uses Expand-Archive instead of COM
$unzipMethod = 'builtin'
#$unzipMethod = '7zip'
#$7zipUrl = 'https://chocolatey.org/7za.exe' (download this file, host internally, and update this to internal)# === ENVIRONMENT VARIABLES YOU CAN SET ===
# Prior to running this script, in a PowerShell session, you can set the
# following environment variables and it will affect the output# - $env:ChocolateyEnvironmentDebug = 'true' # see output
# - $env:chocolateyIgnoreProxy = 'true' # ignore proxy
# - $env:chocolateyProxyLocation = '' # explicit proxy
# - $env:chocolateyProxyUser = '' # explicit proxy user name (optional)
# - $env:chocolateyProxyPassword = '' # explicit proxy password (optional)# === NO NEED TO EDIT ANYTHING BELOW THIS LINE ===
# Ensure we can run everything
Set-ExecutionPolicy Bypass -Scope Process -Force;# If the repository requires authentication, create the Credential object
if ((-not [string]::IsNullOrEmpty($repoUsername)) -and (-not [string]::IsNullOrEmpty($repoPassword))) {$securePassword = ConvertTo-SecureString $repoPassword -AsPlainText -Force$repoCreds = New-Object System.Management.Automation.PSCredential ($repoUsername, $securePassword)
}$searchUrl = ($packageRepo.Trim('/'), 'Packages()?$filter=(Id%20eq%20%27chocolatey%27)%20and%20IsLatestVersion') -join '/'# Reroute TEMP to a local location
New-Item $env:ALLUSERSPROFILE\choco-cache -ItemType Directory -Force
$env:TEMP = "$env:ALLUSERSPROFILE\choco-cache"
# 46行的变量值携程nupkg的路径,用单引号括起来
$localChocolateyPackageFilePath = 'E:\Downloads\choco\chocolatey.0.10.15.nupkg'
$ChocoInstallPath = "$($env:SystemDrive)\ProgramData\Chocolatey\bin"
$env:ChocolateyInstall = "$($env:SystemDrive)\ProgramData\Chocolatey"
$env:Path += ";$ChocoInstallPath"
$DebugPreference = 'Continue';# PowerShell v2/3 caches the output stream. Then it throws errors due
# to the FileStream not being what is expected. Fixes "The OS handle's
# position is not what FileStream expected. Do not use a handle
# simultaneously in one FileStream and in Win32 code or another
# FileStream."
function Fix-PowerShellOutputRedirectionBug {$poshMajorVerion = $PSVersionTable.PSVersion.Majorif ($poshMajorVerion -lt 4) {try{# http://www.leeholmes.com/blog/2008/07/30/workaround-the-os-handles-position-is-not-what-filestream-expected/ plus comments$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField"$objectRef = $host.GetType().GetField("externalHostRef", $bindingFlags).GetValue($host)$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetProperty"$consoleHost = $objectRef.GetType().GetProperty("Value", $bindingFlags).GetValue($objectRef, @())[void] $consoleHost.GetType().GetProperty("IsStandardOutputRedirected", $bindingFlags).GetValue($consoleHost, @())$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField"$field = $consoleHost.GetType().GetField("standardOutputWriter", $bindingFlags)$field.SetValue($consoleHost, [Console]::Out)[void] $consoleHost.GetType().GetProperty("IsStandardErrorRedirected", $bindingFlags).GetValue($consoleHost, @())$field2 = $consoleHost.GetType().GetField("standardErrorWriter", $bindingFlags)$field2.SetValue($consoleHost, [Console]::Error)} catch {Write-Output 'Unable to apply redirection fix.'}}
}Fix-PowerShellOutputRedirectionBug# Attempt to set highest encryption available for SecurityProtocol.
# PowerShell will not set this by default (until maybe .NET 4.6.x). This
# will typically produce a message for PowerShell v2 (just an info
# message though)
try {# Set TLS 1.2 (3072), then TLS 1.1 (768), then TLS 1.0 (192), finally SSL 3.0 (48)# Use integers because the enumeration values for TLS 1.2 and TLS 1.1 won't# exist in .NET 4.0, even though they are addressable if .NET 4.5+ is# installed (.NET 4.5 is an in-place upgrade).[System.Net.ServicePointManager]::SecurityProtocol = 3072 -bor 768 -bor 192 -bor 48
} catch {Write-Output 'Unable to set PowerShell to use TLS 1.2 and TLS 1.1 due to old .NET Framework installed. If you see underlying connection closed or trust errors, you may need to upgrade to .NET Framework 4.5+ and PowerShell v3+.'
}function Get-Downloader {
param ([string]$url)$downloader = new-object System.Net.WebClient$defaultCreds = [System.Net.CredentialCache]::DefaultCredentialsif (Test-Path -Path variable:repoCreds) {Write-Debug "Using provided repository authentication credentials."$downloader.Credentials = $repoCreds} elseif ($defaultCreds -ne $null) {Write-Debug "Using default repository authentication credentials."$downloader.Credentials = $defaultCreds}$ignoreProxy = $env:chocolateyIgnoreProxyif ($ignoreProxy -ne $null -and $ignoreProxy -eq 'true') {Write-Debug 'Explicitly bypassing proxy due to user environment variable.'$downloader.Proxy = [System.Net.GlobalProxySelection]::GetEmptyWebProxy()} else {# check if a proxy is required$explicitProxy = $env:chocolateyProxyLocation$explicitProxyUser = $env:chocolateyProxyUser$explicitProxyPassword = $env:chocolateyProxyPasswordif ($explicitProxy -ne $null -and $explicitProxy -ne '') {# explicit proxy$proxy = New-Object System.Net.WebProxy($explicitProxy, $true)if ($explicitProxyPassword -ne $null -and $explicitProxyPassword -ne '') {$passwd = ConvertTo-SecureString $explicitProxyPassword -AsPlainText -Force$proxy.Credentials = New-Object System.Management.Automation.PSCredential ($explicitProxyUser, $passwd)}Write-Debug "Using explicit proxy server '$explicitProxy'."$downloader.Proxy = $proxy} elseif (!$downloader.Proxy.IsBypassed($url)) {# system proxy (pass through)$creds = $defaultCredsif ($creds -eq $null) {Write-Debug 'Default credentials were null. Attempting backup method'$cred = get-credential$creds = $cred.GetNetworkCredential();}$proxyaddress = $downloader.Proxy.GetProxy($url).AuthorityWrite-Debug "Using system proxy server '$proxyaddress'."$proxy = New-Object System.Net.WebProxy($proxyaddress)$proxy.Credentials = $creds$downloader.Proxy = $proxy}}return $downloader
}function Download-File {
param ([string]$url,[string]$file)$downloader = Get-Downloader $url$downloader.DownloadFile($url, $file)
}function Download-Package {
param ([string]$packageODataSearchUrl,[string]$file)$downloader = Get-Downloader $packageODataSearchUrlWrite-Output "Querying latest package from $packageODataSearchUrl"[xml]$pkg = $downloader.DownloadString($packageODataSearchUrl)$packageDownloadUrl = $pkg.feed.entry.content.srcWrite-Output "Downloading $packageDownloadUrl to $file"$downloader.DownloadFile($packageDownloadUrl, $file)
}function Install-ChocolateyFromPackage {
param ([string]$chocolateyPackageFilePath = ''
)if ($chocolateyPackageFilePath -eq $null -or $chocolateyPackageFilePath -eq '') {throw "You must specify a local package to run the local install."}if (!(Test-Path($chocolateyPackageFilePath))) {throw "No file exists at $chocolateyPackageFilePath"}$chocTempDir = Join-Path $env:TEMP "chocolatey"$tempDir = Join-Path $chocTempDir "chocInstall"if (![System.IO.Directory]::Exists($tempDir)) {[System.IO.Directory]::CreateDirectory($tempDir)}$file = Join-Path $tempDir "chocolatey.zip"Copy-Item $chocolateyPackageFilePath $file -Force# unzip the packageWrite-Output "Extracting $file to $tempDir..."if ($unzipMethod -eq '7zip') {$7zaExe = Join-Path $tempDir '7za.exe'if (-Not (Test-Path ($7zaExe))) {Write-Output 'Downloading 7-Zip commandline tool prior to extraction.'# download 7zipDownload-File $7zipUrl "$7zaExe"}$params = "x -o`"$tempDir`" -bd -y `"$file`""# use more robust Process as compared to Start-Process -Wait (which doesn't# wait for the process to finish in PowerShell v3)$process = New-Object System.Diagnostics.Process$process.StartInfo = New-Object System.Diagnostics.ProcessStartInfo($7zaExe, $params)$process.StartInfo.RedirectStandardOutput = $true$process.StartInfo.UseShellExecute = $false$process.StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden$process.Start() | Out-Null$process.BeginOutputReadLine()$process.WaitForExit()$exitCode = $process.ExitCode$process.Dispose()$errorMessage = "Unable to unzip package using 7zip. Perhaps try setting `$env:chocolateyUseWindowsCompression = 'true' and call install again. Error:"switch ($exitCode) {0 { break }1 { throw "$errorMessage Some files could not be extracted" }2 { throw "$errorMessage 7-Zip encountered a fatal error while extracting the files" }7 { throw "$errorMessage 7-Zip command line error" }8 { throw "$errorMessage 7-Zip out of memory" }255 { throw "$errorMessage Extraction cancelled by the user" }default { throw "$errorMessage 7-Zip signalled an unknown error (code $exitCode)" }}} else {if ($PSVersionTable.PSVersion.Major -lt 5) {try {$shellApplication = new-object -com shell.application$zipPackage = $shellApplication.NameSpace($file)$destinationFolder = $shellApplication.NameSpace($tempDir)$destinationFolder.CopyHere($zipPackage.Items(),0x10)} catch {throw "Unable to unzip package using built-in compression. Set `$env:chocolateyUseWindowsCompression = 'false' and call install again to use 7zip to unzip. Error: `n $_"}} else {Expand-Archive -Path "$file" -DestinationPath "$tempDir" -Force}}# Call Chocolatey installWrite-Output 'Installing chocolatey on this machine'$toolsFolder = Join-Path $tempDir "tools"$chocInstallPS1 = Join-Path $toolsFolder "chocolateyInstall.ps1"& $chocInstallPS1Write-Output 'Ensuring chocolatey commands are on the path'$chocInstallVariableName = 'ChocolateyInstall'$chocoPath = [Environment]::GetEnvironmentVariable($chocInstallVariableName)if ($chocoPath -eq $null -or $chocoPath -eq '') {$chocoPath = 'C:\ProgramData\Chocolatey'}$chocoExePath = Join-Path $chocoPath 'bin'if ($($env:Path).ToLower().Contains($($chocoExePath).ToLower()) -eq $false) {$env:Path = [Environment]::GetEnvironmentVariable('Path',[System.EnvironmentVariableTarget]::Machine);}Write-Output 'Ensuring chocolatey.nupkg is in the lib folder'$chocoPkgDir = Join-Path $chocoPath 'lib\chocolatey'$nupkg = Join-Path $chocoPkgDir 'chocolatey.nupkg'if (!(Test-Path $nupkg)) {Write-Output 'Copying chocolatey.nupkg is in the lib folder'if (![System.IO.Directory]::Exists($chocoPkgDir)) { [System.IO.Directory]::CreateDirectory($chocoPkgDir); }Copy-Item "$file" "$nupkg" -Force -ErrorAction SilentlyContinue}
}# Idempotence - do not install Chocolatey if it is already installed
if (!(Test-Path $ChocoInstallPath)) {# download the package to the local pathif (!(Test-Path $localChocolateyPackageFilePath)) {# 277行 Download-Package 这个注释掉# Download-Package $searchUrl $localChocolateyPackageFilePath}# Install ChocolateyInstall-ChocolateyFromPackage $localChocolateyPackageFilePath
}

powersell开启信任脚本策略

# 开启信任脚本策略
set-ExecutionPolicy RemoteSigned

管理员powershell执行安装脚本

# 进入脚本所在目录
cd D:\Choco脚本所在目录# 执行安装脚本
iex install.ps1

结果验证

# 查看choco版本
choco -v

在这里插入图片描述


http://chatgpt.dhexx.cn/article/GvOI8ec1.shtml

相关文章

Windows使用choco包管理器

Windows使用choco包管理器 安装chocopowershell 2安装choco choco安装失败修改choco后续安装软件的默认安装路径--有bug&#xff0c;失败 choco包下载目录安装的常见命令行工具位置常见的命令行工具安装目录 choco常见操作搜索软件包查看指定软件的所有版本 安装软件包安装指定…

【choco 安装】chocolatey 安装步骤包管理工具

他可以帮你安装东西&#xff0c;不用下载环境软件&#xff0c;就可以使用。choco 包管理工具 官网 chocolatey 单击我下载 安装要求 Windows 7 / Windows Server 2003PowerShell v2 .NET Framework 4&#xff08;如果您没有安装&#xff0c;则安装将尝试安装.NET 4.0&#x…

eclipse配置tomcat8.5

配置的路异常艰辛。。一定要写一篇博客记录一下&#xff01; 温馨提示&#xff1a; 1.不要下载版本太低的eclipse&#xff0c;eclipse版本最好是eclipse for java EE developers&#xff0c;如果是for java Developers会没有sever&#xff0c;解决办法自行搜索。 2.最好不要下载…

tomcat8下载安装教程

在Apache官网下载Tomcat 8压缩包,在左边Download栏中选择Tomcat 8,如下图所示: 进入https://tomcat.apache.org/download-80.cgi页面,进行下载tomcat 8, 选择下载Tomcat 64-bit Windows zip,如下图所示: 解压压缩包,如下图所示: 打开解压文件夹中的bin目录 【apache-t…

tomcat8.5配置https

一、使用jdk自带的工具生成数字证书&#xff0c;如下&#xff1a; 先进入tomcat conf目录下&#xff0c;创建一个文件夹key 然后&#xff0c; keytool -genkey -v -alias tomcat -keyalg RSA -keystore C:\apache-tomcat-8.5.31\conf\key\tomcat.keystore -validity 36500 k…

Tomcat8安装

1、前置环境 Tomcat 8 对应jdk 1.8 版本&#xff1b;如果你的jdk版本是8以上&#xff0c;则安装对应的tomcat版本。 jdk8 官方下载安装时&#xff0c;先安装jdk版本&#xff0c;最后单独安装jre。所以电脑会有两套jre&#xff0c;一套是jdk中的jre&#xff0c;位于 \jre 目录下…

JDK8+Tomcat8配置https

今天在学习Spring Security的时候&#xff0c;Spring Security有一个功能是能够将一部分URL强制使用HTTPS。因此&#xff0c;就需要让我们的Tomcat能够响应HTTPS。现在网上的很多解决方法都是原来版本的软件来生成密钥和开启Tomcat HTTPS功能。到了JDK8和Tomcat8就有一些不适用…

tomcat8.0安装及配置

1.首先进入官网下载ZIP文件。 步骤&#xff1a; 1.安装tomcat8.0。 解压zip后&#xff0c;配置环境变量&#xff1b; CATALINA_HOME D:\apache-tomcat-8.0.51 CATALINA_BASE D:\apache-tomcat-8.0.51 编辑Path下添加bin路径 %JAVA_HOME%\bin %JAVA_HOME%\jre\bin %CATALINA…

CentOS7安装tomcat8.0图文教程

第一步&#xff1a;下载Tomcat8压缩包 进入 http://tomcat.apache.org/download-80.cgi &#xff08;注&#xff1a;更改文件权限命令&#xff1a;chmod 777 文件名&#xff09; 查看防火墙命令&#xff1a;firewall-cmd --state或者systemctl status firewallld 开启防火墙&…

Apache Tomcat8必备知识

Apache Tomcat8必备知识 作者&#xff1a;chszs&#xff0c;转载需注明。博客主页&#xff1a; http://blog.csdn.net/chszs 一、Apache Tomcat 8介绍Apache Tomcat 8RC1版于前几日发布。它 经过了2年的开发&#xff0c;引入了很多新特征&#xff0c;由于目前还只是Alpha版&a…

windows安装tomcat8.0.50

目录 一、下载tomcat安装包&#xff08;推荐第二种&#xff09; 二、安装tomcat 一、下载tomcat安装包&#xff08;推荐第二种&#xff09; 1.官网下载:Apache Tomcat - Welcome!https://tomcat.apache.org/ ①.选择Tomcat8,点击Archives ②.选择版本v8.0.50 ③.进入bin目录…

tomcat 8 安装

目录 一、安装包下载 1、jdk安装包下载 #如果出现没有rz情况需要进行装包 二、安装 1、安装jdk 2、将tomcat安装包解压 三、开启和关闭 1、开启 2、关闭 一、安装包下载 1、jdk安装包下载 Oracle | Cloud Applications and Cloud Platform 将windows上下载好的包直…

Tomcat8下载

Tomcat8下载&#xff1a; 1、打开官网链接&#xff1a;https://tomcat.apache.org 2、选择Download--》tomcat8&#xff0c;如图所示&#xff1a; 3、选择64位zip包下载&#xff0c;如图所示&#xff1a; 4、将下载好的zip包解压缩即可。

Tomcat8.5安装与配置

1.下载 官网&#xff1a;https://tomcat.apache.org/ 下载后解压到根目录&#xff0c;盘符任意。但必须有jdk&#xff0c;本人用的是jdk1.8。 2.配置环境变量 在classpath这个变量中加入E:\apache-tomcat-8.5.31\bin; 没有就新建一个classpath变量名 3.启动Tomcat 进入Tomcat中…

史上最强Tomcat8性能优化

文章目录 授人以鱼不如授人以渔目的服务器资源Tomcat配置优化Linux环境安装运行Tomcat8AJP连接执行器&#xff08;线程池&#xff09;3种运行模式 部署测试用的web项目查看服务器信息部署web应用使用Apache JMeter进行性能测试下载安装修改语言创建接口的测试用例启动与进行接口…

【 Tomcat 】

文章目录 一、了解 Tomcat二、目录结构三、如何使用3.1 启动tomcat3.2 启动失败的原因 四、部署静态页面4.1 部署单个 HTML4.2 部署带有 CSS / JavaScript / 图片 的 HTML4.3 部署 HTML 到单独的目录中 一、了解 Tomcat Tomcat 是一个 HTTP 服务器. 前面我们已经学习了 HTTP …

如何安装Tomcat 8.0

一.打开下载地址&#xff1a;https://tomcat.apache.org/download-80.cgi 根据需要下载不同版本的Tomcat&#xff0c;并根据操作系统选择不同的下载文件。建议下载zip格式的文件&#xff0c;这样可以免于安装。能够直接使用。当然&#xff0c;一些配置工作还是得做好。 二.解压…

Tomcat 8(一)Tomcat常用配置

操作系统&#xff1a;win8 Jdk版本&#xff1a;1.7.0_51 Jdk目录&#xff1a;C:ProgramFilesJavajdk1.7.0_51 Tomcat版本&#xff1a;8.0.3 Tomcat目录&#xff1a;D:ProgramFilesapache-tomcat-8.0.3 Tomcat8下载地址&#xff1a;http://tomcat.apache.org/download-80.c…

FTP协议详细解析

FTP的工作原理 FTP&#xff08;File Transfer Protocol&#xff09;协议&#xff0c;文件传输协议。提供交互式的访问&#xff0c;对传输文件的格式和类型有分类&#xff0c;允许文件具有存取权限。适合异构网络任意计算机的传送。 FTP有以下基本功能&#xff1a; 1.提供不同…

FTP(文件传输协议)

ftp &#xff08;文件传输协议&#xff09; FTP 是File Transfer Protocol&#xff08;文件传输协议&#xff09;的英文简称&#xff0c;是 TCP/IP 协议组中的协议之一。用于Internet上的控制文件的双向传输。同时&#xff0c;它也是一个应用程序&#xff08;Application&…