Posted On April 28, 2021

Add Chocolatey Private Repo

kimconnect 2 comments
blog.KimConnect.com >> Codes >> Add Chocolatey Private Repo
$privateRepo='kimconnectrepo'
$privateRepoUrl='https://choco.kimconnect.net/chocolatey'
$priority=1

# Set private repo source if it doesn't exist
function addRepo{
  param(
    $privateRepoName,
    $privateRepoUrl,
    $priority=1
  )

  function testUrl($url){
    $HTTP_Request = [System.Net.WebRequest]::Create($url)
    $HTTP_Response = $HTTP_Request.GetResponse()
    $HTTP_Status = [int]$HTTP_Response.StatusCode
    If ($HTTP_Status -eq 200) {
        $valid=$true
    }Else {
      $valid=$false
    }
    If (!($HTTP_Response -eq $null)){ $HTTP_Response.Close()}
    return $valid
  }
  # Install Chocolatey from the Intranet private repo
  [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12  
  if (!(Get-Command choco.exe -ErrorAction SilentlyContinue)){
    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'))}
  $privateRepo="'$privateRepoName' -s '$privateRepoUrl' --priority=$priority"  
  $sources=choco source list
  $privateRepoExists=$sources -match $privateRepoName     
  if (!$privateRepoExists -and $(testUrl $privateRepoUrl)){
      invoke-expression "choco source add -n=$privateRepo"
      return $true
  }elseif($privateRepoExists){
    write-host "Private repo $privateRepoName already exists on $env:computername." -foregroundcolor green
    return $True
  }else{
    return $false
  }
}
addRepo $privateRepo $privateRepoUrl $priority

2 thoughts on “Add Chocolatey Private Repo”

  • This is perfect!

    I was looking for a way to add my private repo for company applications that we use internally to install on multiple computers.
    Do we need to add this to the bottom of the install.ps1 file from chocolatey or is this a standalone file? Any kind of brief setup guide would be great!

    Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

Check Whether an Entity Has Access to a Directory or Its Children

Write-Host "This script just be ran in the context of a File Server Administrators member"…

How To Move WordPress Site To Kubernetes Cluster

a. Create backups of source files and database - Logon to Current Hosting Provider to…

PowerShell: Uninstall Program Using Its Name

# uninstallProgramUsingId.ps1 $appName='Virtual Machine Manager Agent' $uninstallStringRegPaths='HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall','HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall' $uninstallStrings=Get-ChildItem -Path $uninstallStringRegPaths $uninstallString=($uninstallStrings|Get-ItemProperty|Where-Object {$_.DisplayName -match $appName}).UninstallString if($uninstallString.count…