Script Updating Chocolatey Server with PowerShell
10/03/2022 Update: This works, but the licensed Chocolatey For Business product includes this functionality and works better.
An on-premise Chocolatey server is more useful if it can update it’s own package repository!
$dowload_folder = "C:\tools\chocolatey.server\download\"
$download_source = "https://chocolatey.org/api/v2/package/"
$package_folder = "C:\tools\chocolatey.server\App_Data\Packages\"
$choco_server = "http://localhost/chocolatey"
$choco_server_api_key = "REPLACE_WITH_API_KEY"
$downloaded_packages_items = Get-ChildItem -Path "$dowload_folder"
$downloaded_packages = @()
foreach ($l in $downloaded_packages_items){
$downloaded_packages += $l.Name
}
$download_links = @()
foreach ($s in $software_packages){
$download_links += $download_source + $s
}
$software_packages = @(
"chocolatey-core.extension",
"chocolatey-windowsupdate.extension",
"googlechrome",
"firefox",
"7zip",
"7zip.install",
"notepadplusplus",
"notepadplusplus.install",
"putty",
"putty.install",
)
$download_links | %{
$uri = $_
Write-Host "Waiting 22 seconds . . ."
Start-Sleep -s 22
$request = Invoke-WebRequest -Uri $uri -MaximumRedirection 0 -ErrorAction Ignore
$location = $request.Headers.Location
$repo_version = $location.SubString($location.LastIndexOf('/') + 1)
$package = $repo_version -split '\.+[0-9]'
$package = $package[0]
$package_index = $package.length
$version = $repo_version.substring($package_index)
$version = $version.substring(1)
$version = $version -replace ".{6}$"
$output = $dowload_folder + $location.SubString($location.LastIndexOf('/') + 1)
$installed_packages = Get-ChildItem "$package_folder" |
Where-Object {$_.PSIsContainer} |
Foreach-Object {$_.Name}
$installed_versions = Get-ChildItem "$package_folder$package" |
Where-Object {$_.PSIsContainer} |
Foreach-Object {$_.Name}
Write-Host "`r`n"
#Write-Host $package_index
Write-Host "Repo File: " $repo_version
Write-Host "Repo Package: " $package
Write-Host "Repo Version: " $version
if($repo_version -notin $downloaded_packages){
Invoke-WebRequest -Uri $location -OutFile $output
Write-Host "Waiting 60 seconds . . ."
Start-Sleep -s 60
}
else{
Write-Host $repo_version "is alreay downloaded to" $dowload_folder
Write-Host "Skipping download of" $repo_version
}
if(($package -notin $installed_packages) -or ($version -notin $installed_versions)){
choco push $dowload_folder$repo_version --source $choco_server --api-key=$choco_server_api_key
}
else{
Write-Host $repo_version "is alreay installed to" $package_folder
Write-Host "Skipping install of" $repo_version
}
}
Further reading:
https://chocolatey.org/
https://docs.chocolatey.org/en-us/guides/organizations/set-up-chocolatey-server