Skip to content

Instantly share code, notes, and snippets.

@sheldonhull
Last active January 27, 2023 09:34
Show Gist options
  • Save sheldonhull/8d6098b87566d2553213d7bb5425490d to your computer and use it in GitHub Desktop.
Save sheldonhull/8d6098b87566d2553213d7bb5425490d to your computer and use it in GitHub Desktop.
[Install-Choco] Install chocolatey using powershell dsc #powershell
configuration ChocoFeatures {
Import-DscResource -ModuleName cChoco
Node 'localhost' {
cChocoFeature powershellHost {
FeatureName = "powershellHost"
Ensure = 'Absent'
}
cChocoFeature allowGlobalConfirmation
{
FeatureName = "allowGlobalConfirmation"
Ensure = 'Present'
}
cChocoFeature useRememberedArgumentsForUpgrades
{
FeatureName = "useRememberedArgumentsForUpgrades"
Ensure = 'Present'
}
cChocoFeature showDownloadProgress
{
FeatureName = "showDownloadProgress"
Ensure = 'Absent'
}
}
}
$config = ChocoFeatures
Start-DscConfiguration -Path $config.psparentpath -Wait -Verbose -Force
Enable-PSRemoting -SkipNetworkProfileCheck -Force
Set-ExecutionPolicy Unrestricted -Force
<#
.Description
Been playing around with using PowerShell DSC to configure some basic resources, and this one is promising.
I have stuck mostly with doing this via nuget, but this is also a viable option and i like that it sets the default install path too so you can use a custom path easily
#>
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
set-location (New-Item 'C:\temp' -ItemType Directory -Force)
Install-Module PSDscResources
Install-Module cChoco
$ErrorActionPreference = 'Stop'
$ChocoInstallDrive = 'C'
$ChocoCacheDIrectory = ('{0}:\temp\choco-cache' -f $ChocoInstallDrive)
New-Item -Path $ChocoCacheDirectory -ItemType Directory -Force -ErrorAction SilentlyContinue
#https://github.com/dsccommunity/xWindowsUpdate/issues/91
Configuration myChocoConfig
{
Import-DscResource -ModuleName PSDscResources
Import-DscResource -ModuleName cChoco
node "localhost"
{
LocalConfigurationManager
{
DebugMode = 'ForceModuleImport'
}
#https://chocolatey.org/docs/chocolatey-configuration#general
cChocoInstaller installChoco
{
InstallDir = 'C:\choco'
}
}
}
try
{
Write-Host "⚡ Initiating myChocoConfig"
myChocoConfig
Start-DscConfiguration myChocoConfig -wait -Verbose -force
}
catch
{
throw
exit 1
}
try
{
$ENV:PATH += ';C:\ProgramData\Chocolatey'
choco --version
choco config set cacheLocation $('{0}:\temp\choco-cache' -f $ChocoInstallDrive)
choco config set upgradeAllExceptions 'vscode'
choco config set commandExecutionTimeoutSeconds 1440
}
catch
{
Write-Error "🔥 Chocolatey failed to install"
throw
exit 1
}
@kiquenet
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment